hdu 1213 How Many Tables
2015-01-02 13:38
381 查看
acm.hdu.edu.cn/showproblem.php?pid=1213
简单并查集,统计集合个数类型
AC代码:
简单并查集,统计集合个数类型
AC代码:
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> using namespace std; const int MAX = 1010; int n,m,father[MAX]; void Init(){ for(int i=0; i<=n; i++) father[i] = i; } int Find(int x){ if(x != father[x]) father[x] = Find(father[x]); return father[x]; } int main(){ int ca; scanf("%d",&ca); while(ca--){ scanf("%d%d",&n,&m); Init(); for(int i=0; i<m; i++){ int a,b; scanf("%d%d",&a,&b); int u = Find(a),v = Find(b); // cout << u << " " << v << endl; father[v] = u; } int ans = 0; for(int i=1; i<=n; i++) if(father[i] == i) ans++; printf("%d\n",ans); } return 0; }
相关文章推荐
- 【并查集】 HDU 1213 How Many Tables
- hdu-1213-How Many Tables
- 并查集之求连通块的数量 HDU - 1213 How Many Tables
- HDU 1213 How Many Tables
- hdu_1213 How Many Tables
- POJ-2236-Wireless Network 和 HDU-1213-How Many Tables
- How Many Tables HDU - 1213
- HDU 1213 How Many Tables
- hdu 1213 How Many Tables
- HDU 1213 How Many Tables
- hdu 1213 How Many Tables
- HDU1213-How Many Tables
- HDU 1213 How Many Tables
- HDU 1213 How Many Tables
- HDU-1213-How Many Tables
- hdu 1213 How Many Tables
- How Many Tables HDU - 1213 并查集
- hdu 1213 How Many Tables
- Hdu 1213 - How Many Tables
- hdu 1213:How Many Tables