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. CocoStudio基础教程(1)创建UI并载入到程序中

    1.概述 CocoStudio的使用无疑是cocos2d-x 3.0的重要组成部分,接下来我们用它来创建一组UI,并将其读入到程序中显示出来.先上效果图: 2.导出 在导出之前,最好先创建一个新的工程 ...

  2. PV公式

    IP(独立IP):  即Internet Protocol,指独立IP数.00:00-24:00内相同IP地址之被计算一次.PV(访问量):  即Page View, 即页面浏览量或点击量,用户每次刷 ...

  3. iOS团队开发者测试

    那么你需要在你下载证书的那个电脑上从钥匙串-->选择证书-->右键到处证书,保存为.p12的证书,以后这个证书拷贝到任何电脑上去都是可以使用的! 本来只有一台电脑可以测试, 现在要团队开发 ...

  4. 入侵检测课设之Libnids开发包

    Libnids开发包介绍     Libnids是一个用于网络入侵检测开发的专业编程接口,它使用了Libpcap所以它具有捕获数据包的功能.同时,Libnids提供了TCP数据流重组功能,所以对于分析 ...

  5. django-cms 代码研究(五)深入(代码结构)

    前言: 前戏已经做得比较充分了,下面我们开始步入正题. 代码结构: cms |--admin (猜测是admin界面的二次开发和改良) |--cache (猜测是缓存机制的处理) |--extensi ...

  6. 15 day 1代碼

    第一题 用堆维护. #include <cstdio> #include <algorithm> #include <queue> int n,i,f[400000 ...

  7. Controller之间传递数据:协议传值

    http://itjoy.org/?p=416 前边介绍过从第一个页面传递数据到第二个页面,那么反过来呢我们该如何操作?还是同一个例子,将第二个页面的字符串传递到第一个页面显示出来,这中形式就可以使用 ...

  8. poj 1664

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

  9. 【转】windows下安装和调用curl的方法

    本文转自:http://1316478764.iteye.com/blog/2100778 curl是利用URL语法在命令行方式下工作的开源文件传输工具.它支持很多协议:FTP, FTPS, HTTP ...

  10. sqlserver的执行计划

    一:执行计划生成过程 说到执行计划,首先要知道的是执行计划大概生成的过程,这样就可以做到就心中有数了,下面我画下简图: 1. 分析过程 这三个比较容易理解,首先我们要保证sql的语法不能错误,sele ...