The Suspects
Time Limit: 1000MS   Memory Limit: 20000K
Total Submissions: 35918   Accepted: 17458

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
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 ;
}
 

poj1611的更多相关文章

  1. 并查集 poj1611&poj2492

    poj1611 简单题 代码中id记录父节点,sz记录子树规模.一个集合为一棵树. #include <iostream> #include <cstdio> using na ...

  2. poj1611 The Suspects(并查集)

    题目链接 http://poj.org/problem?id=1611 题意 有n个学生,编号0~n-1,m个社团,每个社团有k个学生,如果社团里有1个学生是SARS的疑似患者,则该社团所有人都要被隔 ...

  3. poj1611(并查集)

    题目链接:http://poj.org/problem?id=1611 题意: SARS(非典型肺炎)传播得非常厉害,其中最有效的办法是隔离那些患病.和患病者接触的人.现在有几个学习小组,每小组有几个 ...

  4. poj1611 并查集 (路径不压缩)

    http://poj.org/problem?id=1611 题目大意: 有一个学校,有N个学生,编号为0-N-1,现在0号学生感染了非典,凡是和0在一个社团的人就会感染,并且这些人如果还参加了别的社 ...

  5. poj1611(感染病患者)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 24587   Accepted: 12046 De ...

  6. poj1611 带权并查集

    题意:病毒蔓延,现在有 n 个人,其中 0 号被认为可能感染,然后给出多个社交圈,如果某个社交圈里有人被认为可能被感染,那么所有这个社交圈里的人都被认为可能被感染,现在问有多少人可能被感染. 带权并查 ...

  7. poj-2236 Wireless Network &&poj-1611 The Suspects && poj-2524 Ubiquitous Religions (基础并查集)

    http://poj.org/problem?id=2236 由于发生了地震,有关组织组把一圈电脑一个无线网,但是由于余震的破坏,所有的电脑都被损坏,随着电脑一个个被修好,无线网也逐步恢复工作,但是由 ...

  8. 暑假集训(2)第二弹 ----- The Suspects(POJ1611)

    B - The Suspects Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:20000KB   ...

  9. poj1611 并查集

    题目链接:http://poj.org/problem?id=1611 #include <cstdio> #include <cmath> #include <algo ...

  10. poj1611 简单并查集

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 32781   Accepted: 15902 De ...

随机推荐

  1. DateFormat 竟然是非线程安全的?!!!!!

    今天撸代码忽然发现一个奇怪的一场抛出,经过一番排查发现有可能DateFormat 的多线程问题造成的,网上一查DateFormat竟然非线程安全.那我原先的代码...(细思极恐)

  2. UVa 369 - Combinations

    题目大意:给两个数n, m,求C(n, m).用java直接写就好了. import java.io.*; import java.util.*; import java.math.*; class ...

  3. Oracle数据库常用技术

    一.视图(重点) 什么是视图? ·视图由一个或多个表(或视图)中提取数据而成 ·视图是一种虚拟表 ·视图一经创建,可以当作表来使用. 使用视图的好处? · 简化复杂数据查询 · 提高运行效率 · 屏蔽 ...

  4. 升级R版本后,更新Package

    升级R版本后,若重新安装所有的package将非常麻烦,可以尝试运行一下程序: 1)在旧版本中的R中运行 #--run in the old version of R setwd("C:/T ...

  5. Chrome Timeline的指标说明:Blocked、Connect、Send、Wait、Receive

    Blocked time includes any pre-processing time (such as cache lookup) and the time spent waiting for ...

  6. [UT]Unit Test理解

    Coding中有一个原则:Test Driven Development.  UT中的一些基本概念: 1. 测试驱动 2. 测试桩 3. 测试覆盖 4. 覆盖率 单体测试内容: 1. 模块接口:测试模 ...

  7. 纯代码实现CSS圆角

    我这里说的是纯代码,是指的不使用图片实现圆角,图片实现圆角,这里就不说了. 纯代码实现圆角主要有3种方法: 第一种:CSS3圆角   #chaomao{     border-radius:2px 2 ...

  8. redis sets类型及操作

    sets类型及操作set是集合,它是string类型的无序集合.通过hash table实现,添加.删除.查找的复杂度都是0(1).对集合我们可以实现取交际.差集并集.通过这些操作我们可以实现SNS中 ...

  9. JSP模板文本

    JSP模板文本: http://book.51cto.com/art/200907/136020.htm JSP页面就是带有JSP元素的常规Web页面,它是由JSP模版文本和JSP元素组成的.在一个J ...

  10. 迈向新纪元编辑器Atom

    第一次听说Atom是半年前天猫的学姐说她在用这款这款编辑器,期间我从dw到st再到webstorm都分别做了项目~,但是作为一名拥抱开源的FE,怎么能拒绝github社区维护的项目呢?接着就让我们感受 ...