The Suspects 并查集
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
two integers n and m in a line, where n is the number of students, and m
is the number of groups. You may assume that 0 < n <= 30000 and 0
<= m <= 500. Every student is numbered by a unique integer
between 0 and n−1, and initially student 0 is recognized as a suspect in
all the cases. This line is followed by m member lists of the groups,
one line per group. Each line begins with an integer k by itself
representing the number of members in the group. Following the number of
members, there are k integers representing the students in this group.
All the integers in a line are separated by at least one space.
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 <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <string>
#include <cmath>
using namespace std;
int n,m,f[],k,ex[],c;
int init()
{
for(int i=;i<n;i++)
f[i]=i;
}
int getf(int x)
{
if(f[x]!=x)f[x]=getf(f[x]);
return f[x];
}
int merge(int x,int y)
{
int xx=getf(x),yy=getf(y);
f[yy]=xx;
}
int main()
{
while(scanf("%d%d",&n,&m))
{
c=;
if(!m&&!n)break;
init();
for(int i=;i<m;i++)
{
scanf("%d",&k);
for(int i=;i<k;i++)
{
scanf("%d",&ex[i]);
if(i>)merge(ex[i-],ex[i]);
}
}
int fa=getf();
for(int i=;i<n;i++)
if(getf(i)==fa)c++;
cout<<c<<endl;
}
}
The Suspects 并查集的更多相关文章
- The Suspects(并查集维护根节点信息)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 37090 Accepted: 17980 De ...
- 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 ...
- B - The Suspects(并查集)
B - The Suspects Time Limit:1000MS Memory Limit:20000KB 64bit IO Format:%lld & %llu Desc ...
- 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 ...
- The Suspects(并查集求节点数)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 28164 Accepted: 13718 De ...
- [ACM] POJ 1611 The Suspects (并查集,输出第i个人所在集合的总人数)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21586 Accepted: 10456 De ...
随机推荐
- Lua面向对象 --- 继承
工程结构: BasePlayer.lua: BasePlayer = {} BasePlayer.root = "BasePlayer" function BasePlayer:S ...
- Java 常用对象-BigDecimal
2017-11-02 22:03:14 BigDecimal类:不可变的.任意精度的有符号十进制数.BigDecimal 由任意精度的整数非标度值 和 32 位的整数标度 (scale) 组成.如果为 ...
- ASCII 可打印字符与控制字符
2017-08-16 21:29:30 基本的 ASCII 字符集共有 128 个字符,其中有 95 个可打印字符,包括常用的字母.数字.标点符号等,另外还有 33 个控制字符.标准 ASCII 码使 ...
- arcTo
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> < ...
- [LeetCode] 35. Search Insert Position ☆(丢失的数字)
转载:https://leetcode.windliang.cc/leetCode-35-Search-Insert-Position.html 思路 Given a sorted array ...
- JS常用操作方法图表
截取字符串方法 方法名 参数 返回值 例子 String.prototype.substr() (indexStart, length) 都可为负数,length为负数时自动转为0,length不传默 ...
- POJ 2896 AC自动机 or 暴力
DESCRIPTION :大意是说.给你n个代表病毒的字符串.m个表示网站的字符串.让你计算有多少个网站被病毒感染了.被那些病毒感染了. 刚开始就想暴力.然而,忽略了条件:每个网站最多有三个病毒.于是 ...
- vue react自动更新修改的代码
利用node 引入 nodemon,我们项目修改了代码,就不用需要在重新启动 步骤 npm install --save-dev nodemon 执行 nodemon server.js / ...
- DevExpress v17.2新版亮点—WinForms篇(五)
用户界面套包DevExpress v17.2终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.开篇介绍了DevExpress WinForms v17.2 Data Grid Control ...
- hibernate中3个重要的类 Configuration SessionFactory Session
配置类Configuration 主要负责管理hibernate的配置信息以及启动hibernate,在hibernate运行时,配置文件取读底层的配置信息,基本包括数据库驱动,url.usernam ...