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. epoll函数及三种I/O复用函数的对比

    epoll函数 #include <sys/epoll.h>int epoll_create(int size)int epoll_ctl(int epfd, int op, int fd ...

  2. lua好久没有用了

    lua好久没有用了, 最近用python有点多,现在用web.py搞个简单的页面来作通讯录,没美工,很纯白的, 无聊啊,准备去睡了,刚刚百度完lua的一些资料呢, google用不了,真悲剧啊, TM ...

  3. My advice to young people - Donald Knuth [video]

    http://www.youtube.com/watch?v=75Ju0eM5T2c I took a note of what knuth said in the video. 1. Don't d ...

  4. Android从相册读取图片

    Uri originalUri = data.getData();        //获得图片的uri  bm = MediaStore.Images.Media.getBitmap(resolver ...

  5. 面试题 41 和为s的两个数字VS 和为S的连续整数序列

    (1)和为S的两个数字 bool findNumberWithSum(int data[], int length, int sum, int &numb1, int &numb2){ ...

  6. 为什么memset的第二个参数不把int替换成char

    memset是一个经常被用来初始化数组的函数,其定义如下: 1 void * memset ( void * ptr, int value, size_t num ); 它的效果大致是把以ptr为起始 ...

  7. windows下的用户态调试的底层与上层实现

    操作系统:windows XP 调试器通过CreateProcess传入带有DEBUG_PROCESS和DEBUG_ONLY_THIS_PROCESS的dwCreationFlags创建被调试进程.这 ...

  8. authbind start tomcat services as user with less that 1024 ports. linux常规用户使用tomcat的80端口

    Start tomcat services using authbind this will allow user to start ports less than 1024 we do not ne ...

  9. H5页面音频自动播放问题

        最近有这么一个需求,需要在手机加载一个页面的时候,自动播放音乐资源.一般情况下,这个问题也就解决了,但是要保证各种手机上表现一致,那就相当困难了,至少要费点儿周折.       下面有三种常规 ...

  10. thinkphp连接mysql5.5版本数据库

    //数据库配置信息 'DB_TYPE' => 'mysqli', // 数据库类型 'DB_HOST' => 'localhost', // 服务器地址 'DB_NAME' => ' ...