hdu 1213 How Many Tables(并查集算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213
How Many Tables
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13787 Accepted Submission(s):
6760
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.
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.
Ignatius needs at least. Do NOT print any blanks.
#include <iostream>
#include <cstdio>
using namespace std;
int father[]; void set(int n)
{
for (int i=; i<=n; i++)
father[i]=i;
} int find(int a)
{
if (father[a]==a)
return a;
return father[a]=find(father[a]);
} void Union(int x,int y)
{
x=find(x);
y=find(y);
if (x!=y)
father[x]=y;
} int main ()
{
int t,k;
while (cin>>t)
{ while (t--)
{
k=;
int n,m;
cin>>n>>m;
set(n);
while (m--)
{
int s,d;
cin>>s>>d;
Union(s,d);
}
for (int i=; i<=n; i++)
{
if (father[i]==i)
k++;
}
printf ("%d\n",k);
}
}
return ;
}
hdu 1213 How Many Tables(并查集算法)的更多相关文章
- HDU 1213 - How Many Tables - [并查集模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...
- HDU 1213 How Many Tables 并查集 水~
http://acm.hdu.edu.cn/showproblem.php?pid=1213 果然是需要我陪跑T T,禽兽工作人员还不让,哼,但还是陪跑了~ 啊,还有呀,明天校运会终于不用去了~耶耶耶 ...
- HDU 1213 How Many Tables(并查集,简单)
题解:1 2,2 3,4 5,是朋友,所以可以坐一起,求最小的桌子数,那就是2个,因为1 2 3坐一桌,4 5坐一桌.简单的并查集应用,但注意题意是从1到n的,所以要减1. 代码: #include ...
- HDU 1213 How Many Tables (并查集,常规)
并查集基本知识看:http://blog.csdn.net/dellaserss/article/details/7724401 题意:假设一张桌子可坐无限多人,小明准备邀请一些朋友来,所有有关系的朋 ...
- HDU 1213 How Many Tables 并查集 寻找不同集合的个数
题目大意:有n个人 m行数据,每行数据给出两个数A B,代表A-B认识,如果A-B B-C认识则A-C认识,认识的人可以做一个桌子,问最少需要多少个桌子. 题目思路:利用并查集对相互认识的人进行集合的 ...
- hdu 1232 畅通工程(并查集算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1232 畅通工程 Time Limit: 4000/2000 MS (Java/Others) M ...
- hdu 1213 求连通分量(并查集模板题)
求连通分量 Sample Input2 //T5 3 //n m1 2// u v2 34 5 5 12 5 Sample Output24 # include <iostream> # ...
- HDU 1213 How Many Tables(模板——并查集)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday ...
- HDU 1863 畅通工程 (并查集)
原题链接:畅通工程 题目分析:典型的并查集模版题,这里就不详细叙述了.对算法本身不太了解的可以参考这篇文章:并查集算法详解 代码如下: #include <iostream> #inclu ...
- Union-Find 并查集算法
一.动态连通性(Dynamic Connectivity) Union-Find 算法(中文称并查集算法)是解决动态连通性(Dynamic Conectivity)问题的一种算法.动态连通性是计算机图 ...
随机推荐
- 工具函数:cookie的添加、获取、删除
cookie是浏览器存储的命名数据,作用是保存用户的信息,这样我们就可以用这些信息来做一些事了,但是cookie容量很小,只有4kb. 下面是我总结的cookie的添加.获取.删除的函数: cooki ...
- project之chrome.exe
查看chrome.exe的以来文件可以得到下面这个列面,大部分是在%systemroot%/system32下面的系统dll文件,只有两个是chromium自己生成的:base.dll, conten ...
- cacti安装spine 解决WARNING: Result from CMD not valid. Partial Result: U错误
安装spine用来替换cacti原本的执行方式,需要的包在附件中,请注意spine的安装包和你安装的cacti版本不用相同,最好是最新的spine 1.安装gcc #yum install -y gc ...
- 文件“bin\Debug\WindowsFormsApplication2.exe”正由另一进程使用,因此该进程无法访问该文件。
http://zhidao.baidu.com/question/221394579.html?qbl=relate_question_2&word=%BE%AF%B8%E6%094%09%C ...
- AutoHotKey 快速入门
AutoHotKey 是一个免费的键盘宏程序,可以用于配置键盘快捷键.鼠标事件 以及摇杆事件,还可以在输入文本的时候对文本进行扩展(自动补全) 第一个脚本 新建文件test.ahk并输入以下内容: ^ ...
- 【转】C# Datatable排序与取前几行数据
转自:http://www.cnblogs.com/linyechengwei/archive/2010/06/14/1758337.html http://blog.csdn.net/smartsm ...
- BZOJ3572:[HNOI2014]世界树——题解
+++++++++++++++++++++++++++++++++++++++++++ +本文作者:luyouqi233. + +欢迎访问我的博客:http://www.cnblogs.com/luy ...
- POI 2018.10.20
[POI2005]BANK-Cash Dispenser 有多少个4位字符串是所有操作序列的子串. 10^4枚举字符串.暴力判断会TLE 发现,我们就是在每个操作序列中不断找第一个出现的c字符. 预处 ...
- C++分离字符串中的数字和字符 转
#include <iostream> #include <string> #include <vector> using namespace std; void ...
- Python通过PhantomJS获取JS渲染后的网页源代码
新建一个文件,命名为test.js,内容如下: var page = require('webpage').create(), system = require('system'), address; ...