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

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
 题解同hdu1232
#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的更多相关文章

  1. Hdoj 1213.How Many Tables 题解

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

  2. hdu 1213 How Many Tables(并查集算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 How Many Tables Time Limit: 2000/1000 MS (Java/O ...

  3. HDOJ并查集题目 HDOJ 1213 HDOJ 1242

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

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

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday ...

  5. HDU 1213 - How Many Tables - [并查集模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...

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

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

  7. hdu 1213 How Many Tables 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 有关系(直接或间接均可)的人就坐在一张桌子,我们要统计的是最少需要的桌子数. 并查集的入门题,什 ...

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

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

  9. 1213 How Many Tables(简单并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 简单并查集,统计单独成树的数量. 代码: #include <stdio.h> #i ...

随机推荐

  1. Android 获取JSP或ASP的sessionId(Cookie)

    如果使用的是httpClient: try { HttpResponse response = HttpUtil.httpClient.execute(new HttpGet(url)); Heade ...

  2. 一个php类 Autoloader

    php autoloader: This is a class for PHP that keeps the user from having to manually include classes ...

  3. c#自带压缩类实现数据库表导出到CSV压缩文件的方法

    在导出大量CSV数据的时候,常常体积较大,采用C#自带的压缩类,可以方便的实现该功能,并且压缩比例很高,该方法在我的开源工具DataPie中已经经过实践检验.我的上一篇博客<功能齐全.效率一流的 ...

  4. 【HDOJ】4370 0 or 1

    挺有意思的题目.注意等式的条件.等式1实际表示点1的出度为1,等式2实际表示点2的入度为1,等式表示其它点为中间点,入度和出度相等.很容易转换成一条最短路.spfa直接可求,C即为邻接矩阵.同时,可能 ...

  5. Ubuntu设置中文

    Ubuntu设置中文:需要联网下载中文包,不然无法设置中文系统. 进去系统后再右上角有个齿轮图标点击,找到系统设置(System Settings)点击弹出一个界面,找到Language Suppor ...

  6. Eclipse修改编码后乱码解决

    Eclipse用了一段时间,都是用的gbk编码的,突然想要规范下,强迫症犯了,于是将在Window->Preference->Appearances->Workspace修改Text ...

  7. poj 3694 Network(双连通分量)

    题目:http://poj.org/problem?id=3694 #include <iostream> #include <cstring> #include <cs ...

  8. c# 控件闪烁处理方法

    如果你在Form中绘图的话,不论是不是采用的双缓存,都会看到图片在更新的时候都会不断地闪烁,解决方法就是在这个窗体的构造函数中增加以下三行代码:请在构造函数里面底下加上如下几行:SetStyle(Co ...

  9. SSH2中memcached作为hibernate二级缓存

    一.安装memcached服务端 1. 下载memcached的windows稳定版,解压放某个盘下面,比如在c:\memcached2. 在CMD下输入 "c:\memcached\mem ...

  10. erlang判断语法结构:if/case/guard

    erlang 有好几种常用的判断结构语句,如 if.case.guard 等.文章将分别对 if / case /guard 的特点做介绍,以及用例说明 1.if 结构 if Condition 1  ...