hdoj 1213 How Many Tables
How Many Tables
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16616 Accepted Submission(s):
8137
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.
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.
Ignatius needs at least. Do NOT print any blanks.
#include<stdio.h>
int set[2100];
int find(int fa) //找到根节点
{
int ch=fa;
int t;
while(fa!=set[fa])
fa=set[fa];
while(ch!=fa)
{
t=set[ch];
set[ch]=fa;
ch=t;
}
return fa;
}
int mix(int x,int y) //合并已有人数
{
int fx,fy;
fx=find(x);
fy=find(y);
if(fx!=fy)
set[fx]=fy;
}
int main()
{
int n,m,j,i,table,people,s;
scanf("%d",&n);
while(n--)
{
scanf("%d %d",&people,&table);
for(i=1;i<=people;i++)
set[i]=i;
for(i=1;i<=table;i++)
{
scanf("%d %d",&m,&j);
mix(m,j);
}
s=0;
for(i=1;i<=people;i++)
{
if(set[i]==i)
s++; //需要的桌子数
}
printf("%d\n",s);
}
return 0;
}
hdoj 1213 How Many Tables的更多相关文章
- Hdoj 1213.How Many Tables 题解
Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. ...
- hdu 1213 How Many Tables(并查集算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 How Many Tables Time Limit: 2000/1000 MS (Java/O ...
- HDOJ并查集题目 HDOJ 1213 HDOJ 1242
Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. ...
- HDU 1213 How Many Tables(模板——并查集)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday ...
- HDU 1213 - How Many Tables - [并查集模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...
- HDU 1213 How Many Tables(并查集)
传送门 Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Igna ...
- hdu 1213 How Many Tables 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 有关系(直接或间接均可)的人就坐在一张桌子,我们要统计的是最少需要的桌子数. 并查集的入门题,什 ...
- HDU 1213 How Many Tables (并查集)
How Many Tables 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/C Description Today is Ig ...
- 1213 How Many Tables(简单并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 简单并查集,统计单独成树的数量. 代码: #include <stdio.h> #i ...
随机推荐
- SQL四种语言:DDL,DML,DCL,TCL
1.DDL(Data Definition Language)数据库定义语言statements are used to define the database structure or schema ...
- Android 动态Tab分页效果
当前项目使用的是TabHost+Activity进行分页,目前要做个报表功能,需要在一个Tab页内进行Activity的切换.比方说我有4 个Tab页分别为Tab1,Tab2,Tab3,Tab4,现在 ...
- IIS会话过期的问题/WCF日志管理
http://technet.microsoft.com/zh-cn/library/cc725624(v=ws.10).aspx http://msdn.microsoft.com/zh-cn/li ...
- 解决git Push时请求username和password,而不是ssh-key验证
转载自:https://blog.lowstz.org/posts/2011/11/23/why-git-push-require-username-password-github/ 之前开始用git ...
- Linux下实现C++类的动态链接
1. 背景 在java中,jvm支持类的动态链接(Class.forName(String className)),用起来也很方便.动态链接是实现IOC(Inversion of Control,控制 ...
- IPC是什么意思?
IPC(Inter-Process Communication,进程间通信)IPC ( Instruction per Clock 及CPU每一时钟周期内所执行的指令多少) IPC代表了一款处理器的设 ...
- C#中的AssemblyInfo 程序集信息
[VS软件版本号定义.规则和相关的Visual Studio插件](http://blog.csdn.net/cnhk1225/article/details/37500593) [assembly: ...
- oralce索引和分区索引的使用
oracle分区表和分区索引的本质就是将数据分段存储,包括表和索引(索引从本质上来讲也是表),表分区会将表分成多个段分别存储.由此数据查询过程改变为先根据查询条件定位分区表,然后从该表中查询数据,从而 ...
- WP8触摸感应Manipulation的操作
触控感应不同事件的处理: 可将以下三个事件,绑定到一个控件中. /// <summary> /// 触摸开始事件 /// </summary> /// <param na ...
- SpringContextHolder 静态持有SpringContext的引用(如何取得Spring管理的bean )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...