-
Notifications
You must be signed in to change notification settings - Fork 0
/
MST.cpp
39 lines (39 loc) · 846 Bytes
/
MST.cpp
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
struct edge{
int a,b,c;
edge(){}
edge(int a, int b, int c):a(a),b(b),c(c){}
bool operator < (const edge &p)const{
return c<p.c;
}
};
vector<edge>adj;
int par[N];
int find(int n){
if(par[n]==n)return n;
return par[n]=find(par[n]);
/*int tmp = n;
while(par[tmp] != tmp)
tmp = par[tmp];
while(par[n] != tmp){
int tmp_x = par[n];
par[n] = tmp;
n = tmp_x;
}
return tmp;*/
}
int mst(int n){
sort(ALL(adj));
for(int i=1;i<=n;i++) par[i]=i;
int cnt=0,res=0;
for(int i=0;i<adj.size();i++){
int x=find(adj[i].a);
int y=find(adj[i].b);
if(x!=y){
par[x]=y;
cnt++;
res+=adj[i].c;
if(cnt==n-1)break;
}
}
return res;
}