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.

今天是伊格内修斯的生日。他邀请了很多朋友。现在是晚餐时间。伊格内修斯想知道他至少需要多少张桌子。你必须注意,并非所有的朋友都彼此认识,所有的朋友都不想和陌生人呆在一起。这个问题的一个重要原则是,如果我告诉你A知道B,B知道C,那就意味着A,B,C彼此了解,所以他们可以呆在一张桌子里。例如:如果我告诉你A知道B,B知道C,D知道E,那么A,B,C可以留在一张桌子上,而D,E必须留在另一张桌子上。所以Ignatius至少需要2张牌桌。

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.

输入以表示测试用例数的整数T(1 <= T <= 25)开始。然后是T测试用例。每个测试用例以两个整数N和M(1 <= N,M <= 1000)开始。N表示朋友的数量,朋友被标记从1到N.然后是M行。每行由两个整数A和B(A!= B)组成,这意味着朋友A和朋友B彼此了解。两种情况之间会有空白。

Output

For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.

对于每个测试用例,只需输出Ignatius至少需要的表格数量。不要打印任何空白。

Sample Input

2
5 3
1 2
2 3
4 5
 
5 1
2 5

Sample Output

2
4
解题思路:题目的意思就是求有多少个最大连通图,采用并查集的方法,将有关系的合并成一个集合,存放每个节点的father数组全部初始化为-1(默认为根节点)。father数组也可以初始化成自身值,因为每个最大的连通图中必有一个节点的根节点为自身值,所以也可以根据这点来查看有多少个最大连通图。
AC代码:
 #include<bits/stdc++.h>
using namespace std;
const int maxn=;
int t,n,m,a,b,father[maxn];
int find_father(int x){//找根节点
if(father[x]==-)return x;//如果根节点是-1,返回此时的节点编号
else return father[x]=find_father(father[x]);//递归查找根节点
}
void unite(int x,int y){
x=find_father(x);
y=find_father(y);
if(x!=y)father[x]=y;//如果不是同一个连通图,则可以合并
}
int main()
{
cin>>t;
while(t--){
memset(father,-,sizeof(father));//先默认-1为各节点的根节点,跟往常的不一样,因为此题求解是有多少个强连通图
cin>>n>>m;//n表示节点个数,m表示有m种情况
while(m--){
cin>>a>>b;
unite(a,b);//读入时顺便合并
}
int ans=;
for(int i=;i<=n;++i)
if(father[i]==-)++ans;//有多少个根节点是-1,就有多少个最大连通子图
cout<<ans<<endl;
}
return ;
}

题解报告:hdu 1213 How Many Tables的更多相关文章

  1. hdu 1213 How Many Tables(并查集算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 How Many Tables Time Limit: 2000/1000 MS (Java/O ...

  2. hdu 1213 How Many Tables 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 有关系(直接或间接均可)的人就坐在一张桌子,我们要统计的是最少需要的桌子数. 并查集的入门题,什 ...

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

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

  4. HDU 1213 - How Many Tables - [并查集模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...

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

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

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

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

  7. HDU - 1213 How Many Tables 【并查集】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意 给出N个人 M对关系 找出共有几对连通块 思路 并查集 AC代码 #include < ...

  8. HDU 1213 How Many Tables 并查集 水~

    http://acm.hdu.edu.cn/showproblem.php?pid=1213 果然是需要我陪跑T T,禽兽工作人员还不让,哼,但还是陪跑了~ 啊,还有呀,明天校运会终于不用去了~耶耶耶 ...

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

    传送门 Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Igna ...

随机推荐

  1. 使用js将Unix时间戳转换为普通时间

    var unixtime=1358932051;formatTime (time) { let unixtime = time let unixTimestamp = new Date(unixtim ...

  2. Microsoft 根证书计划弃用 SHA-1 哈希算法

    Microsoft 根证书计划弃用 SHA-1 哈希算法 微软官方2016年1月12日发布安全通报,自2016年1月1日起Microsoft 已经发布代码弃用变更,也就是说2016年1月1号后用SHA ...

  3. 【MangoDB分片】配置mongodb分片群集(sharding cluster)

    配置mongodb分片群集(sharding cluster) Sharding cluster介绍 这是一种可以水平扩展的模式,在数据量很大时特给力,实际大规模应用一般会采用这种架构去构建monod ...

  4. noip模拟赛 whzzt-Warmth

    分析:这道题难度和天天爱跑步差不了多少啊......裸的暴力只有10分,最好大的还是那个5%的数据,不过这也才15分,比天天爱跑步的暴力分不知道少到哪里去了. 正解是dp,毕竟要求方案数嘛,但是这个d ...

  5. (13)Corner Detection角点检测

    import cv2 import numpy as np img=cv2.imread('opencv-corner-detection-sample.jpg') gray = cv2.cvtCol ...

  6. Linux下汇编语言学习笔记35 ---

    这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...

  7. T1683 车厢重组 codevs

    http://codevs.cn/problem/1683/  时间限制: 1 s  空间限制: 1000 KB  题目等级 : 白银 Silver 题目描述 Description 在一个旧式的火车 ...

  8. 还原对于server失败 备份集中的数据库备份与现有数据库不同

    还原对于server失败 备份集中的数据库备份与现有数据库不同         今天在SQL Server 2008 R2中还原一个数据库备份,遇到错误.还原对于server失败 备份集中的数据库备份 ...

  9. 怎样对Android设备进行网络抓包

    问题描写叙述: 前段时间自己的app訪问server的url总是会出现间接性失败的问题,于是和server的同事开了个会.提出了他们server存在的这个bug,我的同事自然说自己的server没问题 ...

  10. The Breakpoint will not currently be hit. No executable code associated with this line

    首先.请确认solutin的属性 C/C++->General-> Debug Information Format 选择Program Database(/Zi) Linking-> ...