poj 1611 dsu
| Time Limit: 1000MS | Memory Limit: 20000K | |
| Total Submissions: 35918 | Accepted: 17458 |
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 代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#define pi acos(-1.0)
#define mj
#define inf 2e8+500
typedef long long ll;
using namespace std;
const int N=3e4+;
int p[N];
int find(int x)
{
return x==p[x]?x:p[x]=find(p[x]);
}
void unit(int x,int y)
{
x=find(x),y=find(y);
if(x==y) return ;
else p[x]=y;
}
int main()
{
int n,m,x,y;
while(scanf("%d%d",&n,&m)==,n||m){
for(int i=;i<n;i++){
p[i]=i;
}
int num;
for(int i=;i<m;i++){
scanf("%d%d",&num,&x);
num--;
while(num--){
scanf("%d",&y);
unit(x,y);
}
}
int ans=;
for(int i=;i<n;i++){
if(find()==find(i)) ans++;
}
printf("%d\n",ans);
}
return ;
}
poj 1611 dsu的更多相关文章
- poj 1611(并查集)
http://poj.org/problem?id=1611 题意:有个学生感染病毒了,只要是和这个学生接触过的人都会感染,而和这些被感染者接触的人,也会被感染,现在给定你一些协会的人数,以及所在学生 ...
- poj 1611 The Suspects 解题报告
题目链接:http://poj.org/problem?id=1611 题意:给定n个人和m个群,接下来是m行,每行给出该群内的人数以及这些人所对应的编号.需要统计出跟编号0的人有直接或间接关系的人数 ...
- poj 1611 The Suspects(简单并查集)
题目:http://poj.org/problem?id=1611 0号是病原,求多少人有可能感染 #include<stdio.h> #include<string.h> # ...
- 【原创】poj ----- 1611 The Suspects 解题报告
题目地址: http://poj.org/problem?id=1611 题目内容: The Suspects Time Limit: 1000MS Memory Limit: 20000K To ...
- (并查集)The Suspects --POJ --1611
链接: http://poj.org/problem?id=1611 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#probl ...
- POJ - 1611 The Suspects 【并查集】
题目链接 http://poj.org/problem?id=1611 题意 给出 n, m 有n个人 编号为 0 - n - 1 有m组人 他们之间是有关系的 编号为 0 的人是 有嫌疑的 然后和 ...
- 【裸的并查集】POJ 1611 The Suspects
http://poj.org/problem?id=1611 [Accepted] #include<iostream> #include<cstdio> #include&l ...
- 并查集 (poj 1611 The Suspects)
原题链接:http://poj.org/problem?id=1611 简单记录下并查集的模板 #include <cstdio> #include <iostream> #i ...
- [并查集] POJ 1611 The Suspects
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 35206 Accepted: 17097 De ...
随机推荐
- ArrayList、Vector、HashMap、HashSet
1. list 和 set 的有序无序是否可重复 List是有序可重复的: Set是无序不可重复的: 2.ArrayList.Vector.HashMap.HashSet的默认初始容量.加载因子.扩 ...
- 问问javascript
问题1:在创建新函数(如function P(){};)的时候会自动创建一个原型对象P.prototype(也称作原型属性prototype).当创建一个新对象(此处指非函数对象,在js里面函数也会被 ...
- android sqlite3命令行检查自己的代码操作数据库是否正确
真机调试的话需要root ,否则没有访问目录的权限 在 linux 的终端 或者 windows的cmd 中输入 adb shell 进入shell 环境 cd /data/data/程序包名/dat ...
- RC4 in TLS is Broken: Now What?
https://community.qualys.com/blogs/securitylabs/2013/03/19/rc4-in-tls-is-broken-now-what RC4 has lon ...
- TP5.1:模板赋值与变量输出
模板赋值:assign() 模板渲染:fetch() 前提准备: 1.在app/index/controller下建立一个控制器,名为Templates.php,里面有test1和test2方法,并且 ...
- 深入了解MongoDB
一.介绍: 数据库分为关系型数据库和非关系型数据库 关系型数据库是建立在关系模型上的数据库,主要的有Oracle MySQL Microsoft SQL Server NoSQL是非关系型数据存储的广 ...
- 504. Inverted Index (Map Reduce) lintcode
https://www.lintcode.com/problem/inverted-index-map-reduce/description -- decription of the map redu ...
- lambda Expression的使用方法
Expression<Func<your class, bool>> whereExp = f => true;//类似1=1,初始化条件 if (!string.IsN ...
- hash函数的选择
哈稀函数按照定义可以实现一个伪随机数生成器(PRNG),从这个角度可以得到一个公认的结论:哈希函数之间性能的比较可以通过比较其在伪随机生成方面的比较来衡量. 一般来说,对任意一类的数据存在一个理论上完 ...
- python图形库(1)
python有很好图形库cv2(包含很多图形处理的算法),pylab(绘图工具模块) 这两个“模块”是肯定要配置的. 安装这两个模块可用了我不少时间. pylab它不是一个包,而是 numpy, sc ...