#include <iostream> #include <cmath> #include <cstring> using namespace std; int f[10010],n,m; void chushi() { for(int i=1;i<=n;i++) f[i]=i; return; } int father(int x) { if(f[x]==x) return f[x]; else { f[x]=father(f[x]); return f[x]; } } void hebing(int tm,int zhh) { int ltj1,ltj2; ltj1=father(tm); ltj2=father(zhh); if(ltj1!=ltj2) { f[ltj2]=ltj1; } return; } int main() { int sum=0; scanf("%d%d",&n,&m); chushi(); for(int i=1;i<=m;i++) { int x,y; scanf("%d%d",&x,&y); hebing(x,y); } for(int i=1;i<=n;i++) { if(f[i]==i) sum++; } printf("%d",sum); }
转载于:https://www.cnblogs.com/jason2003/p/6572398.html
相关资源:JAVA上百实例源码以及开源项目