The Suspects
Time Limit: 1000MS   Memory Limit: 20000K
Total Submissions: 30158   Accepted: 14665

Description

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  //n个学生,m个分组
2 1 2  //该分组k人,分别...
5 10 13 11 12 14
2 0 1
2 99 2    //假如0感染后,哪些人也会被感染或间接感染
200 2
1 5
5 1 2 3 4 5
1 0
0 0

Sample Output

4
1
1
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define Max 30005
int par[Max],num[Max];
int find(int x)
{
if(par[x]!=x)
return par[x]=find(par[x]);
return par[x];
}
void unite(int x,int y)
{
x=find(x);
y=find(y);
if(x!=y)
{
par[x]=y;
num[y]+=num[x];
}
return;
}
int main()
{
int n,m;
int a,b,k;
int i,j;
freopen("in.txt","r",stdin);
while(scanf("%d%d",&n,&m))
{
if(n==&&m==)
break;
for(i=;i<Max;i++)
{
par[i]=i;
num[i]=;
}
for(i=;i<m;i++)
{
scanf("%d",&k);
for(j=;j<k;j++)
{
if(j==)
scanf("%d",&a);
else
{
scanf("%d",&b);
unite(a,b);
}
}
}
printf("%d\n",num[find()]);
}
}

The Suspects(POJ 1611 并查集)的更多相关文章

  1. poj 1611(并查集)

    http://poj.org/problem?id=1611 题意:有个学生感染病毒了,只要是和这个学生接触过的人都会感染,而和这些被感染者接触的人,也会被感染,现在给定你一些协会的人数,以及所在学生 ...

  2. POJ 1611(并查集+知识)

    并查集主要是两个过程,一个是并,一个是查 原理是用一个数组p[i]保存每个i的根节点,如果根节点一样则在同一个集合里,所以只有根节点p[i]=i; 查: int find(int x){return ...

  3. POJ 1611并查集

    我发现以后写题要更细心,专心! #include<iostream>#include<algorithm>#include<stdio.h>#include< ...

  4. poj 1984 并查集

    题目意思是一个图中,只有上下左右四个方向的边.给出这样的一些边, 求任意指定的2个节点之间的距离. 就是看不懂,怎么破 /* POJ 1984 并查集 */ #include <stdio.h& ...

  5. poj 1611 :The Suspects经典的并查集题目

    Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized ...

  6. C - The Suspects POJ - 1611(并查集)

    Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized ...

  7. 【POJ】The Suspects(裸并查集)

    并查集的模板题,为了避免麻烦,合并的时候根节点大的合并到小的结点. #include<cstdio> #include<algorithm> using namespace s ...

  8. poj 1797(并查集)

    http://poj.org/problem?id=1797 题意:就是从第一个城市运货到第n个城市,最多可以一次运多少货. 输入的意思分别为从哪个城市到哪个城市,以及这条路最多可以运多少货物. 思路 ...

  9. POJ 2492 并查集扩展(判断同性恋问题)

    G - A Bug's Life Time Limit:10000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u S ...

随机推荐

  1. C语言初学 计算三角形面积问题

    #include<stdio.h> #include<math.h> #include<stdlib.h> int main() { float a,b,c,s,a ...

  2. Kafka笔记--监控系统KafkaOffsetMonitor

    KafkaOffsetMonitor下载链接: http://download.csdn.net/detail/changong28/7930337github官方:https://github.co ...

  3. 职员时序安排lingo求解

    大家好,我是小鸭酱,博客地址为:http://www.cnblogs.com/xiaoyajiang !职员时序安排模型 题目: 一项工作一周七天都需要有人,每天所需的最少职工数为20,16,13,1 ...

  4. Matlab基本数据类型

    本文转载自:http://hi.baidu.com/xmf6227/blog/item/97ca2ddf98f1b61f495403cb.html Matlab中有15种基本数据类型,主要是整型.浮点 ...

  5. android 反编译操作

    1:首先将apk程序解压: 2:将classes.dex反编译为jar包:$ ./dex2jar/dex2jar.sh classes.dex 将生成一个新的文件classes_dex2jar.jar ...

  6. jQuery 弹出div层

    目的:使用jQuery弹出一个div窗口:这种效果经常应用于页面内容的展示,登录效果的实现.其实,实现这种效果有好多种方式: 效果如下: 代码如下: <html> <head> ...

  7. Bring it on

    I am going to open a whole new English Blog here. Most blogs here would be computer technologies, in ...

  8. 【hihoCoder第十四周】无间道之并查集

    就是基础的并查集.0代表合并操作,1代表查询操作.一开始以为会卡路径压缩,忐忑的交了一版裸并查集,结果AC了.数据还是很水的. 以后坚持做hiho,当额外的练习啦~ #include <bits ...

  9. SVN 一次性提交多个目录中文件

    情况一:将项目中未加入版本控制的文件提交到版本库. 在使用WINDOW下的SVN客户端工具时,在提交一个项目的文件时,如果有未加入版本库的文件,这时可以先将未加入的文件选中,然后一起提交. 但在LIN ...

  10. python linecache标准库基础学习

    #python标准库基础之:linecacge:高效读取文本文件#说明与作用"""可以从文件或者导入python模块获取文件,维护一个结果缓存,从而可以更高效地从相同文件 ...