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. 【已解决】Makefile执行过程中出错:make: *** No rule to make target ` ‘, needed by xxx. Stop(转载)

    转自: http://www.crifan.com/makefile_error_make_no_rule_to_make_target_needed_by_stop/ [问题] 有个已有的Makef ...

  2. 学学cookie和session

    什么是Cookie? HTTP Cookie(也叫 Web Cookie或浏览器 Cookie)是服务器发送到用户浏览器并保存在本地的一小块数据,它会在浏览器下次向同一服务器发起请求时被携带并发送到服 ...

  3. PCB OD工具破解实例应用

    以下破解Genesis为例,对OD工具使用进行实例讲解 工具简单 介绍下下载地址: OD工具:是一个新的动态追踪工具,将IDA与SoftICE结合起来的思想,Ring 3级调试器, 是为当今最为流行的 ...

  4. go 学习成长之路

    一.go的搭建 二.初识go 三.混个脸熟--go 四.go的语言结构 五.go的常量与变量 六.go基础数据类型 七.go 条件语句 八.go 运算符 九.go条件语句switch 十.go循环语句 ...

  5. JavaScript--关闭窗口(window.close)

    close()关闭窗口 用法: window.close(); //关闭本窗口 或 <窗口对象>.close(); //关闭指定的窗口 例如:关闭新建的窗口. <script typ ...

  6. 快速搭建Hadoop及HBase分布式环境

    本文旨在快速搭建一套Hadoop及HBase的分布式环境,自己测试玩玩的话ok,如果真的要搭一套集群建议还是参考下ambari吧,目前正在摸索该项目中.下面先来看看怎么快速搭建一套分布式环境. 准备 ...

  7. C# 多线程系列(六)

    同步 当多个线程共享一些数据的时候,我们就需要使用同步技术,确保一次只有一个线程访问合改变共享状态.注意,同步问题与争用和死锁有关. 例: ; static void Add() { ; i < ...

  8. [ BZOJ 4318 & 3450 / CodeForces 235 B ] OSU!

    \(\\\) \(Description\) 一共进行\(N\)次操作,生成一个长度为\(N\)的\(01\)序列,成功对应\(1\),失败对应\(0\),已知每一次操作的成功率\(p_i\). 在这 ...

  9. 深拷贝js

    JSON 最简单的一个 let obj = {} let newObj = JSON.parse(JSON.stringify(obj)) 弊端:这种方法无法拷贝function函数,undefine ...

  10. 【转】Centos7 ftp 配置及报错处理

    原文链接: https://www.cnblogs.com/GaZeon/p/5393853.html Centos7网络配置,vsftpd安装及530报错解决 今天在虚拟机安装CentOS7,准备全 ...