HDU 1213 How Many Tables( 并查集)
2015-09-28 11:18
274 查看
How Many Tables
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 19270 Accepted Submission(s): 9562
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
Author
Ignatius.L
这道题是并查集问题,过几天写一个并查集的专题,这次就先把代码附上,以供参考
附上代码:
#include <iostream> #include <algorithm> #include <cstring> #include <string> #include <cstdio> #include <queue> #define MAX_N 5000 using namespace std; int ans; int par[MAX_N]; // 父亲 int Rank[MAX_N]; // 树的高度 void init(int n) // 初始化n个元素 { for(int i = 0;i < n;i++) { par[i] = i; // 初始化 将 n 个数的根都定位自己,形成 n 个数的森林 Rank[i] = 0; // 初始化 每棵树的深度为 0 } } int find(int x) // 查询根节点 { if(par[x] == x) // 寻找到根节点,返回根节点的值 return x; else return par[x] = find(par[x]); // 如果该节点不为根节点,就递归求其根节点 } void unite(int x,int y) // 合并 x 和 y 所属的集合 (哪个树高 则将 低树合并到高树上面,保持树的深度最优化,便于查找) { x = find(x); y = find(y); if(x == y) // 如果原本就属于同一个根,那说明在一个集合中,就不用合并了 return; if(Rank[x] < Rank[y]) // 如果 y 的树的高度比 x 的树的高度高的 par[x] = y; // 将 x 合并到 y 上面,以 y 为根 else par[y] = x; // 将 y 合并到 x 上面,以 x 为根 if(Rank[x] == Rank[y]) Rank[x]++; // 如果要合并的两颗树的长度相等,则树的高度 +1 } bool same(int x,int y) // 判断 x 和 y 是否属于同一个集合内! { return find(x) == find(y); // 属于统一集合返回值为 真,不属于返回值为 假 ! ( 既是 find(x) == find(y) 为真还是为假 ) } int main() { int n,m,t; cin >> t; while(t--) { scanf("%d%d",&n,&m); memset(par,0,sizeof(par)); // 对父节点进行清除 ans = n; // 初始化根的数目 init(n); for(int i = 0;i < m;i++) { int a,b; scanf("%d%d",&a,&b); if(same(a,b)) // 判断两个是否属于同一集合内 continue; else // 如果两个点原本不属于同一集合内,则将其合并 { unite(a,b); ans--; // 每两个树合并,树的数量少 1 } } cout << ans << endl; // 最后还有 ans 棵树,则需要 ans 张桌子就行了 } return 0; }
相关文章推荐
- iOS开发之UITableViewCell的官方文档库
- spark文本处理-文章分类
- 数据库SQL优化总结 学习必看
- OC中ARC forbids explicit message send of release错误(转)
- mybites 大于 小于符号的情况
- 三分钟教你学Git(十九) - 全局,用户,仓库作用域的配置文件
- Swift - 添加、修改、删除通讯录联系人
- Swift - 访问通讯录联系人(使用纯代码实现)
- XCode工程中ARC模式与非ARC模式共用(转)
- iOS 中的 xml 解析
- springmvc 上传文件
- 如何使用Mediawiki搭建个人wiki系统、以及wiki的部分基本操作和设置
- alloc与allocWithZone的用法区别
- JAVA实现json和对象间的相互转换,支持复杂对象格式
- Swift - 访问通讯录联系人(使用系统提供的通讯录交互界面)
- 8月~9月公有云故障事件
- 爸爸儿子和狗狗
- 渗透学习笔记--基础篇--sql注入(字符型)
- 深入理解Git(七) - 神秘的数据恢复
- Android HTTP Content-type( Mime-Type)对照表