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. 点击label时click事件被触发两次的坑

    今天帮群里的朋友看一段代码的时候偶然间遇到一个label的坑,点击label的时候,监听的click事件被执行两次: 具体代码如下: <div id="test"> & ...

  2. kindle paperwhite折腾记

    在亚马逊官网上买了一个kindle paperwhite 一代(849元) , 打算再买个皮套, 淘宝店  http://detail.tmall.com/item.htm?spm=a230r.1.1 ...

  3. FineUI第二天

    原博文http://www.cnblogs.com/sanshi/archive/2012/02/12/2347789.html 1.首先复制extJS的文件夹到根目录. 2.引用程序集 3.配置配置 ...

  4. 基于2d Tool Kit 精灵合图,动作生成工具

    http://blog.csdn.net/onerain88/article/details/18563687 2d Tool Kit 是一款出色的基于unity3d 开发2d游戏的工具,提供了丰富的 ...

  5. svn提交时强制添加注释 (转)

    SVN提交时,如果没有注释,在查阅历史时,会非常不方便.因此我们需要有一个让程序员提交代码时,强制添加注释的规则.下面看看在SVN中怎么实现. 1. 推荐使用VisualSVN作为服务端(免费下载地址 ...

  6. glut64位操作系统安装

    64位win7下OpenGL的配置 - walkandthink的专栏 - 博客频道 - CSDN.NEThttp://blog.csdn.net/walkandthink/article/detai ...

  7. python如何安装pip和easy_installer工具

    1.在以下地址下载最新的PIP安装文件:http://pypi.python.org/pypi/pip#downloads 2.解压安装 3.下载Windows的easy installer,然后安装 ...

  8. 配置oss bucket cors

    到bucket中属性中选择跨越设置,点击添加规则会看到以下界面: 对应的输入如上即可.

  9. WCDMA是什么意思?CDMA是什么意思?GSM是什么意思

    有些朋友在购买3G智能手机的时候会遇到这样的困惑,为什么相同的手机会有不同手机网络制式之分呢?有的支持WCDMA/GSM,有的支持CDMA/GSM,到底自己应该选购哪一种手机好呢?WCDMA是什么意思 ...

  10. 【原创】Mapped Statements collection does not contain value for DaoImpl.method

    问题: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.Pers ...