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. [php]手动搭建php开发环境(排错)

    前提:针对自己的系统下载相应的php.apache.mysql,安装完毕后按照以下去配置httpd.conf和php.ini 本人用的是php5.6.4和apache2.4.4 一.Apache : ...

  2. LOW逼三人组(一)----冒泡算法

    排序 1.冒泡排序 冒泡算法 import random # 随机模块 def bubble_sort(li): ###################################冒泡排序#### ...

  3. docker使用现有容器生成新的镜像

    /*运行docker run后 --则进入该容器里了 我们做一些变更,比如安装一些东西 ,然后针对这个容器进行创建新的镜像 */ 基本形式: docker commit -m "change ...

  4. TinyOS 代码分析

    1.Basestation案例   位于/opt/tinyos-main-master/apps/Basetation 1.1本例的顶层结构图: 1.2软件实现流程 1) uartIn,uartOut ...

  5. EasyUi组合条件分页查询

    1.引入css与js文件 <link rel="stylesheet" type="text/css" href="themes/default ...

  6. webgote的例子 数据库与sql注入的相关联系(1)

    大家好我是时光凉春衫薄 之前将讲的sql注入有点随便了我同事也觉得有些地方看不懂,往后的几天我尽量写的细一点.尽可能让大家能看懂.(新手出道大佬多多指教.欢迎评论批评.) 数据库与sql注入的相关联系 ...

  7. tera term通过ttl脚本 自动连接服务器(转自http://www.cnblogs.com/wxb0328/p/teraterm.html)

    转自http://www.cnblogs.com/wxb0328/p/teraterm.html 在现在的这个公司一直使用tera term来远程连接服务器,感觉很方便,特别是它的ttl脚本配置的自动 ...

  8. 64_t2

    texlive-biblatex-chem-doc-svn42065-33.fc26.2.no..> 24-May-2017 15:44 1044190 texlive-biblatex-che ...

  9. spring(四)之基于注解(Annotation-based)的配置.md

    注解 这里讲的注解有下面几个 @Autowired @Qualifier(" ") @Genre(" ") @Offline @Resource(name=&q ...

  10. StringBuilder基本用法

    //StringBuilder用法 public class StringBuilderTest { public static void main(String[] args) { StringBu ...