# How Many Tables

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

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

``` #include <iostream>
#include <cstdio>
using namespace std;
int father[];

void set(int n)
{
for (int i=; i<=n; i++)
father[i]=i;
}

int find(int a)
{
if (father[a]==a)
return a;
return father[a]=find(father[a]);
}

void Union(int x,int y)
{
x=find(x);
y=find(y);
if (x!=y)
father[x]=y;
}

int main ()
{
int t,k;
while (cin>>t)
{

while (t--)
{
k=;
int n,m;
cin>>n>>m;
set(n);
while (m--)
{
int s,d;
cin>>s>>d;
Union(s,d);
}
for (int i=; i<=n; i++)
{
if (father[i]==i)
k++;
}
printf ("%d\n",k);
}
}
return ;
}```

## hdu 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 (并查集，常规)

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

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

6. hdu 1232 畅通工程（并查集算法）

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1232 畅通工程 Time Limit: 4000/2000 MS (Java/Others)    M ...

7. hdu 1213 求连通分量（并查集模板题）

求连通分量 Sample Input2 //T5 3 //n m1 2// u v2 34 5 5 12 5 Sample Output24 # include <iostream> # ...

8. HDU 1213 How Many Tables（模板——并查集）

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

9. Union-Find 并查集算法

一.动态连通性(Dynamic Connectivity) Union-Find 算法(中文称并查集算法)是解决动态连通性(Dynamic Conectivity)问题的一种算法.动态连通性是计算机图 ...

10. 并查集算法Union-Find的思想、实现以及应用

并查集算法,也叫Union-Find算法,主要用于解决图论中的动态连通性问题. Union-Find算法类 这里直接给出并查集算法类UnionFind.class,如下: /** * Union-Fi ...

## 随机推荐

1. PAT A 1030. Travel Plan (30)【最短路径】

https://www.patest.cn/contests/pat-a-practise/1030 找最短路,如果有多条找最小消耗的,相当于找两次最短路,可以直接dfs,数据小不会超时. #incl ...

2. Map遍历两种方式

Java代码 Map<String,String> map=new HashMap<String,String>(); map.put("username" ...

3. Android 源码下载

一直想尝试android源码的编译,这两天正好海思代码的编译也需要ubuntu环境,于是安装了ubuntu 12.04,安装时选了语言为中文,因此下面很多状态及错误报告都是中文了,另外分配了4G sw ...

4. css3百叶窗轮播图效果

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

5. Ribbon2: 创建动态的Ribbon库

6. 部署lamp

7. python filter map reduce

filter(function, iterable): Construct a list from those elements of iterable for which function retu ...

8. jenkins 启动slave时，找不到合适的java程序

今天添加slave 又不成功,我用右键,发现随意点了java的那个打开,结果刚弹出来就关闭了窗口,显示未启动成功. 真是小白的想法,不过跟工具打交道久了,多少逻辑上想一下不对. 一定是用了不合适的程序 ...

9. 【BZOJ1083】[SCOI2005]繁忙的都市（最小生成树）

[BZOJ1083][SCOI2005]繁忙的都市(最小生成树) 题面 BZOJ 洛谷 题解 模板题. #include<iostream> #include<cstdio> ...

10. SSD 固态硬盘，Trim指令 ，查看状态、开启、关闭

一说到SSD 固态硬盘,经常会看到Trim指令这个名词,那什么是Trim?   Trim是什么? 为了解决硬盘降速的问题,微软联合各大SSD厂商开发了一个新技术——Trim.Trim指令也叫disab ...