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

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
#include<iostream>
#include<stdio.h>
#define max1 30005
using namespace std;
int pa[max1];
int find(int a)
{
while(a!=pa[a])
{
pa[a]=pa[pa[a]];
a=pa[a];
}
return a;
}
void bild(int a,int b)
{
int fa=find(a);
int fb=find(b);
if(fa!=fb)
{
pa[fb]=fa;
}
}
int main()
{
int n,m;
while((scanf("%d%d",&n,&m))&&(n+m!=))
{
for(int i=;i<n;i++)
{
pa[i]=i;
} for(int i=;i<m;i++)
{
int p1,p2;
int t;
cin>>t>>p1;
for(int j=;j<t-;j++)
{
cin>>p2;
bild(p1,p2);
}
}
int ans=;
for(int i=;i<n;i++)
{
if(find()==find(i)) ans++;
}
cout<<ans<<endl;
}
return ;
}

这个我昨天写过博客了,但是今天做了一点点改动,就是根节点的设定变成任意的了,这样在最后搜索的时候要查找所有和 0  的根节点相同的节点。最后注意一点的是,0号同学也是一个人,不要忘了把他算入最后的结果里面去。

Sample Output

4
1
1

poj 1611 The Suspects 并查集的更多相关文章

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

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

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

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

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

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

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

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

  5. POJ 1611 The Suspects 并查集 Union Find

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

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

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

  7. 并查集 (poj 1611 The Suspects)

    原题链接:http://poj.org/problem?id=1611 简单记录下并查集的模板 #include <cstdio> #include <iostream> #i ...

  8. [并查集] POJ 1611 The Suspects

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 35206   Accepted: 17097 De ...

  9. poj 1611:The Suspects(并查集,经典题)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 21472   Accepted: 10393 De ...

随机推荐

  1. delete this及堆破坏检测方法

    作者: Bruce   日期: 2012年06月03日 04:20 周日 发表评论 (0) 查看评论 --END*1--> 0 条评论 --END*2-->1,837 人阅读   程序BU ...

  2. WebRequest中的工厂方法模式

  3. 开发jquery tab 插件

    开发最简单的效果- -,基本构架 html,可以换更有意义的结构,这里demo,就简单写,不考虑SEO <div id="tab-hd"> <div class= ...

  4. hiho一下第88周《Coordinates》

    题目大意 给定数字P,Q,求出所有P和Q的约数p,q能够组成的不重复数字对(p,q) 解题思路 作为本场比赛的第一题,本题的难度在于考察选手是否有基本的编程能力. 本题中需要求出P,Q所有约数组成的数 ...

  5. C++简单使用Jsoncpp来读取写入json文件

    一.源码编译 C++操作json字符串最好的库应该就是jsoncpp了,开源并且跨平台.它可以从这里下载. 下载后将其解压到任意目录,它默认提供VS2003和VS2010的工程文件,使用VS2010可 ...

  6. 序列GCD和问题(题目)

    序列GCD和 题目描述 Massacc有一个序列$A_1,A_2,A_3,\dots ,A_n$. Popbab说:我要知道这个序列的和$\pmod{1\times10^9+7}$. Massacc在 ...

  7. Windows上搭个Nginx集群环境玩玩

    一.在windows上安装nginx 1.从这里下载nginx的windows版本 2.把压缩文件解压至c盘根目录,并将文件夹重命名成nginx 3.在conf目录下的nginx.conf文件中,指定 ...

  8. Search a 2D Matrix | & II

    Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix, ret ...

  9. jlink安装

    https://www.segger.com/jlink-software.html?step=1&file=JLink_510p

  10. poj 1664

    http://poj.org/problem?id=1664 题目是中文的,一个递归的题目 把每一次的苹果分为两类 Ⅰ:所以盘子都放一个,然后其他的在随便放: Ⅱ:有一个盘子没有放苹果: 这样下去的话 ...