How Many Tables

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 27812    Accepted Submission(s): 13801

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:  1232 1856 1233 1198 1863
/*
并查集入门题,无坑点。
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = ;
int fa[maxn];
int T, N, M;
int u, v;
int Find(int u)
{
if(u == fa[u]) return fa[u];
else return fa[u] = Find(fa[u]);
}
void adde(int u, int v)
{
int x = Find(u);
int y = Find(v);
if(x != y) fa[x] = fa[y];
}
int main ()
{
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &N, &M);
for(int i = ; i <= N; i++)
fa[i] = i;
for(int i = ; i <= M; i++)
{
scanf("%d%d", &u, &v);
adde(u, v);
}
int ans = ;
for(int i = ; i <= N; i++)
{
if(fa[i] == i) ans++;
}
printf("%d\n", ans);
}
return ;
}

HDU 1213(并查集)的更多相关文章

  1. hdu 1213(并查集模版题)

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

  2. hdu 4514 并查集+树形dp

    湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  3. HDU 3926 并查集 图同构简单判断 STL

    给出两个图,问你是不是同构的... 直接通过并查集建图,暴力用SET判断下子节点个数就行了. /** @Date : 2017-09-22 16:13:42 * @FileName: HDU 3926 ...

  4. HDU 4496 并查集 逆向思维

    给你n个点m条边,保证已经是个连通图,问每次按顺序去掉给定的一条边,当前的连通块数量. 与其正过来思考当前这边会不会是桥,不如倒过来在n个点即n个连通块下建图,检查其连通性,就能知道个数了 /** @ ...

  5. HDU 1232 并查集/dfs

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...

  6. HDU 2860 并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=2860 n个旅,k个兵,m条指令 AP 让战斗力为x的加入y旅 MG x旅y旅合并为x旅 GT 报告x旅的战斗力 ...

  7. hdu 1198 (并查集 or dfs) Farm Irrigation

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1198 有题目图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇 ...

  8. hdu 1598 (并查集加贪心) 速度与激情

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1598 一道带有贪心思想的并查集 所以说像二分,贪心这类基础的要掌握的很扎实才行. 用结构体数组储存公 ...

  9. hdu 4496(并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4496. 思路:简单并查集应用,从后往前算就可以了. #include<iostream> ...

随机推荐

  1. 可编辑表格(Editable Table)

    需求分析 1.单击table的每个cell后,给cell加上一个尺寸相当的input; 2.input后把value传给cell的innerHTML; 3.失焦后删除input. HTML <! ...

  2. 垂直居中vertical-align

    vertical-align对一些特定显示样式(例如单元格显示方式:table-cell)的元素才会起作用. div{display:table-cell; vertical-align:middle ...

  3. C# 遍历枚举

    C#中,如何获取(遍历)枚举中所有的值: public enum Suits { Spades, Hearts, Clubs, Diamonds, NumSuits } private static ...

  4. perf + 火焰图分析程序性能

    1.perf命令简要介绍 性能调优时,我们通常需要分析查找到程序百分比高的热点代码片段,这便需要使用 perf record 记录单个函数级别的统计信息,并使用 perf report 来显示统计结果 ...

  5. 自定义ISO结构

    流程: 1.OS安装 1.1 网卡配置 1.2 密码 1.3 语言 1.4 时区 1.5 分区 1.6 rpms ... 2.软件安装 2.1 BIC Server 2.2 APP Server 2. ...

  6. C++之C/C++内存对齐

    一.什么是字节对齐,为什么要对齐 现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定类型变量的时候经常在特 定的内存地址访问,这 ...

  7. Entity Framework 5.0 Code First全面学习 (转)

    原文地址:感谢原文作者 http://blog.csdn.net/gentle_wolf/article/details/14004345 不贴图片了,太累. Code First 约定 借助 Cod ...

  8. 美化的select下拉框

    ie7浏览器以后的下拉框,给他加上边框样式,是没用的.要是想做出样式好看的下拉框需要用js或者jquery来模拟实现. 代码如下: <div class="r"> &l ...

  9. ireport报表制作, 当一个字段显示的数据太多时(数据过长),则需要自动换行

    1.当一个字段显示的数据太长,一个表格放不下,则需要自动换行,选中要更改的表格(要显示动态内容的字段),设置属性Stretch with overflow 为钩选状态. 未勾选之前: 勾选之后: 2. ...

  10. 强大的PHP一句话后门

    强悍的PHP一句话后门  这类后门让网站.服务器管理员很是头疼,经常要换着方法进行各种检测,而很多新出现的编写技术,用普通的检测方法是没法发现并处理的. 今天我们细数一些有意思的PHP一句话木马. 1 ...