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>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<cmath>
#include<cstring>
using namespace std; int p[];
int find(int i)
{
if(p[i]==i)
return i;
else
return find(p[i]);
} int main()
{
int T,n,m,a,b,x,y;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
p[i]=i;
for(int i=;i<=m;i++)
{
scanf("%d%d",&a,&b);
x=find(a);
y=find(b);
if(x!=y)
p[x]=y;
}
int ans=;
for(int i=;i<=n;i++)
if(p[i]==i)
ans++;
printf("%d\n",ans);
}
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 (并查集,常规)

    并查集基本知识看: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. hdu 1213 求连通分量(并查集模板题)

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

  7. 【HDU1231】How Many Tables(并查集基础题)

    什么也不用说,并查集裸题,直接盲敲即可. #include <iostream> #include <cstring> #include <cstdlib> #in ...

  8. HDU 1213 How Many Tables(并查集模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意: 这个问题的一个重要规则是,如果我告诉你A知道B,B知道C,这意味着A,B,C知道对方,所以他们可以 ...

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

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

随机推荐

  1. hadoopMR自定义输入类型

    hadoop中的输入输出数据类型: BooleanWritable:标准布尔型数值 ByteWritable:单字节数值 DoubleWritable:双字节数值 FloatWritable:浮点数 ...

  2. vue methods computed watch区别

    一.methods和computed computed是计算属性,methods是方法. html: <p>Reversed message: "{{ reversedMessa ...

  3. nyoj-1015-二分图判定

    二部图 时间限制:1000 ms  |  内存限制:65535 KB 难度:1   描述 二部图又叫二分图,我们不是求它的二分图最大匹配,也不是完美匹配,也不是多重匹配,而是证明一个图是不是二部图.证 ...

  4. GIS入门基础知识点

    自从国企辞职以后,找了一份关于GIS开发的工作,好多从事这个都是地理信息科学方面的专业.由于自己才疏学浅,只能从头入门学起,先是大致了解公司的业务以及产品,学习一下相关地理信息的基础知识. 首先今天简 ...

  5. time_wait 和 close_wait

    tcp 四次握手状态图: 使用以下命令统计 tcp 连接信息: netstat -n |awk '/^tcp/ {++S[$NF]} END {for (a in S) print a, S[a]}' ...

  6. spring boot整合shiro后,部分注解(Cache缓存、Transaction事务等)失效的问题

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/elonpage/article/details/78965176 前言 整合有缓存.事务的sprin ...

  7. securecrt远程管理工具连接VM虚拟机

    对桥接,net,host_only网络不理解的,请点击:桥接,net,host_only的区别 我这里以net连接为例: 我们使用securecrt实现net的连接,前提是保证虚拟机的ip和虚拟网卡V ...

  8. laravel获取的数据转换为数组

    当构建 JSON API 时,您可能常常需要把模型和关联对象转换成数组或JSON.所以Eloquent里已经包含了这些方法.要把模型和已载入的关联对象转成数组,可以使用 toArray方法: $use ...

  9. day04_python_1124

    01 上周内容回顾 int bool str int < --- > str: i1 = 100    str(i1) s1 = '10'    int(s1)字符串必须是数字组成. in ...

  10. pyhton字符串

    a = 5 # 1 + 1 = 10 + 1 = 11 + 1 = 100 + 1 = 101print(a.bit_length()) # 计算一个数字的二进制长度. a = 10# print(t ...