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. Java学习笔记--“==”与"equals"

    java中的数据类型,可分为两类: 1. 基本数据类型,也称原始数据类型.byte,short,char,int,long,float,double,boolean 他们之间的比较,应用双等号(==) ...

  2. ResourceString的用法

    在Delphi编程的那段“古老”的日子里(就是在版本4之前),在程序中使用字符串有两个基本的方法.你可以使用字符串将它们嵌入到源程序中,例如: MessageDlg( 'Leave your stin ...

  3. Python 在Visual studio 中做单元测试进行TDD开发

    Unit Tests Steve Dower edited this page on 14 Jul · 3 revisions Pages 38 Home Azure Remote Debugging ...

  4. Xmind 快捷键

    Xmind 快捷键 快捷键(Windows) 快捷键(Mac) 描述 Ctrl+N Command+N 建立新工作簿 Ctrl+O Command+O 开启工作簿 Ctrl+S Command+S 储 ...

  5. 处理通过<input type="file">的Post 请求

    [HttpPost] public ActionResult Cal() { string ExcelName = System.DateTime.Now.ToString("yyyyMMd ...

  6. Android实现摇晃手机的监听

     摘自:http://blog.csdn.net/xwren362922604/article/details/8515343 监听摇晃手机的类: /**  * @author renxinwei ...

  7. poj1305:概念水题

    了解一下毕达哥拉斯三元组概念= = 暴力求出所有的本源三元组即可 代码: #include <iostream> #include <stdio.h> #include< ...

  8. 算法导论(第三版)Exercies2.2(插入排序)

    2.2-1: Θ (n3) 2.2-2:插入排序 void selectionSort(int a[], int n) { int i, j, k, key; ; i<n-; i++) { k ...

  9. XSS初体验

    主要内容 什么是XSS? XSS的危害有哪些? 常见的XSS漏洞 如何防范XSS?   什么是XSS? 跨站脚本攻击(Cross Site Scripting),是一种 Web 应用程序的漏洞,当来自 ...

  10. USB枚举详细过程剖析(转)

    USB枚举详细过程剖析(转) 原文地址:http://blog.163.com/luge_arm/blog/static/6774972620071018117290/ 从驱动开发网看到一篇<U ...