The Suspects
Time Limit: 1000MS   Memory Limit: 20000K
Total Submissions: 18890   Accepted: 9150

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

Sample Output

4
1
1

Source

代码:
#include<iostream>
#include<cstdio>
#define maxn 30002
using namespace std;
int Father[maxn],rank[maxn];
void makeset(int n) //初始化
{
for(int i=;i<n;i++)
{
Father[i]=i; //以自身做根
rank[i]=; //开始高度为1
}
}
int findset(int x)
{
if(x!=Father[x])
{
Father[x]=findset(Father[x]);
}
return Father[x];
} void unionset(int fx,int fy)
{
fx=findset(Father[fx]);
fy=findset(Father[fy]);
if(fx==fy)
return ;
if(rank[fx]>rank[fy])
{
Father[fy]=fx;
rank[fx]+=rank[fy];
}
else
{
Father[fx]=fy;
rank[fy]+=rank[fx];
}
}
int main()
{
int n,m,k,st,num;
while(scanf("%d%d",&n,&m),n+m)
{
makeset(n);
while(m--)
{
scanf("%d%d",&k,&st);
while(--k)
{
scanf("%d",&num);
unionset(st,num);
}
}
printf("%d\n",rank[findset()]);
}
return ;
}

POJ----The Suspects的更多相关文章

  1. POJ——1611The Suspects(启发式并查集+邻接表)

    The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 31100 Accepted: 15110 Descri ...

  2. 【原创】poj ----- 1611 The Suspects 解题报告

    题目地址: http://poj.org/problem?id=1611 题目内容: The Suspects Time Limit: 1000MS   Memory Limit: 20000K To ...

  3. [并查集] POJ 1611 The Suspects

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

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

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

  5. poj 1611 The Suspects 解题报告

    题目链接:http://poj.org/problem?id=1611 题意:给定n个人和m个群,接下来是m行,每行给出该群内的人数以及这些人所对应的编号.需要统计出跟编号0的人有直接或间接关系的人数 ...

  6. poj 1611 The Suspects 并查集

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

  7. POJ 1611 The Suspects (并查集)

    The Suspects 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/B Description 严重急性呼吸系统综合症( S ...

  8. poj 1611 The Suspects(简单并查集)

    题目:http://poj.org/problem?id=1611 0号是病原,求多少人有可能感染 #include<stdio.h> #include<string.h> # ...

  9. The Suspects(POJ 1611 并查集)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 30158   Accepted: 14665 De ...

  10. poj 1611 The Suspects(并查集)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 21598   Accepted: 10461 De ...

随机推荐

  1. css3 transform方法常用属性

    css3中transform方法是一个功能强大的属性,可以对元素进行移动.缩放.转动.拉长或拉伸等功能. transform中最为常用的4个属性分别是:rotate();.scale();.skew( ...

  2. OpenCV 脸部跟踪(2)

          前面一篇文章中提到,我们在一副脸部图像上选取76个特征点,以及这些特征点的连通性信息来描述脸部形状特征,本文中我们会把这些特征点映射到一个标准形状模型.       通常,脸部形状特征点能 ...

  3. Longest Consecutive Sequence leetcode java

    题目: Given an unsorted array of integers, find the length of the longest consecutive elements sequenc ...

  4. C#中HTML和UBB互相转换的代码

    C#中HTML和UBB互相转换的代码html转UBB的还不是很完美,有空修改,一些代码来自百度谷歌 private string DoHtmlToUBB(string _Html)        {  ...

  5. 【理解】column must appear in the GROUP BY clause or be used in an aggregate function

    column "ms.xxx_time" must appear in the GROUP BY clause or be used in an aggregate functio ...

  6. dev -c++ 快捷键

    转自:http://blog.csdn.net/abcjennifer/article/details/7259222 F8:开始调试 F7:进一步执行当前行,并跳到下一行 F4:添加查看 ctrl ...

  7. OSX:不同OSX版本号的标记可能不兼容-续

    不同OSX版本号的标记可能不兼容-续: 经过測试,10.10DP2的Update.俗称DP3.的版本号也没有纠正这个问题.而造成该问题的是安装过程中一開始就选择中文,假设安装时使用英文.在第一次进入操 ...

  8. PHP的CLI综合

    tip1:传入参数 使用标准的输入和输出    PHP CLI会定义三个常量,以便让在命令行提示符下与解释器进行交互操作更加容易.这些常量见表格A.表格A 常量 说明STDIN 标准的输入设备STDO ...

  9. uni-app 生命周期

    生命周期分为:页面生命周期和应用生命周期 生命周期可参考:uni-app官方API 注意平台支持,仅某个平台支持会显示,5+App是超HTML5+的App方案. 例如分享:只有小程序支持.这时我们就要 ...

  10. win10系统80端口被占用怎么办?

    win10系统80端口被占用怎么办? 因为win10默认开启了IIS因此占用了80端口,可以netstat –ano 查到. 运行netstat -aon | findstr :80 ,发现pid是4 ...