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. Linux3.5内核以后的路由下一跳缓存

    在Linux3.5版本号(包括)之前.存在一个路由cache.这个路由cache的初衷是美好的,可是现实往往是令人遗憾的.下面是陈列得出的两个问题:1.面临针对hash算法的ddos问题(描写叙述该问 ...

  2. Educational Codeforces Round 12 E. Beautiful Subarrays trie求两异或值大于等于k对数

    E. Beautiful Subarrays   One day, ZS the Coder wrote down an array of integers a with elements a1,   ...

  3. Codeforces Round #282 (Div. 1)B. Obsessive String KMP+DP

    B. Obsessive String   Hamed has recently found a string t and suddenly became quite fond of it. He s ...

  4. 0x31 质数

    poj2689 算根号R的质数,然后把L~R区间(这个很小啊)的合数判下 #include<cstdio> #include<iostream> #include<cst ...

  5. php5.5安装笔记

    这次没想到本来很简单的php编译,没想到遇到那么多问题.再此记录一下. 1.php5.5编译安装主要有一个难点,就是GD库的问题,因为php5.5的GD库必须是2.1以上的版本哦 原来都是用的gd2. ...

  6. 36.QT地图

    widget.h #ifndef MAPWIDGET_H #define MAPWIDGET_H #include <QGraphicsView> #include <QLabel& ...

  7. $(function(){});里的方法无效问题

    $(function(){})已经是一个匿名函数了,在里面定义的函数已经是私有了,不能全局访问.把函数定义在全局,也就是function外面,这样外部才能调用.

  8. Android 多线程下载 显示进度 速度

    功能要求:从网络下载一APK应用,显示下载速度.进度,并安装应用. 运行效果图: 工程结构图: 很简单,就一个activity,一个更新UI的线程,一个下载线程加个文件处理类 主要代码: /** *多 ...

  9. python2中新式类和旧式类的对比【译】

    Classes and instances come in two flavors: old-style (or classic) and new-style. ➤类和实例分为两大类:旧式类和新式类. ...

  10. protocol 和delegate(协议和代理)的区别

    定义 protocol:中文叫协议,一个只有方法体(没有具体实现)的类,Java中称作接口,实现协议的类必须实现协议中@required标记的方法(如果有的话): delegate:中文叫代理或委托, ...