pipioj 1454: 网络工程师(kruskal)
作者:互联网
1 #define bug(x) cout<<#x<<" is "<<x<<endl 2 #define IO std::ios::sync_with_stdio(0) 3 #include <bits/stdc++.h> 4 using namespace std; 5 typedef long long ll; 6 #define mk make_pair 7 #define pb push_back 8 const int inf=2147483647; 9 const int N=1e3+10; 10 int fa[N],a[N]; 11 vector<int>g[N],v[N]; 12 int find(int x){ 13 return fa[x]==x?x:fa[x]=find(fa[x]); 14 } 15 int n,m,k; 16 struct node{ 17 int x,y,w; 18 }b[100005]; 19 20 21 bool cmp(node p,node q){ 22 return p.w<q.w; 23 } 24 int main(){ 25 IO; 26 while(cin>>n>>m>>k){ 27 for(int i=1;i<=n;i++)fa[i]=i; 28 for(int i=1;i<=n;i++){ 29 cin>>a[i]; 30 } 31 for(int i=1;i<=m;i++){ 32 int x,y; 33 cin>>x>>y; 34 b[i].x=x; 35 b[i].y=y; 36 if(a[x]==a[y]){ 37 b[i].w=0; 38 } 39 else{ 40 b[i].w=1; 41 } 42 } 43 sort(b+1,b+1+m,cmp); 44 int ans=0,cnt=0; 45 for(int i=1;i<=m;i++){ 46 //bug(i); 47 int x=b[i].x; 48 int y=b[i].y; 49 x=find(x); 50 y=find(y); 51 if(x!=y){ 52 ans+=b[i].w; 53 fa[x]=y; 54 cnt++; 55 } 56 if(cnt==n-1)break; 57 } 58 cout<<ans<<endl; 59 } 60 61 } 62 /* 63 4 4 4 64 1 2 3 4 65 1 2 66 2 3 67 3 4 68 4 1 69 70 4 3 2 71 1 1 2 2 72 1 3 73 2 3 74 3 4 75 */
标签:node,const,int,kruskal,1454,fa,pipioj,find,define 来源: https://www.cnblogs.com/ccsu-kid/p/14269502.html