杭电 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 ...
随机推荐
- HTML5中div,article,section的区别
最近正在学习html5,刚接触html5,感觉有点不适应,因为有一些标签改变了,特别是div, section article这三个标签,查了一些资料,也试着用html5和css3布局网页,稍微有点头 ...
- hdu1856 More is better 基础并查集
#include <cstdio> #include <cstring> #include <algorithm> #include <cstdlib> ...
- spring3升级到spring4
升级又失败了,dao层太多要改了,记录一下修改的内容,也是没白费我一下午时间 1. org.springframework.orm.hibernate3.annotation.AnnotationSe ...
- [WOJ1583]向右看齐
题目链接: WOJ1583 题目分析: 大水题--我就来水个题解 倒序扫,单调栈维护单减序列,每个对象的答案是栈里它下面那个元素 代码: #include<bits/stdc++.h> # ...
- java 反向工具类
1.ReflectUtil.java package com.example.scansell; import android.util.Log; import java.lang.reflect.C ...
- h5-21-文件操作-读取文件内容
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 把AM_B_ENTRY表里的ARCHIVAL_CODE字段,值复制给BA_ARCHIVAL_CODE_160812字段
UPDATE AM_B_ENTRY T SET T.BA_ARCHIVAL_CODE_160812=T.ARCHIVAL_CODE SELECT BA_ARCHIVAL_CODE_160812 FRO ...
- MySql数据基本操作sql语句
表字段 1>插入数据insert 如果不插入id,让id自动增加 INSERT INTO `gpw_group`(group_code, group_parent_id, group_name, ...
- 507 Perfect Number 完美数
对于一个 正整数,如果它和除了它自身以外的所有正因子之和相等,我们称它为“完美数”.给定一个 正整数 n, 如果他是完美数,返回 True,否则返回 False示例:输入: 28输出: True解释: ...
- C/S WinForm自动升级
这二天刚好完成一个C/S 自动升级的功能 代码分享一下 /// <summary> /// 版本检测 /// </summary> public class ...