HDU1213 How Many Tables(简单的并查集)
2013-03-23 09:36
543 查看
找了个简单的并查集题目练手,对并查集复习了一遍,留个模板作为备忘。
点击打开链接
点击打开链接
#include <iostream> #include <string> using namespace std; int father[1010],a,b,visit[1010]; int find(int x) { if(x!=father[x]) father[x]=find(father[x]); return father[x]; } void mere(int x,int y) { x=find(x); y=find(y); father[y]=x; } int main() { int i,m,n,t,j,a,b; cin>>t; while(t--) { int count=0; memset(visit,0,sizeof(visit)); cin>>n>>m; for(i=1;i<=n;i++) father[i]=i; for(i=1;i<=m;i++) { cin>>a>>b; a=find(a); b=find(b); if(a!=b) mere(a,b); } for(i=1;i<=n;i++) { if(visit[i]) continue; // visit[i]=1; for(j=i+1;j<=n;j++) { if(!visit[j]&&find(i)==find(j)) { visit[j]=1; } } count++; } cout<<count<<endl; } 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(并查集,简单)
- hdu 1213 How Many Tables(并查集的简单应用)
- HDU 1213 How Many Tables(简单并查集)
- hdu 1213 How Many Tables(UFS 简单并查集)
- 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(并查集,简单题)
- 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(并查集)
- HDU - 1213 How Many Tables解题报告(并查集)
- HDU---1213(How Many Tables,并查集判连通分支数)