并查集——How Many Tables
来源:互联网 发布:怎么申请网站域名 编辑:程序博客网 时间:2024/06/05 09:10
小明马上要过生日,要邀请N个好友,而他的好友都只愿意和认识的人坐,给出朋友之间的认识关系,假设所有认识的人坐一桌,问最少需要多少桌子。
规定:若A认识B,B认识C,则A认识C,则ABC全部认识,可以坐一桌; 例如:小明的好友为 A B C D E 五人,关系式 A-B B-C D-E。则此时需要两个桌子:ABC一桌,DE一桌。 输入N表示好友人数,M表示关系数,接下来M行显示所有关系。
Sample Input
2
5 3
1 2
2 3
4 5
5 1
2 5
Sample Output
2
4
#include<stdio.h>int father[1005];int n;void chuhua(){ int i; for(i=0;i<=n;i++) { father[i]=i; }}int getfather(int x){ while(x!=father[x]) { x=father[x]; } return x;}void mix(int a,int b){ int fa,fb; fa=getfather(a); fb=getfather(b); if(fa!=fb) { father[fa]=fb; }}int main(){ int t,s,i,m,v1,v2; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); chuhua(); while(m--) { scanf("%d%d",&v1,&v2); mix(v1,v2); } int i; s=0; for(i=1;i<=n;i++) { if(father[i]==i) { s++; } } printf("%d\n",s); } return 0;}
题目传送门:How Many Tables点此可交题
0 0
- HDOJ 1213 How Many Tables——并查集
- HDU1213——How Many Tables(并查集)
- 并查集——How Many Tables
- 【hdu1213】How Many Tables【并查集】
- How Many Tables(并查集)
- How Many Tables hdu1213 并查集
- hdu1213(How Many Tables 并查集)
- hdu_1213 how many tables (并查集)
- Hdu1213 - How Many Tables - 并查集
- HDU1213 How Many Tables 【并查集】
- 并查集---How Many Tables
- HDU1213 How Many Tables【并查集】
- HDU1213 How Many Tables 【并查集】
- [并查集]How Many Tables
- hdu1213 How Many Tables(并查集)
- hdoj How Many Tables【并查集】
- hdoj1213 How many tables?(并查集)
- How Many Tables-并查集
- Server Tomcat v7.0 Server at localhost failed to start.
- 【C++标准I/0库】错误:runtime_error was not declared
- 算法之归并排序
- 【湖南集训 4.7】sanrd
- STL学习笔记— —容器forward_list
- 并查集——How Many Tables
- 在Androidstudio配置svn上找不到svn.exe解决方法
- 06_判断101-200之间有多少个素数,并输出所有素数
- 数据结构(9):二叉树的遍历、构建以及显示(凹入法)
- 算法_动态规划_矩阵路径最大和
- IPC基础小记 关于Binder
- 畅通工程系列问题题解(HDU 1233 1863 1875 1879)
- c++作业3-定期存款利息计算器
- ZCCZCC Loves Codefires