
Hdu 1213-How Many Tables
解题思路: 本题考察了并查集的相关操作,不多赘述,上代码!
发布日期:2021-05-07 03:02:10
浏览次数:27
分类:精选文章
本文共 2104 字,大约阅读时间需要 7 分钟。
题目链接:
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 55 1
2 5Sample Output:
2 4中文版本:
题目描述: 今天是Ignatius的生日,他邀请了许多朋友。现在是吃晚饭的时候,Ignatius想知道他至少需要多少张桌子。你要注意,并不是所有的朋友都互相认识,以及所有的朋友不想和陌生人呆在一起。这个问题的一个重要原则是如果我告诉你A认识B,并且B认识C,那意味着A、B、C都相互认识,所以他们能呆在一张桌上。 例如:如果我告诉你A认识B,B认识C,以及D认识E,所以A、B、C可以呆在一张桌子上,D、E必须呆在另一张桌子上,所以lgnatius至少需要2张桌子。输入:
输入从一个整数T(1≤T≤25)开始,它表明了测试样例的数量。接着是T个测试样例。每个测试样例从两个整数N和M(1≤N、M≤1000)开始。N表示朋友的数量,朋友从1到N编号。后面有M行,每一行由两个整数A和B组成(A!=B),那意味着朋友A和朋友B之间互相认识,两个组测试样例之间会有一个空白行。输出:
对于每个测试样例,只需要输出Ignatius至少需要多少张桌子,不要打印任何空格。样例输入:
2 5 3 1 2 2 3 4 55 1
2 5样例输出:
2 4样例示意图:


参考代码:
#includeint t,n,m;int parent[1001];void init(){ int i; for(i=1;i<=n;i++) parent[i]=i;}int find(int x){ if(x==parent[x]) return x; else return parent[x]=find(parent[x]); }void Union(int a,int b){ int A=find(a); int B=find(b); if(A!=B) parent[A]=B;}int main(){ int i,a,b,count; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); init(); count=0; for(i=0;i
发表评论
最新留言
感谢大佬
[***.8.128.20]2025年04月10日 11时40分48秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
用ThreadLocal来优化下代码吧
2019-03-06
netcore中使用session
2019-03-06
Android 开发学习进程0.25 自定义控件
2019-03-06
多媒体文件格式全解说(下)--图片
2019-03-06
淘宝WAP版小BUG分析
2019-03-06
NodeJS+Express+MongoDB
2019-03-06
(四十四)c#Winform自定义控件-水波-HZHControls
2019-03-06
c#winform主题实现的一个方法
2019-03-06
asp.net打印网页后自动关闭网页【无需插件】
2019-03-06
一个人开发的html整站源码分享网站就这么上线了
2019-03-06
SQLServer 查看耗时较多的SQL语句(转)
2019-03-06
【计算机网络】应用层
2019-03-06
【Maven】POM基本概念
2019-03-06
【Java思考】Java 中的实参与形参之间的传递到底是值传递还是引用传递呢?
2019-03-06
【设计模式】单例模式
2019-03-06
【SpringCloud】Hystrix熔断器
2019-03-06
【Linux】2.3 Linux目录结构
2019-03-06
java.util.Optional学习笔记
2019-03-06
远程触发Jenkins的Pipeline任务的并发问题处理
2019-03-06
jackson学习之七:常用Field注解
2019-03-06