hdu 1213 How Many Tables(并查集)
2016-05-17 15:36
501 查看
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.
[align=left]Sample Input[/align]
2
5 3
1 2
2 3
4 5
5 1
2 5
[align=left]Sample Output[/align]
2
4
//模板题诶
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.
[align=left]Sample Input[/align]
2
5 3
1 2
2 3
4 5
5 1
2 5
[align=left]Sample Output[/align]
2
4
//模板题诶
#include <iostream> #include <cstring> using namespace std; int data[1005]; bool a[1005]; int _find(int x)//查找头头 { int r=data[x]; while(r!=data[r]) r=data[r]; int i=x,j; while(data[i]!=r)//路径压缩 { j=data[i]; data[i]=r; i=j; } return r; } void mix(int x,int y) { int fx=_find(x),fy=_find(y); if(fx!=fy) data[fx]=fy; } int main() { int t,n,m,ans; cin>>t; while(t--) { cin>>n>>m; ans=0; for(int i=1;i<=n;i++) data[i]=i; for(int i=1;i<=m;i++) { int a,b; cin>>a>>b; mix(a,b); } memset(a,false,sizeof(a)); for(int i=1;i<=n;i++) a[_find(i)]=true; for(int i=1;i<=n;i++) if(a[i]==true) ans++; cout<<ans<<endl; } return 0; }
相关文章推荐
- 每日一更提醒
- Echart多图联动
- C++实现CString和string的互相转换
- 第10,11周 项目三(1)
- 链表有环找入口的问题
- ping不是内部或外部命令
- Shell 脚本中中括号“[]”判断问题
- 一些常用的正则表达式及一些扩展
- JAVA正则表达式:Pattern类与Matcher类详解(转)
- leetcode.335. Self Crossing
- Android Notification 样式!
- php 数学函数
- 23种设计模式(5)--prototype模式
- 160517、nginx负载均衡详解
- ps 实例部分
- Android 基本控件及表单三大控件,事件处理
- [置顶] java web开发(一) 环境搭建
- iOS RunTime
- 桌面右键菜单显示左右侧
- 项目中经验总结,问题记录和后期改善