Archives
Trending
Support
Login
clear text
XML
Django
JavaScript
MATLAB
C
C++
C#
Python
SQL
Shell
Bash
Markdown
YAML
JSON
HTML
CSS
PHP
Java
Ruby
Go
Rust
Swift
Kotlin
Arduino
TypeScript
Perl
Autohotkey
Lua
SQF
R
Scala
Haskell
Groovy
Dart
Clojure
VB.NET
Objective-C
PowerShell
Bash
CoffeeScript
Verilog
#include
using namespace std; const int maxn = 1e5 + 10; int idx[maxn]; int sz[maxn]; void init() { for(int i = 0; i < maxn; i++) { idx[i] = i; sz[i] = 1; } } int find_root(int A) { while(idx[A] != A) { idx[A] = idx[idx[A]]; A = idx[A]; } return A; } void unite(int A, int B) { int root_A = find_root(A); int root_B = find_root(B); if(root_A != root_B) { if(sz[root_A] < sz[root_B]) { sz[root_B] += sz[root_A]; idx[root_A] = idx[root_B]; } else { idx[root_B] = idx[root_A]; sz[root_A] += sz[root_B]; } } } bool check_if_they_belong_to_same_set(int A, int B) { return find_root(A) == find_root(B); } int main() { init(); int n, m; cin >> n >> m; vector
>> graph; for(int i = 0; i < m; i++) { int a, b, c; cin >> a >> b >> c; graph.push_back(make_pair(c, make_pair(a, b))); } sort(graph.begin(), graph.end()); int res = 0; for(int i = 0; i < m; i++) { int a = graph[i].second.first; int b = graph[i].second.second; int c = graph[i].first; if(!check_if_they_belong_to_same_set(a, b)) { unite(a, b); res += c; } } cout << res << endl; }
Mark as private (unlisted)
for 30 minutes
for 6 hours
for 1 day
for 1 week
for 1 month
for 1 year
everlasting (like CCCP)