hdoj 1213.How Many Tables
2012-11-28 21:22
141 查看
How Many Tables
#include<iostream> #include<cstdio> using namespace std; const int MAXN=1000+10; int n,father[MAXN]; void init(){ for(int i=1;i<=n;i++){ father[i]=i; } } int getfather(int a){ int r; r=a; while(r!=father[r]){ r=father[r]; } int i=a; while(i!=father[i]){ i=father[i]; father[i]=r; } return r; /* if(a!=father[a]){ father[a]=getfather(father[a]); } return a; */ } void Union(int a,int b){ int fx=getfather(a); int fy=getfather(b); if(fx!=fy){ father[fx]=fy; } } int main() { int ct,m; scanf("%d",&ct); for(int i=0;i<ct;i++){ int cnt=0; scanf("%d %d",&n,&m); init(); for(int j=0;j<m;j++){ int a,b; scanf("%d %d",&a,&b); Union(a,b); } for(int k=1;k<=n;k++){ if(k==father[k]){ cnt++; } } printf("%d\n",cnt); } return 0; }
相关文章推荐
- 【hdoj2734】Quicksum
- Big Event in HDU 【HDOJ-1171-母函数】
- HDOJ 2544 最短路 (简单的最短路之spfa)
- hdoj1005
- HDOJ HDU 2602 Bone Collector
- HDOJ4115-Eliminate the Conflict,2SAT
- HDOJ 5012 Dice--2014网络赛西安赛区F题
- HDOJ 1789 Doing Homework again (贪心&&优先队列)
- (Java实现) HDOJ 2024 && 2043 Character类方法的使用
- HDOJ2795 线段树查询区间最大值
- hdoj2046
- HDOJ 1880 魔咒词典
- HDOJ 2199 Can you solve this equation? (二分)
- HDOJ(HDU) 1708 Fibonacci String
- HDOJ2003
- HDOJ_ACM_命运
- HDOJ 5008 Boring String Problem
- HDOJ 2120 Ice_cream's world I (并查集)
- HDOJ(HDU) 1785 You Are All Excellent(角度运算)
- HDOJ2019