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. vmware里面的linux怎么和windows相互传文件

    我们常常遇到这样的问题.高版本号的vmware遇到低版本号的linux.使用起来就比較抠脚,比方低版本号的linux安装在高版本号的vmware里. 1.不能全屏显示虚拟机 2.每次切换出来.总要按一 ...

  2. ios 仿android gallery控件

    ios 上没有发现与android gallery类似的控件,因为在项目上须要使用到.採用UICollectionView实现 watermark/2/text/aHR0cDovL2Jsb2cuY3N ...

  3. LeetCode 7. Reverse Integer (倒转数字)

    Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...

  4. Android开发之策略模式初探

            策略模式主要定义一系列的算法,学过数据结构的朋友肯定知道,对于数组从大到小进行排序有着非常多的算法.比方冒泡.交换.高速插入等等,策略模式就是把这些算法封装成一个个独立的类.方便使用时 ...

  5. PhoneGap:JS跨域请求

    PhoneGap开发,理论上好处多多.但因为javascript是其中的主角,并且是直接存放于手机,跟服务器数据交互,就会有一个跨域访问的问题. 当然,这个问题肯定有解决方案,不然的话,这种利用Pho ...

  6. Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/util/POILogFactory

    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/util/POILogFacto ...

  7. cas4.2的安装

    cas4.2使用的是gradle来构建项目的,项目代码在https://github.com/Jasig/cas下载. 然后之后进入项目的根目录,然后执行gradle来编译项目,如下: gradle ...

  8. poj1201 Intervals——差分约束

    题目:http://poj.org/problem?id=1201 差分约束裸题: 设 s[i] 表示到 i 选了数的个数前缀和: 根据题意,可以建立以下三个限制关系: s[bi] >= s[a ...

  9. kubernetes Traefik ingress配置详解

    理解Ingress 简单的说,ingress就是从kubernetes集群外访问集群的入口,将用户的URL请求转发到不同的service上.Ingress相当于nginx.apache等负载均衡方向代 ...

  10. E20171015-hm

    quirk   n. 怪癖; 奇事,巧合; 突然的弯曲; propagation  n. 宣传; 传播,传输,蔓延,扩展,波及深度; [生]繁殖法,[地]传导; 培养; immediate  adj. ...