How Many Tables

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

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
 
 
 
提示:不用管他输入的格式!
  这个还是查找树的个数!父节点等于本身的点是根节点!
  
 
 
 
 #include<stdio.h>
#include<string.h>
#include<algorithm>
#define maxn 10000
using namespace std;
int a;
int per[maxn];
void init()
{
int i;
for(i=;i<=a;i++)
per[i]=i;//初始化数组
}
int find(int x)//查找根节点
{
int t=x;
while(t!=per[t])
t=per[t];
per[x]=t;//缩短路径
return t;
}
void join(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
per[fx]=fy;
else
return ;
} int main()
{
int b,n,c,d;
scanf("%d",&n);
while(n--)
{ int sum=,i;
scanf("%d%d",&a,&b);
init();
while(b--)
{
scanf("%d%d",&c,&d);
join(c,d);
}
for(i=;i<=a;i++)
{
if(per[i]==i)
sum++;//查找根节点的个数 }
printf("%d\n",sum);
}
return ;
}

How Many Tables--hdu1213(并查集)的更多相关文章

  1. HDU1213:How Many Tables(并查集)

    How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  2. HDU1213 How Many Tables (并查集)

    题目大意: 有一个人要过生日了,请他的朋友来吃饭,但是他的朋友互相认识的才能坐在一起,朋友的编号从1 ~ n,输入的两个数代表着这两个人互相认识(如果1和2认识,2和3认识,那么1和3也就认识了).问 ...

  3. HDU1213:How Many Tables(并查集入门)

    -----------刷点水题练习java------------- 题意:给定N点,M边的无向图,问有多少个连通块. 思路:可以搜索;  可以并查集.这里用并查集练习java的数组使用,ans=N, ...

  4. HDU 1213 How Many Tables (并查集)

    How Many Tables 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/C Description Today is Ig ...

  5. hdu 1213 How Many Tables(并查集练习)

    题目链接:hdu1213 赤裸裸的并查集.....水题一个.... #include<stdio.h> #include<string.h> #include<algor ...

  6. HDU--1213并查集

    题目传送门:HDU--1213 //题意:ignatius过生日,客人来到,他想知道他需要准备多少张桌子.然而一张桌子上面只能坐上相互熟悉的人, //其中熟悉可定义成为A与B认识,B与C认识,我们就说 ...

  7. 并查集:HDU1213-How Many Tables(并查集最简单的应用)

    How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...

  8. HDU——1213How Many Tables(并查集按秩合并)

    J - How Many Tables Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  9. 杭电 1213 How Many Tables (并查集求团体数)

    Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius ...

  10. 杭电ACM省赛集训队选拔赛之热身赛-How Many Tables,并查集模板题~~

    How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

随机推荐

  1. HIBERNATE - 符合Java习惯的关系数据库持久化(精华篇)

    HIBERNATE - 符合Java习惯的关系数据库持久化      下一页 HIBERNATE - 符合Java习惯的关系数据库持久化 Hibernate参考文档 3.0.4   目录 前言 1. ...

  2. centos7.0 php-fpm 安装ImageMagic php扩展imagick

    Just copy and paste: $ yum install make $ yum install gcc $ yum install php php-fpm php-devel php-pe ...

  3. shell练习--批量创建账号

    #!/bin/bash #By spinestars #-- #cksum5位数获取方法,可能有重复 #pd="user`head -200 /dev/urandom | cksum | h ...

  4. IOS开发UI篇-NavigationController的控制器之间的跳转

    一.效果图如下 1> 第一个控制器的NavigationBar隐藏 2> 有按钮,可以跳转到下一个控制器,返回上一个控制器或者根控制器 二.思路代码 思路: 1> 设置window的 ...

  5. js加载优化

    阻塞特性:       JS 有个很无语的阻塞特性,就是当浏览器在执行JS 代码时,不能同时做其他任何事情,无论其代码是内嵌的还是外部的. 脚本位置:       浏览器在碰到一个引入外部JS 文件的 ...

  6. SPI 四种模式

    SPI时钟极性CPOL, = 0表示在没有数据传输时为低电平,= 1表示没有数据传输时为高电平. SPI时钟相位CPHA,= 0表示时钟的第一个沿更新数据.第二个沿锁存数据,= 1表示时钟的第一个沿锁 ...

  7. C# GC 垃圾回收

    一.托管 .Net所指的托管资源到底是什么意思呢?是相对于所有资源,还是只限于某一方面的资源?很多人对此不是很了解. 其实.Net所指的托管只是针对内存这一个方面,并不是对于所有的元素:因此对于Str ...

  8. HOWTO Use Python in the web — Python v3.0.1 documentation

    HOWTO Use Python in the web - Python v3.0.1 documentation mod_python¶ People coming from PHP often f ...

  9. python 性能优化

    1.优化循环 循环之外能做的事不要放在循环内,比如下面的优化可以快一倍 2.使用join合并迭代器中的字符串 join对于累加的方式,有大约5倍的提升 3.使用if is 使用if is True比i ...

  10. Unity 制作RPG地图2(自己控制地图上图标)

    上一次用Unity摄像机方式实现了地图的制作,现在介绍另一种实现地图的方式: 自己通过代码实现小地图NCP图标的显示和隐藏 制作地图的步骤: 1. 根据游戏人物的3D坐标转换成2D平面坐标,根据距离显 ...