hdoj 1213 How Many Tables 并查集模板
2016-08-06 17:41
363 查看
简单的并查集应用
How Many Tables
#include<iostream> using namespace std; int f[1001]; int findroot(int x) { if(x == f[x])return x; else return findroot(f[x]); } void unionset(int x,int y) { x=findroot(x); y=findroot(y); f[x] = y; //集合拼接 } int main() { int t,m,n,a,b,ans; cin>>t; while(t--) { ans = 0; cin>>m>>n; for(int i = 1;i<=m;i++)//初始化时每个元素本身代表一个集合 f[i]=i; for(int i=0;i<n;i++) { cin>>a>>b; unionset(a,b); //将元素合并到一个集合 } for(int i =1;i<=m;i++) //找尾巴的数量,一条尾“尾巴”代表含有一个集合 if(f[i]==i) ans++; cout<<ans<<endl; } }
相关文章推荐
- 【HDOJ1213】How Many Tables[并查集模板题]
- HDOJ 1213 How Many Tables(并查集)
- HDOJ1213 How Many Tables(并查集)
- HDOJ 1213 How Many Tables(并查集)
- hdu 1213 How Many Tables 并查集模板题+路径压缩
- POJ-1213 How Many Tables (并查集模板题)
- HDOJ1213 How Many Tables 并查集裸题
- Hdoj 1213 How Many Tables 【并查集】
- HDOJ 1213 how many tables (并查集)
- HDU 1213 How Many Tables(并查集模板)
- HDOJ 1213 How Many Tables——并查集
- HDU 1213 How Many Tables(并查集入门模板题)
- HDU ~ 1213 ~ How Many Tables (并查集模板题)
- hdoj-1213-How Many Tables【并查集】
- hdoj 1213 How Many Tables (并查集||最小生成树)
- 【HDU 1213】How many tables(并查集模板)
- HDU 1213 How Many Tables(模板——并查集)
- hdoj1213 How many tables?(并查集)
- hdu 1213 (How Many Tables)(简单的并查集,纯模板)
- HDU-1213-How Many Tables(并查集模板题)