HDU
来源:互联网 发布:淘宝嘻哈店铺 编辑:程序博客网 时间:2024/06/08 04:22
How Many Tables
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 34088 Accepted Submission(s): 17022
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 <cstdio>#define max 1010using namespace std;int pre[max];int find(int x){return pre[x]==x?x:pre[x]=find(pre[x]);}void join(int x,int y){x=find(x);y=find(y);if(x!=y)pre[x]=y;}int main(){int t;scanf("%d",&t);while(t--){int n,m,count=0;scanf("%d%d",&n,&m);int i;for(i=1;i<=n;i++){pre[i]=i;}while(m--){int a,b;scanf("%d%d",&a,&b);join(a,b);}for(i=1;i<=n;i++){if(pre[i]==i){count++;}}printf("%d\n",count);}}
阅读全文
0 0
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- hdu
- hdu
- HDU
- PAT (Basic Level) Practise (中文) 1057. 数零壹(20)
- 学习Java小结-2
- 貌不惊人,但味道一定惊艳你
- BCD码和十进制的相互转换
- ForkJoin框架(一):ForkJoin框架概述
- HDU
- 将数据通过php语法存入到表里面
- 虚拟机入门二,虚拟机的三种网络模式
- 津津的储蓄计划
- 字符串优化,与字符串的不可变,字符串对象重载的参数
- Linux开机自启动/服务注册的几种方式:
- spark源码之Job执行(1)stage划分与提交
- <转载>AS中导入GitHub开源项目SlidingMenu总结
- Memcached expire 设置错误引起的set(key,exp,value)为true而get(key)为null的问题