标签:
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 741 Accepted Submission(s):
429
#include<stdio.h> #include<string.h> #define MAX 1010 int set[MAX]; int sum=0; int find(int fa) { int t; int ch=fa; while(fa!=set[fa]) fa=set[fa]; while(ch!=fa) { t=set[ch]; set[ch]=fa; ch=t; } return fa; } void mix(int x,int y) { int fx,fy; fx=find(x); fy=find(y); if(fx!=fy) set[fx]=fy; else sum++; } int main() { int n,m,i,a,b; while(scanf("%d%d",&n,&m)!=EOF) { for(i=0;i<=n;i++) set[i]=i; for(i=0;i<m;i++) { scanf("%d%d",&a,&b); mix(a,b); } printf("%d\n",sum); sum=0; } return 0; }
标签:
原文地址:http://www.cnblogs.com/tonghao/p/4539627.html