poj 1611:The Suspects(并查集,经典题)
Time Limit: 1000MS | Memory Limit: 20000K | |
Total Submissions: 21472 | Accepted: 10393 |
Description
In the Not-Spreading-Your-Sickness University (NSYSU), there are many student groups. Students in the same group intercommunicate with each other frequently, and a student may join several groups. To prevent the possible transmissions of SARS, the NSYSU collects the member lists of all student groups, and makes the following rule in their standard operation procedure (SOP).
Once a member in a group is a suspect, all members in the group are suspects.
However, they find that it is not easy to identify all the suspects when a student is recognized as a suspect. Your job is to write a program which finds all the suspects.
Input
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.
Output
Sample Input
100 4
2 1 2
5 10 13 11 12 14
2 0 1
2 99 2
200 2
1 5
5 1 2 3 4 5
1 0
0 0
Sample Output
4
1
1
Source
int ufs[MAXN]; //并查集 void Init(int n) //初始化
{
int i;
for(i=;i<n;i++){
ufs[i] = i;
}
} int GetRoot(int a) //获得a的根节点。路径压缩
{
if(ufs[a]!=a){ //没找到根节点
ufs[a] = GetRoot(ufs[a]);
}
return ufs[a];
} void Merge(int a,int b) //合并a和b的集合
{
ufs[GetRoot(b)] = GetRoot(a);
} bool Query(int a,int b) //查询a和b是否在同一集合
{
return GetRoot(a)==GetRoot(b);
}

#include <iostream>
#include <stdio.h>
using namespace std;
#define MAXN 30010
int sum[MAXN]; //集合总数
int ufs[MAXN]; //并查集 void Init(int n) //初始化
{
int i;
for(i=;i<n;i++){
ufs[i] = i;
sum[i] = ;
}
} int GetRoot(int a) //获得a的根节点。路径压缩
{
if(ufs[a]!=a){ //没找到根节点
ufs[a] = GetRoot(ufs[a]);
}
return ufs[a];
} void Merge(int a,int b) //合并a和b的集合
{
int x = GetRoot(a);
int y = GetRoot(b);
if(x!=y){
ufs[y] = x;
sum[x] += sum[y];
}
} int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
if(n== && m==) break;
Init(n); //初始化并查集
while(m--){ //读入m行
int t,one,two;
scanf("%d",&t); //每一行有t个数需要输入
scanf("%d",&one);
t--;
while(t--){
scanf("%d",&two);
Merge(one,two); //合并集合
}
}
printf("%d\n",sum[GetRoot()]);
}
return ;
}
Freecode : www.cnblogs.com/yym2013
poj 1611:The Suspects(并查集,经典题)的更多相关文章
- poj 1611 The Suspects(并查集输出集合个数)
Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...
- poj 1611 The Suspects 并查集变形题目
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 20596 Accepted: 9998 D ...
- POJ 1611 The Suspects (并查集+数组记录子孙个数 )
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 24134 Accepted: 11787 De ...
- POJ 1611 The Suspects (并查集求数量)
Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...
- POJ 1611 The Suspects 并查集 Union Find
本题也是个标准的并查集题解. 操作完并查集之后,就是要找和0节点在同一个集合的元素有多少. 注意这个操作,须要先找到0的父母节点.然后查找有多少个节点的额父母节点和0的父母节点同样. 这个时候须要对每 ...
- poj 1611 The Suspects 并查集
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 30522 Accepted: 14836 De ...
- [ACM] POJ 1611 The Suspects (并查集,输出第i个人所在集合的总人数)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21586 Accepted: 10456 De ...
- POJ1611 The Suspects 并查集模板题
题目大意:中文题不多说了 题目思路:将每一个可能患病的人纳入同一个集合,然后遍历查找每个点,如果改点点的根节点和0号学生的根节点相同,则该点可能是病人. 模板题并没有思路上的困难,只不过在遍历时需要额 ...
- 【转】并查集&MST题集
转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...
- poj1182 食物链(并查集 好题)
https://vjudge.net/problem/POJ-1182 并查集经典题 对于每只动物创建3个元素,x, x+N, x+2*N(分别表示x属于A类,B类和C类). 把两个元素放在一个组代表 ...
随机推荐
- BestCoder Round #61 1001 Numbers
Problem Description There are n numbers A1,A2....An{A}_{1},{A}_{2}....{A}_{n}A1,A2....An,yo ...
- U盘安装中标麒麟服务器操作系统 一 (NeoKylin 6.5)
U盘安装中标麒麟服务器操作系统(NeoKylin 6.5) 首先需要下载中标麒麟服务器操作系统的iso镜像.我这里的是NeoKylin Linux A 6.5.iso 因为超过了4GB,百度网盘不支持 ...
- PyQt4软件打包成exe文件
使用py2exe进行打包 例: from distutils.core import setupimport py2exeimport sys sys.argv.append('py2exe') py ...
- SQL Sever2008r2 数据库服务各种无法启动的解决办法
一.Sql Server服务远程过程调用失败解决 以前出现过这个问题,那时候是因为把实例安装在了D盘,后来D盘被格式化了.然后,这些就没了.今天早上打开电脑,竟然又出现这个问题,可是Server200 ...
- python wraps
用代码说明问题: def d(f): def _d(*args, **kwargs): print f.__name__, ' is called' f(*args, **kwargs) return ...
- eclipes的Spring注解SequenceGenerator(name="sequenceGenerator")报错的解决方式
eclipes的Spring注解SequenceGenerator(name="sequenceGenerator")报错的解决方式 右键项目打开Properties—>JA ...
- 深度分析Linux下双网卡绑定七种模式 多网卡的7种bond模式原理
http://blog.csdn.net/abc_ii/article/details/9991845多网卡的7种bond模式原理 Linux网卡绑定mode共有七种(~) bond0.bond1.b ...
- 关于 Tomcat 的线程池的理解
默认配置下,Tomcat 会为每个连接器创建一个绑定的线程池(最大线程数 200).在大多数情况下你不需要改这个配置(除非增大最大线程数以满足高负载需要).但是 Tomcat 喜欢在每个工作者线程的 ...
- [k]自定义上传文件按钮样式
<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title> ...
- STL---vector(向量)
1 基本操作 (1)头文件#include<vector>. (2)创建vector对象,vector<int> vec; (3)尾部插入数字:vec.push_back(a) ...