hdu 1213 How Many Tables
来源:互联网 发布:nmt tensorflow 编辑:程序博客网 时间:2024/06/05 08:02
How Many Tables
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 31742 Accepted Submission(s): 15774
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.
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
25 31 22 34 55 12 5
Sample Output
24
给出许多朋友关系,是朋友就在一个桌子上,问要几张桌子,并查集处理完,再找出有几个根节点就可以了。
#include<iostream>#include<cstring>#include<cmath>#include<algorithm>#include<cstdio> using namespace std;int city,road;int pre[1111];int ans[1111];int find(int x){int r=x;while(r!=pre[r]){ r=pre[r];}int i=x,j; while(pre[i]!=r) { j=pre[i]; pre[i]=r; i=j; } return r;}void merge(int x,int y){ int fx=find(x); int fy=find(y); if(fx!=fy) pre[fy]=fx;}int main(){ int T; cin>>T; while(T--) { int i; cin>>city>>road; for(i=1;i<=city;i++) pre[i]=i; int c1,c2; for(i=1;i<=road;i++) { scanf("%d%d",&c1,&c2); merge(c1,c2); } int js=0; memset(ans,0,sizeof(ans)); for(i=1;i<=city;i++) ans[find(i)]=1; for(i=1;i<=city;i++) if(ans[i])js++; cout<<js<<endl; } return 0;}
阅读全文
0 0
- hdu 1213 how many tables
- hdu 1213 How Many Tables
- hdu 1213 How Many Tables
- HDU 1213 How Many Tables
- hdu 1213 How Many Tables
- HDU 1213 How Many Tables
- hdu 1213 How Many Tables
- Hdu 1213 How Many Tables
- hdu 1213 How Many Tables
- hdu 1213 How Many Tables
- HDU 1213 how many tables
- hdu 1213 How Many Tables
- hdu 1213how many tables
- hdu 1213 How Many Tables
- Hdu 1213 - How Many Tables
- HDU 1213 How Many Tables
- HDU 1213 How Many Tables
- hdu 1213 How Many Tables
- 一点拙见
- Arpa’s obvious problem and Mehrdad’s terrible solution
- hdu 4324 Triangle LOVE
- mysql 1055错误
- 通过修改系统文件添加一个可登陆用户
- hdu 1213 How Many Tables
- C/C++中的strncpy()函数
- hdu 1272 小希的迷宫
- TCP定时器 和 URG.PSH版本控制位 浅析
- Github如何提交修改的代码以及更新到最新版本
- hdu 2818 Building Block
- C++中栈区 堆区 常量区
- 如何从乱码中恢复 (下)
- linux 应用调试