How Many Tables hd 1213
来源:互联网 发布:java 自然语言 编辑:程序博客网 时间:2024/05/29 16:44
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
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
#include<stdio.h>const int Max=1000+5;int f[Max];int find(int x){if(x==f[x])return x;return f[x]=find(f[x]);}int bing(int a,int b){a=find(a);b=find(b);if(a!=b)f[a]=b;}int main(){int n,a,b,c,d,sum,i;scanf("%d",&n);while(n--){scanf("%d%d",&a,&b);for(i=1;i<=a;i++)f[i]=i;for(i=1;i<=b;i++){scanf("%d%d",&c,&d);bing(c,d);}sum=0;for(i=1;i<=a;i++){if(f[i]==i)sum++;}printf("%d\n",sum);}return 0;}
0 0
- How Many Tables hd 1213
- hd 1213 How Many Tables
- 【HD 1213】How Many Tables
- HD 1213 How Many Tables 【并查集】
- HD 1213 How Many Tables(裸 并查集)
- HD-1213-How Many Tables(并查集)
- 1408012025-hd-How Many Tables.cpp
- How Many Tables 1213
- 1213 How Many Tables
- 1213:How Many Tables
- hdu 1213 how many tables
- hdu 1213 How Many Tables
- HDOJ 1213 How Many Tables
- hdu 1213 How Many Tables
- HDU 1213 How Many Tables
- hdu 1213 How Many Tables
- hdoj 1213 How Many Tables
- HDU 1213 How Many Tables
- 理解Linux文件权限
- SOCKET连接与TCP/IP连接
- getElementByName????????,????????,
- 51nod1080——两个数的平方和
- Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Lig
- How Many Tables hd 1213
- python中将中文汉字转换为英文首字母的代码及遇到的小问题
- 利用数据库表转置将多条记录合并成一条记录
- UCOSIII简介于移植
- 使用正则表达式获取url中的中文参数值出现乱码问题
- DeepLearning tutorial(1)Softmax回归原理简介+代码详解
- poj 2135 Farm Tour(最小费用最大流)
- 【项目管理和构建】eclipse配置maven + 创建maven项目(三)
- HDOJ 5807 Keep In Touch