POJ1161——The Suspects

 
Time Limit: 1000MS   Memory Limit: 20000K
Total Submissions: 48220   Accepted: 23072

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

 
题目大意:
 
n个学生分属m个团体,(0 < n <= 30000 , 0 <= m <= 500) 一个学生可以属于多个团体。 一个学生疑似患病,则它所属的整个团体都 疑似患病。已知0号学生疑似患病,以及每个 团体都由哪些学生构成,求一共多少个学生 疑似患病。
 
解法:
 
最基础的并查集,把所有可疑的都并一 块。
 
#include<iostream>
#include<cstdio>
#define N 4000000
#define LL long long
#define RE register
#define IN inline using namespace std; IN void in(int &x) {
RE char c=getchar();x=;int f=;
while(!isdigit(c)) {if(c=='-') f=-;c=getchar();}
while(isdigit(c)) {x=x*+c-'';c=getchar();}
x*=f;
} int n,m,fa[N],w[N],anss,ans; int find(int x){return fa[x]==x?x:find(fa[x]);} int main()
{
while(){
in(n);in(m);
if(n==&&m==) return ;
for(int i=;i<=n;i++) fa[i]=i;
for(int i=;i<=m;i++){
int tp;
in(tp);
for(int j=;j<=tp;j++){
int fx,fy;in(w[j]);
fx=find(w[j]);
for(int k=;k<j;k++){
fy=find(w[k]);
if(fx!=fy) fa[fx]=fy;
}
}
}ans=find();anss=;
for(int i=;i<=n;i++){
if(find(i)==ans) ++anss;
}printf("%d\n",anss);
}return ;
}

  

 

POJ1161——The Suspects的更多相关文章

  1. [并查集] POJ 1611 The Suspects

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

  2. poj-1611-The Suspects

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 34284   Accepted: 16642 De ...

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

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

  4. DP(记忆化搜索) + AC自动机 LA 4126 Password Suspects

    题目传送门 题意:训练指南P250 分析:DFS记忆化搜索,范围或者说是图是已知的字串构成的自动机图,那么用 | (1 << i)表示包含第i个字串,如果长度为len,且st == (1 ...

  5. Poj1611The Suspects

    A - The Suspects Time Limit: 1000 MS Memory Limit: 20000 KB 64-bit integer IO format: %I64d , %I64u  ...

  6. poj 1611 The Suspects 并查集

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

  7. LA 4126 Password Suspects

    问题描述:给定m个模式串,计数包含所有模式串且长度为n的字符串的数目. 数据范围:模式串长度不超过10,m <= 10, n <= 25,此外保证答案不超过1015. 分析:既然要计数给定 ...

  8. POJ 1611 The Suspects (并查集)

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

  9. 暑假集训(2)第二弹 ----- The Suspects(POJ1611)

    B - The Suspects Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:20000KB   ...

随机推荐

  1. LeetCode 788. Rotated Digits (旋转数字)

    X is a good number if after rotating each digit individually by 180 degrees, we get a valid number t ...

  2. UVA - 10691 Subway

    题目大意:给定n个点.要求建造尽量少得铁路(从原点发射出的射线).使得全部点到铁路的最短距离小于d. 解题思路:题目能够转化成区间选点问题,即以极角来表示铁轨.然后计算出每一个区间可行的极角范围,进行 ...

  3. intellij idea 写 Helloworld

    http://www.jetbrains.com/idea/webhelp/creating-and-running-your-first-java-application.html Creating ...

  4. IT痴汉的工作现状22-由Dalvik虚拟机引发的口水战

    Dalvik是啥呢? 从未知道冰岛有Dalvik这么一个重要的村庄.直到Dan Bornstein将自己为Android系统编写的进程虚拟机命名为Dalvik后才被我所知. 它是Android系统独有 ...

  5. bootstrap table 显示连续序号,分页有效

    参考:http://blog.csdn.net/nazhidao/article/details/51647799 第一种,通过index+1可以简单实现,但是,翻页后又重新从第一个开始 { fiel ...

  6. Linux下的IPC-UNIX Domain Socket【转】

    本文转载自:http://blog.csdn.net/guxch/article/details/7041052 一. 概述 UNIX Domain Socket是在socket架构上发展起来的用于同 ...

  7. YTU 2706: 编写一个函数求最大的n值

    2706: 编写一个函数求最大的n 值. 时间限制: 1 Sec  内存限制: 128 MB 提交: 341  解决: 132 题目描述 编写一个函数求满足以下条件的最大的n.:12+22+32+-+ ...

  8. JSP-Runoob:JSP 自动刷新

    ylbtech-JSP-Runoob:JSP 自动刷新 1.返回顶部 1. JSP 自动刷新 想象一下,如果要直播比赛的比分,或股票市场的实时状态,或当前的外汇配给,该怎么实现呢?显然,要实现这种实时 ...

  9. SpringMVC+MyBaties关于上传(跟新)图片的问题

    /** * 方法名:uploadPhoto * 描 述:TODO(上传图片) * 作 者:池彦龙 * 时 间:2017/03/30 * 返回类型: * 参 数: * 异 常: */ Controlle ...

  10. 【WIP】客户端JavaScript DOM

    创建: 2017/10/12 初步完成: 2017/10/15   更新: 2017/10/14 标题加上[WIP],继续完成     [TODO] 补充暂略的, 搜[略]  DOM树  概要  基本 ...