-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkruskal_fast.cpp
More file actions
53 lines (44 loc) · 908 Bytes
/
kruskal_fast.cpp
File metadata and controls
53 lines (44 loc) · 908 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<bits/stdc++.h>
using namespace std;
const int MX = 1e6;
int sz[MX], par[MX];
void make(int n){
for(int i = 1; i<=n; i++) par[i]=i , sz[i] = 1;
}
int find(int u){
return par[u] = par[u] == u ? u : find(par[u]);
}
void uni(int a, int b){
a = find(a), b = find(b);
if(a == b) return;
if(sz[a] < sz[b]) swap(a, b);
sz[a] += sz[b];
par[b] = a;
}
bool check(int a, int b){
return find(a) == find(b);
}
int main(){
int n, m; cin>>n>>m;
make(n);
priority_queue<array<int, 3>,vector<array<int, 3>> ,greater<array<int , 3>>> adj;
for(int i = 0; i<m; i++){
int u, v, w; cin>>u>>v>>w;
adj.push({w, u, v});
}
vector<array<int, 3>> ans;
int e = 0;
while(adj.size()){
auto x = adj.top();
adj.pop();
if(check(x[1], x[2])) continue;
uni(x[1], x[2]);
ans.push_back(x);
e++;
if(e == n-1) break;
}
for(auto x: ans){
cout<<x[1]<<" "<<x[2]<<" "<<x[0]<<endl;
}
return 0;
}