Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to separate the suspects from others.
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

The input file contains several cases. Each test case begins with
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

For each case, output the number of suspects in one line.

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 并查集的更多相关文章

  1. The Suspects(并查集维护根节点信息)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 37090   Accepted: 17980 De ...

  2. poj 1611 The Suspects(并查集输出集合个数)

    Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...

  3. poj 1611 The Suspects 并查集变形题目

    The Suspects   Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 20596   Accepted: 9998 D ...

  4. B - The Suspects(并查集)

    B - The Suspects Time Limit:1000MS     Memory Limit:20000KB     64bit IO Format:%lld & %llu Desc ...

  5. POJ 1611 The Suspects (并查集+数组记录子孙个数 )

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 24134   Accepted: 11787 De ...

  6. POJ 1611 The Suspects (并查集求数量)

    Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...

  7. POJ 1611 The Suspects 并查集 Union Find

    本题也是个标准的并查集题解. 操作完并查集之后,就是要找和0节点在同一个集合的元素有多少. 注意这个操作,须要先找到0的父母节点.然后查找有多少个节点的额父母节点和0的父母节点同样. 这个时候须要对每 ...

  8. poj 1611 The Suspects 并查集

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 30522   Accepted: 14836 De ...

  9. The Suspects(并查集求节点数)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 28164   Accepted: 13718 De ...

  10. [ACM] POJ 1611 The Suspects (并查集,输出第i个人所在集合的总人数)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 21586   Accepted: 10456 De ...

随机推荐

  1. Shell循环输入符合条件为止

    提供用户输入,直到输入d/D/r/R为止. #!/bin/bash ]; do echo -n "(D)ebug or (R)elease?" read select_build_ ...

  2. mac 下安装mongodb

    转载自https://segmentfault.com/a/1190000002547229 概念 MongoDB 是一个跨平台的,面向文档的数据库,提供高性能,高可用性和可扩展性方便. MongoD ...

  3. iconfont使用方法

    最近才发现一种新的iconfont的使用方法 http://www.iconfont.cn/ 经常使用的图标放在一个项目里 需要关注的几点: 使用方法有三种方式unicode  , font clas ...

  4. LeetCode--118--杨辉三件I

    问题描述: 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2 ...

  5. pandas的concat函数和append方法

    pd.concat(objs, axis=0, join='outer', join_axes=None, ignore_index=False,keys=None, levels=None, nam ...

  6. Anton and School - 2 CodeForces - 785D (组合计数,括号匹配)

    大意: 给定括号字符串, 求多少个子序列是RSGS. RSGS定义如下: It is not empty (that is n ≠ 0). The length of the sequence is ...

  7. Animation同时改变多个属性的动画

    <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    < ...

  8. css控制编辑器内容自动换行

    在编辑器或者文本框中按住数字或字母不放 当字符很长时,就会撑破页面, 可以用一下方法控制字符自动换行 style="word-break:break-all;"

  9. ipython与sublime调用其shell出现的问题

    本机电脑 win10 已安装python3.5 1.    直接在命令行运行 pip install ipython[all] 安装 ipython 安装完成后 在命令行输入 jupyter note ...

  10. python爬虫田康林

       代码如下 import requestsimport json import time import os from threading import Timer def getHTML(url ...