标签:
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 839 Accepted Submission(s):
488
1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 #define maxn 10010 5 using namespace std; 6 7 int per[maxn],sum; 8 void init() 9 { 10 int i; 11 for(i=0;i<maxn;i++) 12 { 13 per[i]=i;//初始化数组 14 } 15 } 16 int find(int x)//查找根节点 17 { 18 int t=x; 19 while(t!=per[t]) 20 t=per[t]; 21 return t; 22 } 23 void join(int x,int y) 24 { 25 int fx=find(x); 26 int fy=find(y); 27 if(fx==fy) 28 sum++;//环的个数 29 else 30 per[fx]=fy; 31 } 32 int main() 33 { 34 int a,b,i,m,n; 35 while(scanf("%d%d",&a,&b)!=EOF) 36 { 37 init(); 38 sum=0; 39 for(i=0;i<b;i++) 40 { 41 scanf("%d%d",&m,&n); 42 join(m,n); 43 } 44 printf("%d\n",sum); 45 } 46 return 0; 47 }
标签:
原文地址:http://www.cnblogs.com/Eric-keke/p/4687497.html