-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkruskal.cpp
More file actions
42 lines (37 loc) · 710 Bytes
/
kruskal.cpp
File metadata and controls
42 lines (37 loc) · 710 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
#include <bits/stdc++.h>
using namespace std;
const int N=100010;
struct aresta{
int x,y,t;
};
int pai[N];
aresta mst[N],v[N];
bool f(aresta a, aresta b){return a.t < b.t;}
int find(int a){
if (a==pai[a])return a;
return pai[a]=find(pai[a]);
}
void unite(int a,int b){
a=find(a);
b=find(b);
pai[b]=a;
}
int main(){
int n,m,tam=1;
scanf("%d %d", &n, &m);
for (int i=1;i<=n;i++)pai[i]=i;
for (int i=1;i<=m;i++){
cin >> v[i].x >> v[i].y >> v[i].t;
}
sort(v+1,v+m+1,f);
for (int i=1;i<=m;i++){
if(find(v[i].x)!=find(v[i].y)){
unite(v[i].x,v[i].y);
mst[tam]=v[i];
tam++;
}
}
for (int i=1;i<n;i++){
cout << mst[i].x << " " << mst[i].y << " " << mst[i].t << endl;
}
}