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.

InputThe 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.

OutputFor 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>
#include <queue>
using namespace std;
int n , m ;
int f[] ;
void init()
{
for(int i = ; i <= n ; i++)
{
f[i] = i ;
}
}
int getf(int x)
{
if(f[x] != x)f[x]=getf(f[x]);
return f[x] ;
}
int merge(int x,int y)
{
f[getf(y)]=getf(x);
}
int main()
{
int T,x,y,c;
scanf("%d" , &T);
while(T--)
{
c=;
scanf("%d%d",&n,&m);
init();
for(int i = ; i <= m ; i++)
{
scanf("%d %d",&x,&y);
merge(x,y);
}
for(int i = ; i <= n ; i++)
if(f[i]==i)c++;
printf("%d\n",c);
}
}

How Many Tables 简单并查集的更多相关文章

  1. 1213 How Many Tables(简单并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 简单并查集,统计单独成树的数量. 代码: #include <stdio.h> #i ...

  2. POJ 2524 (简单并查集) Ubiquitous Religions

    题意:有编号为1到n的学生,然后有m组调查,每组调查中有a和b,表示该两个学生有同样的宗教信仰,问最多有多少种不同的宗教信仰 简单并查集 //#define LOCAL #include <io ...

  3. poj1611 简单并查集

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 32781   Accepted: 15902 De ...

  4. 【简单并查集】Farm Irrigation

    Farm Irrigation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Tot ...

  5. ACM_“打老虎”的背后(简单并查集)

    “打老虎”的背后 Time Limit: 2000/1000ms (Java/Others) Problem Description: “习大大”自担任国家主席以来大力反腐倡廉,各地打击贪腐力度也逐步 ...

  6. 并查集:HDU1213-How Many Tables(并查集最简单的应用)

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

  7. HDU——1213How Many Tables(并查集按秩合并)

    J - How Many Tables Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  8. 杭电ACM省赛集训队选拔赛之热身赛-How Many Tables,并查集模板题~~

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

  9. HDU 1213 How Many Tables (并查集)

    How Many Tables 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/C Description Today is Ig ...

随机推荐

  1. Codeforces 595D - Max and Bike

    595D - Max and Bike 思路:开始和结束时的计时器的高度相同时(也就是关于圆竖着直径对称)时间最少. 证明: 总距离为d. 圆周长为s=2*π*r. 设len=d-floor(d/s) ...

  2. Android ListView常见配置说明

    ListView是我们经常使用的控件,但是使用中却因为各种原因无法设置出我们需要的效果,现将常用的设置记录下来方便以后查询. 1.拖动时背景变黑 android:cacheColorHint=&quo ...

  3. [转]UTF-8网页中的头部部分多出一行空白

    最近做php开发,我自己的习惯都是用utf-8编码,有时候却在网页顶部多了一个空白行,甚至引起了式样的错乱,后来google到这么一篇文 章,彻底解决问题UTF-8的BOM问题通常情况下,使用Wind ...

  4. 利用Py-Socket模块做的一个不登陆windows服务器自动实现替换或者调用自动拨号功能

    xu言: 最近,有个朋友让我帮忙“搞点事情”,然后正好在学习socket模块,这个模块666啊,基本上可以实现远程服务器cmd shell的大部分功能.好,话不多说,直接上码~ 由于很多电信运营商都会 ...

  5. 3-4 8精彩算法集合。struct(C,ruby) Ruyb类对象和结构体, 3-5

    在本章我遇到了c语言的struct数据,即自定义的数据结构.比如: struct edge { int u; int v; int w; }; 题目给了一组数据,用edge储存.需要按照w大小排序.我 ...

  6. 生成图片验证码(.NET)

    一.生成随机字符串 方法一: public string CreateRandomCode(int codeCount) { string allChar = "0,1,2,3,4,5,6, ...

  7. zuul网关源码解析

    zuul网关源码解析 zuul请求的生命周期 ZuulServlet ZuulServlet定义了对zuul整个过程的处理,如下: public void service(javax.servlet. ...

  8. Windows系统配置Python环境,python2和python3共存

      Windows系统配置python2和python3共存   1.下载python: https://www.python.org/downloads/ 注:选择需要的版本(python2 or ...

  9. xrat CC特征

    ["6", 11818669.0, 8326.0, 599.75, 19705.992496873696, 13.882451021258857, 0.07203338938265 ...

  10. CSS3动画和JS动画的比较

    前言 之前有被问到一个问题,css3动画和js动画性能谁更好,为什么.据我的经验,当然觉得css3动画性能更好,至于为什么一时还真答不上来,所以特意查了一下资料总结一波. JS动画 优点: js动画控 ...