HDU 1213 How Many Tables -- 并查集
来源:互联网 发布:java自动包装机制 编辑:程序博客网 时间:2024/06/06 17:36
How Many Tables
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 22520 Accepted Submission(s): 11206
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 <stdio.h>#include <string.h>#include <algorithm>const int N = 1005;using namespace std;int tree[N];int findroot(int x){if(tree[x]==-1) return x;else{int tmp=findroot(tree[x]); tree[x]=tmp;return tmp;}}int main(){int n,m,i,j;int t;scanf("%d",&t);while(t--){scanf("%d %d",&n,&m);for(i=1;i<=n;i++){tree[i]=-1;}int x,y;while(m--){scanf("%d %d",&x,&y);x=findroot(x);y=findroot(y);if(x!=y){tree[y]=x;}}int sum=0;for(i=1;i<=n;i++){if(tree[i]==-1)sum++;}printf("%d\n",sum);}return 0;}
0 0
- 并查集 hdu 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(并查集练习)
- HDU 1213 How Many Tables 并查集入门
- HDU 1213 How Many Tables 并查集 水~
- HDU 1213 How Many Tables(并查集)
- [ACM] hdu 1213 How Many Tables(并查集)
- [ACM] 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(并查集)
- 什么是需求分析
- 从源码层理解Hashtable中的put和get
- csdn博客如何添加图片
- asp.net创建XML文件的方法小结
- Android Studio使用
- HDU 1213 How Many Tables -- 并查集
- iOS之UI学习-UITextField代理篇
- Activity要点总结
- Mac下通过Homebrew安装MySQL
- untiy 3d ShaderLab_第7章_ Forward渲染路径_4_Forward渲染路径总结
- Qt QPainter::end: Painter ended whith 2 saced states
- 关于字符串截取收藏
- 【CDMI-PROJECT】DAILY SUMMURY
- (Leetcode)22. Generate Parentheses