How Many Tables

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 17892 Accepted Submission(s): 8794

Problem Description
Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want
to stay with strangers.



One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table.



For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs 2 tables at least.
Input
The input starts with an integer T(1<=T<=25) which indicate the number of test cases. Then T test cases follow. Each test case starts with two integers N and M(1<=N,M<=1000). N indicates the number of friends, the friends are marked
from 1 to N. Then M lines follow. Each line consists of two integers A and B(A!=B), that means friend A and friend B know each other. There will be a blank line between two cases.
Output
For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.
Sample Input
2
5 3
1 2
2 3
4 5 5 1
2 5
Sample Output
2
4
Author
Ignatius.L
Source
Recommend
Eddy | We have carefully selected several similar problems for you:

pid=1856" target="_blank">
1856

pid=1325" target="_blank">
1325
1102 1301 1116


#include<stdio.h>
int root[1010];
int find(int i){
if(root[i]==i) return i;
return root[i]=find(root[i]);
}
void unio(int i,int j){
if(find(i)<=find(j)) root[j]=find(i);
else root[i]=find(j);
return;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
int n,m,i,x,y,nu=0;
scanf("%d%d",&n,&m);
for(i=1;i<=n;++i) root[i]=i;
for(i=1;i<=m;++i){
scanf("%d%d",&x,&y);
if(find(x)!=find(y))
unio(find(x),find(y));
//unio(x,y); //此处写法是错误的,在这里WA了多次! !!!! !! !。!!!!改动的应是各自的根
}
for(i=1;i<=n;++i){
if(root[i]==i) nu++;
}
printf("%d\n",nu);
}
return 0;
}

hdoj-1213-How Many Tables【并查集】的更多相关文章

  1. HDU 1213 - How Many Tables - [并查集模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...

  2. HDU 1213 How Many Tables 并查集 水~

    http://acm.hdu.edu.cn/showproblem.php?pid=1213 果然是需要我陪跑T T,禽兽工作人员还不让,哼,但还是陪跑了~ 啊,还有呀,明天校运会终于不用去了~耶耶耶 ...

  3. HDU 1213 How Many Tables(并查集,简单)

    题解:1 2,2 3,4 5,是朋友,所以可以坐一起,求最小的桌子数,那就是2个,因为1 2 3坐一桌,4 5坐一桌.简单的并查集应用,但注意题意是从1到n的,所以要减1. 代码: #include ...

  4. HDU 1213 How Many Tables (并查集,常规)

    并查集基本知识看:http://blog.csdn.net/dellaserss/article/details/7724401 题意:假设一张桌子可坐无限多人,小明准备邀请一些朋友来,所有有关系的朋 ...

  5. HDU 1213 How Many Tables 并查集 寻找不同集合的个数

    题目大意:有n个人 m行数据,每行数据给出两个数A B,代表A-B认识,如果A-B B-C认识则A-C认识,认识的人可以做一个桌子,问最少需要多少个桌子. 题目思路:利用并查集对相互认识的人进行集合的 ...

  6. POJ-1213 How Many Tables( 并查集 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday. ...

  7. hdu1213 How Many Tables 并查集的简单应用

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 简单的并查集 代码: #include<iostream> #include< ...

  8. Hdoj 1213.How Many Tables 题解

    Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. ...

  9. C - How Many Tables 并查集

    Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to kn ...

  10. hdu1213 How Many Tables(并查集)

    How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

随机推荐

  1. mac废纸篓清空的心得、mac设置不睡眠不待机不锁屏、如何快速锁屏待机睡眠、mac重启、mac学习的必备软件-城

    mac废纸篓清空: 1.使用废纸篓的清空废纸篓,清空所有包括被锁定的文件: 2.使用“磁盘工具”的“修复磁盘权限”,修复完成再操作清空废纸篓: 3.使用cleanmymac软件“垃圾清理”和“擦除器” ...

  2. Mac-安装itellij idea

    1.安装解压 Unzip and copy JetbrainsCrack.jar to /Applications/CLion.app/Contents/bin/JetbrainsCrack.jar ...

  3. 安装XHProf分析PHP性能瓶颈(原创)

    废话不多说,直接上代码 ,手动滑稽.o(╯□╰)o   如果已解决您的问题,请在文章底部点击下关注,非常感谢. 下面是LINUX命令行 $ wget http://pecl.php.net/get/x ...

  4. hdu5819

    补多校系列,具体见多校题解http://www.cnblogs.com/duoxiao/p/5777700.html 值得注意的是如果当前i初始向左,前i个骑士最终只有1个向右 对于f[i][1]状态 ...

  5. (五)agentd端cpu的触发器配置

    配置===>模板===>选择对应的模板===> 这里我验证触发器是否有效,定义的触发器的值超过0.01就出发报警,这里我做的是最新的T值超过0.01就触发触发器 验证,说明触发器触发 ...

  6. java异常基本知识

    Throwable     |--Error     |--Exception         |--RuntimeException                异常体系的特点:异常体系中的所有类 ...

  7. 从1到整数n中1出现的次数

    题目:输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数.例如输入12,从1到12这些整数中包含1的数字有1,10,11和12共出现了5次.   不考虑时间效率的解法: int Numbe ...

  8. web.input()和web.data() 遇到特殊字符

    使用web.py的时候,web.input()和web.data() 都可以接收用户从浏览器端输入的参数. web.input()方法返回一个包含从url(GET方法)或http header(POS ...

  9. java开发3~5年工作经验面试题

    关于java基础 String,StringBuilder,StringBuffer区别是什么?底层数据结构是什么?分别是如何实现的? HashSet的底层实现是什么?它与HashMap有什么关系? ...

  10. 洛谷——P1722 矩阵 II

    P1722 矩阵 II 题目背景 usqwedf 改编系列题. 题目描述 如果你在百忙之中抽空看题,请自动跳到第六行. 众所周知,在中国古代算筹中,红为正,黑为负…… 给定一个1*(2n)的矩阵(us ...