标签:
1 #include<iostream> 2 #define MAXN 50000 3 using namespace std; 4 5 int parent[MAXN]; 6 int Find_set(int); 7 void Make_set(int); 8 void Union(int,int); 9 int main() 10 { 11 //freopen("acm.acm","r",stdin); 12 int p; 13 int s; 14 int i; 15 int j; 16 int m; 17 int n; 18 int ans; 19 int time; 20 time = 1; 21 while(cin>>p>>s) 22 { 23 if(!p&&!s) 24 break; 25 ans = 0; 26 Make_set(p); 27 for(i = 0; i < s; ++ i) 28 { 29 cin>>m>>n; 30 Union(m-1,n-1); 31 } 32 for(i = 0; i < p; ++ i) 33 { 34 if(parent[i] == i) 35 { 36 ++ ans; 37 } 38 } 39 cout<<"Case "<<time++<<": "<<ans<<endl; 40 } 41 } 42 void Make_set(int n) 43 { 44 int i; 45 for(i=0;i<n;i++) 46 parent[i]=i; 47 } 48 int Find_set(int x) 49 { 50 if(x!=parent[x]) 51 parent[x]=Find_set(parent[x]); 52 return parent[x]; 53 } 54 void Link(int x,int y) 55 { 56 parent[y]=x; 57 } 58 void Union(int x,int y) 59 { 60 Link(Find_set(x),Find_set(y)); 61 }
标签:
原文地址:http://www.cnblogs.com/gavinsp/p/4568469.html