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 ...
随机推荐
- Python requests快速上手
Python requests快速上手 这里参考官方文档,在ide中写了一遍,加深一下印象,定义的函数只是为了方便区分不同的请求方式 #-*-coding:utf-8-*- # Time:2017/1 ...
- http-server 基于nodejs的http服务器
http-server所用场景: 作为前端的同学来说,想要运行一段代码,但又没有必要使用tomcat或是Apache http server,这个时候,一个简单的轻量的http-server就能搞定. ...
- LeetCode--058--最后一个单词的长度
问题描述 给定一个仅包含大小写字母和空格 ' ' 的字符串,返回其最后一个单词的长度. 如果不存在最后一个单词,请返回 0 . 说明:一个单词是指由字母组成,但不包含任何空格的字符串. 示例: 输入: ...
- Java 主要特性
Java 有下面的一些主要特性. 面向对象 在 Java 中,所有的都是对象.正式因为 Java 基于对象模型,所以 Java 更加容易进行扩展. Java语言提供类.接口和继承等面向对象的特性,为了 ...
- 卸载 PrestaShop 1.7
PrestaShop 的卸载非常简单: 在你的 Web 服务器上删除所有 PrestaShop 的文件和目录.你可以使用 FTP 客户端,你也可以使用 SSH 工具. 使用数据库工具删除数据库中所有以 ...
- CF1083B The Fair Nut and String
题意 给出两个长度为n的01字符串S和T. 选出k个字典序在S和T之间的长度为n的01字符串,使得尽可能多的字符串满足其是所选字符串中至少一个串的前缀. 这是一道思路比较奇怪的类似计数dp的题. 首先 ...
- Nginx配置https, 80端口重定向443
server { listen 443 ssl; server_name 域名; charset utf-8; access_log /var/log/nginx/webhook.iminho.me/ ...
- Hive之 Python写UDF
大自然的搬运工: 参考: 使用Python编写Hive UDF https://www.iteblog.com/archives/2329.html 使用 Python 编写 Hive UDF 环境问 ...
- Leetcode 105
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
- quartz---的Cron表达式
quartz---的Cron表达式 CronTrigger CronTriggers往往比SimpleTrigger更有用,如果您需要基于日历的概念,而非SimpleTrigger完全指定的时间间隔, ...