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. P2258 子矩阵(dp)

    P2258 子矩阵 题目描述 给出如下定义: 子矩阵:从一个矩阵当中选取某些行和某些列交叉位置所组成的新矩阵(保持行与列的相对顺序)被称为原矩阵的一个子矩阵. 例如,下面左图中选取第2.4行和第2.4 ...

  2. Vue 页面回退参数被当作字符串处理

    当时情景是这样的,我从A页面跳到B页面时会传一个Boolean类型的参数,当B跳到C,再从C返回B的时候,控制台打印发现参数还在,可是判断怎么都不起作用,后来发现,当页面返回的时候,默认将参数变成了字 ...

  3. Sum It Up -- 深搜 ---较难

    每一行都是一组测试案例   第一个数字 表示总和 第二个数字表示 一共有几个可用数据  现在 按照从小到大的顺序   输出  那些数字中若干数字之和为总和的  信息 /. 很好很明显的  遍历痕迹 , ...

  4. Spring Boot (24) 使用Spring Cache集成Redis

    Spring 3.1引入了基于注解(annotation)的缓存(cache)技术,它本质不是一个具体的缓存实现方案,而是一个对缓存使用的抽象,通过在既有代码中添加少量它定义的个助攻annotatio ...

  5. MySQL关于视图的创建

    -- 视图就是一条select 语句 执行后返回结果集,是一种虚拟表,是一个逻辑表 -- 方便操作,减少复杂的SQL语句,增加可读性,更加安全一些 create view demo_view as s ...

  6. 如何下载 Nginx (windows 版本)并且简单的使用

    官网地址:http://nginx.org/ 进到官网 我这里下载的是 稳定版的 windows版本. 开始我们的简单测试 步骤一:找到nginx的压缩包,(随意找个地方)解压 步骤二:进入conf文 ...

  7. Node.js的运行

    nodejs的运行 首先要在你的电脑上下载node.js并安装,大家可以去官网下载 1.第一种方法:去官网下载git,安装好后,在桌面上单击鼠标右键,会有一个Git Bash Here,然后在新建一个 ...

  8. Android传递中文参数方法(之一)

    最近在做app,用的volley传参,有一个地方传中文参数不行(貌似是get方式),我又试了下post方式,成功了,记录下,以后有用! RequestQueue requestQueue = Voll ...

  9. Android的HttpUrlConnection类的GET和POST请求

    /** * get方法使用 */ private void httpGet() { new Thread() { @Override public void run() { //此处的LOGIN是请求 ...

  10. html5——3D案例(音乐盒子、百度钱包)

    1.音乐盒子:触碰盒子,盖子会打开 2.百度钱包:触碰钱包,钱包,会180度旋转 <!DOCTYPE html> <html lang="en"> < ...