标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17411 Accepted Submission(s):
8527
点我
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 int f[1000]; 5 int find(int x)//查找x的祖先结点 6 { 7 while(x!=f[x]) 8 x=f[x]; 9 return x; 10 } 11 int merge(int x,int y) 12 { 13 int fx,fy; 14 fx=find(x); 15 fy=find(y); 16 if(fx!=fy)//判断他们是不是在一个集合里 17 f[fx]=fy;//合并 18 } 19 int main() 20 { 21 int n; 22 freopen("in.txt","r",stdin); 23 cin>>n; 24 while(n--) 25 { 26 int num,i,j,rel,a,b,count=0; 27 cin>>num>>rel; 28 for(i=1;i<=num;i++) 29 f[i]=i; 30 for(i=0;i<rel;i++) 31 { 32 cin>>a>>b; 33 merge(a,b); 34 } 35 for(i=1;i<=num;i++) 36 { 37 if(f[i]==i) 38 count++; 39 } 40 if(n!=0) 41 getchar(); 42 cout<<count<<endl; 43 } 44 }
标签:
原文地址:http://www.cnblogs.com/a1225234/p/4596505.html