杭电 1213 How Many Tables (并查集求团体数)
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.
Input
Output
Sample Input
2
5 3
1 2
2 3
4 5 5 1
2 5
Sample Output
2
4 大意:
有n个人,m中关系,每种关系的人在一起,问有多少种团体;第一组测试数据有5个人编号1~5,3种关系1和2,2和3,4和5,所以1,2,3为一个团体,4,5为一个团体。 将有关系的人放在一个集合,求出集合数。
并查集模板
#include<cstdio>
int n,m,ans,i,a,b,fa[]; /*int find(int a)
{
int r=a;
while(r != fa[r])
{
r=fa[r];
}
int i=a,j; //循环实现路径压缩
while(i != r)
{
j=fa[i];
fa[i]=r;
i=j;
}
return r;
}*/ int find(int a) //递归实现路径压缩
{
if(a == fa[a])
{
return a;
}
else
{
return fa[a]=find(fa[a]);
}
} void f1(int x,int y)
{
int nx,ny;
nx=find(x);
ny=find(y);
if(nx != ny)
{
fa[ny] = nx;
}
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m);
for(i = ; i <= n ; i++)
{
fa[i] = i;
}
for(i = ; i < m ; i++)
{
scanf("%d %d",&a,&b);
f1(a,b);
}
ans=;
for(i = ; i <= n ; i++)
{
if(fa[i] == i)
{
ans++;
}
}
printf("%d\n",ans);
}
}
杭电 1213 How Many Tables (并查集求团体数)的更多相关文章
- 杭电OJ——1198 Farm Irrigation (并查集)
畅通工程 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可 ...
- 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认识,认识的人可以做一个桌子,问最少需要多少个桌子. 题目思路:利用并查集对相互认识的人进行集合的 ...
- 【杭电OJ3938】【离线+并查集】
http://acm.hdu.edu.cn/showproblem.php?pid=3938 Portal Time Limit: 2000/1000 MS (Java/Others) Memo ...
- 杭电 2120 Ice_cream's world I (并查集求环数)
Description ice_cream's world is a rich country, it has many fertile lands. Today, the queen of ice_ ...
- Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 并查集求奇偶元环
D. Dividing Kingdom II Long time ago, there was a great kingdom and it was being ruled by The Grea ...
随机推荐
- spring 通配符
原文地址:http://www.bubuko.com/infodetail-848675.html classpath是指 WEB-INF文件夹下的classes目录(惯例大于配置) classpat ...
- linux添加开机启动脚本
[root@mysql ~]# ll /etc/rc.local lrwxrwxrwx. 1 root root 13 Mar 12 22:20 /etc/rc.local -> rc.d/rc ...
- YII2修改backend模块报错An Error occurred while handling another error: exception 'yii\base\InvalidRouteException' with message 'Unable to resolve the request "site/error".' in E:\project\demo\vendor\yiisoft
报错内容: 原因:没有修改common/config/bootstrap.php里的别名 修改后:
- CSS实现文字旋转/实现角标
主要用到属性transform:rotate(-30deg) example: .divedittable .project-tag div { width: 43px; line-height: 4 ...
- php url地址栏传中文乱码解决方法集合
php地址栏传中文$_GET下来后乱码,urlencode和urldecode,iconv,base64_encode等方法,整理基本是常用的了. php地址栏传中文$_GET下来后乱码,urlen ...
- P1597 语句解析
题目背景 木有背景…… 题目描述 一串(<255)PASCAL语言,只有a,b,c 3个变量,而且只有赋值语句,赋值只能是一个一位的数字或一个变量,未赋值的变量值为0.输出a,b,c 最终的值. ...
- mybatis generator 整合lombok
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration ...
- [ CQOI 2018 ] 异或序列
\(\\\) Description 给出一个长为 \(n\) 的数列 \(A\) 和 \(k\),多次询问: 对于一个区间 \([L_i,R_i]\),问区间内有多少个不为空的子段异或和为 \(k\ ...
- flutter基础
1.flutter安装 1.参考官网安装sdk https://flutter.io/get-started/install 安卓和IOS需要分别配置对应的开发环境,安卓建议使用as开发,安装Flut ...
- Get value from agent failed: cannot connect to [[ip]:10050]: no rout host
被监控机添加之后,可用性显示红色,鼠标放上去之后显示如标题问题,关闭被监控机器的防火墙,设置setenforce 0,可用性变可用.