How Many Tables

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 17946    Accepted Submission(s): 8822

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
 
Recommend

推断几个人是否是朋友。。

。。。

。。。。。。。。。

。。。。

。。。

仅仅要我们把这几个人看成几棵树好啦。

。我们就数数有几棵树即可。

既然要数有几棵树。那我们怎么区分它们是不是同一棵树呢?就须要推断它们的老祖宗是不是同样。。

并查集啦 并查集

看代码,看代码。。

先想想思想,再自己动手去做。不要照抄、、、

#include <stdio.h>
#include <string.h>
int fa[1005],n;
int find(int x)
{
if(fa[x]!=x) fa[x]=find(fa[x]);
return fa[x];
}
void init()
{
for(int i=1;i<=n;i++)
fa[i]=i;
}
int main()
{
int ncase,m;
scanf("%d",&ncase);
while(ncase--)
{
scanf("%d %d",&n,&m);
init();
for(int i=0;i<m;i++)
{
int a,b;
scanf("%d %d",&a,&b);
int x=find(a);
int y=find(b);
if(x!=y)
fa[x]=y;
}
int count=0;
for(int i=1;i<=n;i++)
if(fa[i]==i)
count++;
printf("%d\n",count);
}
return 0;
}

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

  1. hdu1213 How Many Tables 并查集的简单应用

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 简单的并查集 代码: #include<iostream> #include< ...

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

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

  3. C - How Many Tables 并查集

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

  4. POJ-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(并查集,简单)

    题解:1 2,2 3,4 5,是朋友,所以可以坐一起,求最小的桌子数,那就是2个,因为1 2 3坐一桌,4 5坐一桌.简单的并查集应用,但注意题意是从1到n的,所以要减1. 代码: #include ...

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

    并查集基本知识看:http://blog.csdn.net/dellaserss/article/details/7724401 题意:假设一张桌子可坐无限多人,小明准备邀请一些朋友来,所有有关系的朋 ...

  7. HDU 1213 How Many Tables 并查集 寻找不同集合的个数

    题目大意:有n个人 m行数据,每行数据给出两个数A B,代表A-B认识,如果A-B B-C认识则A-C认识,认识的人可以做一个桌子,问最少需要多少个桌子. 题目思路:利用并查集对相互认识的人进行集合的 ...

  8. HDU 1213 How Many Tables 并查集 水~

    http://acm.hdu.edu.cn/showproblem.php?pid=1213 果然是需要我陪跑T T,禽兽工作人员还不让,哼,但还是陪跑了~ 啊,还有呀,明天校运会终于不用去了~耶耶耶 ...

  9. HDU1213最简单的并查集问题

    题目地址 http://acm.hdu.edu.cn/showproblem.php?pid=1213 #include<iostream> using namespace std; #d ...

随机推荐

  1. Shell脚本:推断用户和用户组是否已经存在/创建用户和用户组

    通常作为一个应用程序的部署脚本,開始的第一项工作是为当前应用创建一个专用(dedicated)的用户和用户组.这个脚本非常easy.这里贴一个參考样本: #!/bin/sh user=test_use ...

  2. unity3d面试题摘选(全)

    ======================================= 数据结构和算法非常重要.图形学也非常重要! 大的游戏公司非常看重个人基础.综合能力.也有的看重你实际工作能力,看你的De ...

  3. BZOJ 2751 容易题(easy) 快速幂+快速乘

    2751: [HAOI2012]容易题(easy) Description 为了使得大家高兴,小Q特意出个自认为的简单题(easy)来满足大家,这道简单题是描述如下:有一个数列A已知对于所有的A[i] ...

  4. Bitcoin学习篇之---PPS和PPLNS挖矿模式介绍

    PPS和PPLNS挖矿模式介绍 比特币每10分钟产生一个区块,会有千万人竞争.而这个区块终于仅仅归1个人全部.其他人都颗粒无收. 你或许要挖5年才干获得一个区块. 组队挖矿就是.一旦队伍里不论什么人获 ...

  5. Oracle在Linux下的性能优化

    Oracle数据库内存参数的优化 Ø       与oracle相关的系统内核参数 Ø       SGA.PGA参数设置   Oracle下磁盘存储性能优化 Ø       文件系统的选择(ext2 ...

  6. C# Keywords - is

    记录一下在日常开发过程中遇到的一些C# 基础编程的知识!希望以后能用的着.知识是在平常的开发过程中去学到的.只有用到了,你才能深入的理解它,并用好它. 本资料来源于:MSND下面是一些相关的code ...

  7. 17.QT键盘

    mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> 5 #include <Q ...

  8. javascript中document.getElementsByClassName兼容性封装方法一

    var getElmsByClsName = function(className, results) { results = results || []; // 判断浏览器是否支持 getEleme ...

  9. 基于XMPP利用openfire简单的即时通讯

    功能的实现结果:能够使自己编写客户端与spark客户端信息通讯,将接受到的信息更新到textview上. 1.下载openfire并安装.设置域名,添加用户 2.下载安装spark客户端 3.jar包 ...

  10. 织梦Fatal error: Call to a member function GetInnerText()

    问题:织梦修改或者添加了自定义表单后在后台修改文章的时候出现如下错误:Fatal error: Call to a member function GetInnerText() on a non-ob ...