题解报告:hdu 1213 How Many Tables
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
Sample Output
#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的更多相关文章
- hdu 1213 How Many Tables(并查集算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 How Many Tables Time Limit: 2000/1000 MS (Java/O ...
- hdu 1213 How Many Tables 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 有关系(直接或间接均可)的人就坐在一张桌子,我们要统计的是最少需要的桌子数. 并查集的入门题,什 ...
- HDU 1213 How Many Tables(模板——并查集)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday ...
- HDU 1213 - How Many Tables - [并查集模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...
- HDU 1213 How Many Tables (并查集)
How Many Tables 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/C Description Today is Ig ...
- HDU 1213 How Many Tables(并查集模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意: 这个问题的一个重要规则是,如果我告诉你A知道B,B知道C,这意味着A,B,C知道对方,所以他们可以 ...
- HDU - 1213 How Many Tables 【并查集】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意 给出N个人 M对关系 找出共有几对连通块 思路 并查集 AC代码 #include < ...
- HDU 1213 How Many Tables 并查集 水~
http://acm.hdu.edu.cn/showproblem.php?pid=1213 果然是需要我陪跑T T,禽兽工作人员还不让,哼,但还是陪跑了~ 啊,还有呀,明天校运会终于不用去了~耶耶耶 ...
- HDU 1213 How Many Tables(并查集)
传送门 Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Igna ...
随机推荐
- 将登录代码模块化,然后用add address接口来调用它,success!
登录模块,单独写成mylogin()方法,保存为modeltry119.py,然后接口的文件只写接口, 在post请求时,引用mylogin()返回的cookies就好了. requests.post ...
- python爬虫27 | 当Python遇到MongoDB的时候,存储av女优的数据变得如此顺滑爽~
上次 我们知道了怎么操作 MySQL 数据库 python爬虫26 | 把数据爬取下来之后就存储到你的MySQL数据库. MySQL 有些年头了 开源又成熟又牛逼 所以现在很多企业都在使用 MySQL ...
- 在此计算机中仅有部分visual studio2010产品已升级到SP1,只有全部升级,产品才能正常运行
先说废话: 本人机子刚装系统Win10 专业版 1709 开始安装vs2010的时候中途报错了,有一个什么驱动不兼容,被我给关闭了,继续安装完,然后找不到vs的启动快捷方式,开始里面没有,于是我开始修 ...
- Windows中更新python模块的命令
最近写爬虫,突然发现自己的动态的User-Agent用不了了,所以想可能是新版本出来了,旧的版本用不了了,坏掉了. 一时间想不起用什么命令了,网上查了一下,发现很简单,所以记录一下方便以后忘了的时候快 ...
- 20180705关于mysql binlog的解析方式
来自:https://blog.csdn.net/u012985132/article/details/74964366/ 关系型数据库和Hadoop生态的沟通越来越密集,时效要求也越来越高.本篇就来 ...
- MySQL Workbench查看和修改表字段的Comment值
查看: 选择单个表->[右键]->[Table Inspector] 再选择Columns选项卡即可,把表格拉倒最后一列. 编辑: 选择单个表->[右键]->[Alter Ta ...
- SecureCRT 8.0公布
百度搜索到的7.3 注冊码生成器还是能够用于8.0的破解. 破解时,选择手动输入(Enter Licence Manually)产生的代码. 添加了一些特性,我最看重的是: 1. 能够在以下命令窗体 ...
- 每日五题(jsp)
1.forward 和 redirect 的差别 答: 1.从地址栏显示来说 forward是server请求资源,server直接訪问目标地址的URL,把那个URL的响应内容读取过来,然后把这些内容 ...
- Linux中W与Who命令的使用
踢掉一个从某个终端连上的用户pkill -kill -t pts/0 ---------------------------------------------------------------- ...
- update city_demo set city=(select city from city order by rand() limit1);
update city_demo set city=(select city from city order by rand() limit1); 因为使用了rand()函数,所以每一次查询的结果是不 ...