题目1445:How Many Tables
来源:互联网 发布:知乎和果壳 编辑:程序博客网 时间:2024/05/26 22:57
// 并查集,求有多少个集合,即根结点个数。。。
#include<stdio.h>
#define N 1000int Tree[N];
int findRoot(int x){
if(Tree[x]==-1) return x;
else{
int tmp=findRoot(Tree[x]);
Tree[x]=tmp;
return tmp;
}
}
int main()
{
int t;
while(scanf("%d",&t)!=EOF){
while(t--){
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) Tree[i]=-1;
while(m--){
int a,b;
scanf("%d%d",&a,&b);
a=findRoot(a); b=findRoot(b);
if(a!=b) Tree[a]=b;
}
int ans=0;
for(int i=1;i<=n;i++){
if(Tree[i]==-1) ans++;
}
printf("%d\n",ans);
}
}
return 0;
}
0 0
- 题目1445:How Many Tables
- 题目1445:How Many Tables
- 题目1445:How Many Tables
- 【九度】题目1445:How Many Tables
- 九度 题目1445:How Many Tables
- 九度 题目1445:How Many Tables
- 题目69:How Many Tables
- hdu 题目 题目1213 How Many Tables
- 九度oj 题目1445:How Many Tables
- 【九度OJ】题目1445:How Many Tables 解题报告
- 题目1445:How Many Tables 九度OJ
- 九度OJ-1445:How Many Tables
- How Many Tables
- HDOJ How Many Tables
- hduoj1213:How Many Tables
- hdu How Many Tables
- HDU1213:How Many Tables
- HDU1213--How Many Tables
- Android 两个Activity之间切换时背景能否改变
- JVM内存管理和JVM垃圾回收机制
- 编程可以教给你的6个生活习惯
- EXT 做的导航树 可以调整节点顺序
- POJ 1988 Cube Stacking(路径压缩并查集)
- 题目1445:How Many Tables
- centos apache的基本操作命令
- md5
- Gson的使用汇总
- Qt学习笔记
- vxWorks/BootROM Imageq启动顺序详解
- 如何优化MySQL insert性能
- 软件的安装技术
- 使用HASH技术进行排重优化的案例