HDU--1213--How Many Tables--并查集
How Many Tables
Total Submission(s): 9066 Accepted Submission(s): 4429
Problem Description
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.
#include <iostream>
using namespace std;
int father[];
int Find(int a) //找到祖先并所有家族成员指向祖先
{
return father[a]==a?a:father[a]=Find(father[a]);
}
void Union(int a,int b) //把a家族并入b家族,a祖先指向b祖先
{
if(father[a]!=father[b])
father[Find(a)]=Find(b);
}
int main (void)
{
int t,n,m,i,j,k,l;
cin>>t;
while(t--&&cin>>n>>m)
{
for(i=;i<=n;i++)
father[i]=i;
for(i=;i<m;i++)
{
cin>>k>>l;
Union(k,l);
}
for(i=,j=;i<=n;i++)
if(father[i]==i) //计算有多少独立个体即计算多少家族即计算祖先数
{
j++;
}
cout<<j<<endl;
}
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 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 1213 How Many Tables(并查集模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意: 这个问题的一个重要规则是,如果我告诉你A知道B,B知道C,这意味着A,B,C知道对方,所以他们可以 ...
- HDU - 1213 How Many Tables 【并查集】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意 给出N个人 M对关系 找出共有几对连通块 思路 并查集 AC代码 #include < ...
- HDU 1213 How Many Tables (并查集)
How Many Tables 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/C Description Today is Ig ...
随机推荐
- windows系统调用 临界区机制
#include "iostream" #include "windows.h" #include "cstring" using name ...
- HttpClient_002_中文乱码、HttpClient中文乱码透析、总结
中文乱码原理代码: String s = "中文"; byte[] bs2 = s.getBytes("utf-8");//将s拆成:utf-8个体,注:utf ...
- CSS3 border-image详解、应用
一.border-image的兼容性 border-image可以说是CSS3中的一员大将,将来一定会大放光彩,其应用潜力真的是非常的惊人.可惜目前支持的浏览器有限,仅Firefox3.5,chrom ...
- 执行gem install linne时报错
由于linner安装实际上是从 rubygems.org 获得的,而其被墙,所以,需要寻找国内的镜像进行安装: 第一种方法: gem sources --remove https://rubygems ...
- UltraISO向U盘写入镜像特别慢
电脑:Dell INSPIRON 1416 系统:WIN7旗舰版32位 U盘:金士顿8G 镜像:CentOS7 ×86_64 问题: 开始使用"写入"功能,写入速度72k/s 后来 ...
- SOAP和WSDL的一些必要知识
SOAP和WSDL对Web Service.WCF进行深入了解的基础,因此花一些时间去了解一下是很有必要的. 一.SOAP(Simple Object Access Protocol) 如果我们要调用 ...
- noi 9267 核电站
题目链接:http://noi.openjudge.cn/ch0206/9267/ 描述 一个核电站有N个放核物质的坑,坑排列在一条直线上.如果连续M个坑中放入核物质,则会发生爆炸,于是,在某些坑中可 ...
- SQL Server T-SQL高级查询
name like 'ja%'; select * from student where name not like '%[j,n]%'; select * from student where na ...
- stoneniqiu 理想就是自己喜欢做,并对社会和他人都有意义的事情!
H5嵌入原生开发小结----兼容安卓与ios的填坑之路 2016-11-19 08:29 by stoneniqiu, 1038 阅读, 8 评论, 收藏, 编辑 一开始听说开发H5,以为就是做适配现 ...
- ubuntu删除软件命令
第一步,apt-get remove xxx :就是卸载xxx 或者 apt-get remove --purge xxx :卸载xxx并清除配置. 这两条命令对于依赖则是不管的.因为别的软件可 ...