How Many Tables

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11038    Accepted Submission(s): 5447

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.

 
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
 
Author
Ignatius.L
 
Source
 
 #include <stdio.h>
int pre[];
int find(int x)
{
int i,r,t;
t=r=x;
while(r!=pre[r])
{
r=pre[r];
}
while(t!=r)
{
i=pre[t];
pre[t]=r;
t=i;
}
return r;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int m,n,i,j,a,b,pa,pb,total;
scanf("%d %d",&n,&m);
total=n;
for(i=;i<=n;i++)
pre[i]=i;
for(i=;i<m;i++)
{
scanf("%d %d",&a,&b);
pa=find(a);
pb=find(b);
if(pa!=pb)
{
pre[pb]=pa;
total--;
}
}
printf("%d\n",total);
}
return ;
}

//并查集简单应用

hdu_1213_How Many Tables_201403091126的更多相关文章

随机推荐

  1. Android 数据库

    官方文档:https://developer.android.com/training/basics/data-storage/databases.html#WriteDbRow 原帖:http:// ...

  2. D. Toy Sum(cf)

    http://codeforces.com/problemset/problem/405/D 题意:已知集合S={1,2,3......1000000},s=1000000,从集合S中选择n个数,X= ...

  3. [Apple开发者帐户帮助]九、参考(6)支持的功能(watchOS)

    watchOS扩展可用的功能取决于您的程序成员身份. 注意:对于watchOS应用程序目标,可用的功能是应用程序组和后台模式,并且不依赖于您的程序成员身份. 能力 ADP 企业 Apple开发者 应用 ...

  4. 拼接sql ()

    SELECT ID,FORMNAME,NODENAME,SEQUENCE, NAME, STATE, NOWTIMES,      WORK.FQREALNAME||'('||FQDEPT.FULLN ...

  5. nginx入门学习

    1.yum解决编译nginx所需的依赖包,之后你的nginx就不会报错了 yum install gcc patch libffi-devel python-devel zlib-devel bzip ...

  6. Struts2 之 实现文件上传(多文件)和下载

    Struts2  之 实现文件上传和下载 必须要引入的jar commons-fileupload-1.3.1.jar commons-io-2.2.jar 01.文件上传需要分别在struts.xm ...

  7. html5——应用缓存

    基本概念 1.HTML5中我们可以轻松的构建一个离线(无网络状态)应用,只需要创建一个cache manifest文件 2.可配置需要缓存的资源,网络无连接应用仍可用,本地读取缓存资源,提升访问速度, ...

  8. Java_Web三大框架之Hibernate+jsp+selvect+HQL登入验证

    刚开始接触Hibernate有些举手无措,觉得配置信息太多.经过一个星期的适应,Hibernate比sql简单方便多了.下面做一下Hibernate+jsp+selvect+HQL登入验证. 第一步: ...

  9. 解决springmvc返回json中文乱码

    在pringmvc中通过设置@ResponseBody返回json乱码问题,这个问题上网找了很久,发现答案真是人云亦云,奉上我的解决方案: 解决方案一:需要导入 jackson-core-asl-1. ...

  10. netperf使用指南

    1. 介绍: Netperf是由惠普公司开发的,测试网络栈.即测试不同类型的网络性能的benchmark工具,大多数网络类型TCP/UPD端对端的性能,得到网络上不同类型流量的性能参数.Netperf ...