The Suspects

Time Limit: 1000MS   Memory Limit: 20000K
Total Submissions: 39211   Accepted: 18981

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

 
 //2017-07-19
#include <iostream> using namespace std; const int N = ;
int n, m, fa[N]; void init(){
for(int i = ; i < N; i++)
fa[i] = i;
} int getfa(int x){
if(x == fa[x])return x;
else return fa[x] = getfa(fa[x]);
} void merge0(int a, int b){
int af = getfa(a);
int bf = getfa(b);
if(af != bf)
fa[bf] = af;
} int main(){
while(cin>>n>>m){
if(!n && !m)break;
init();
int k, a, b;
while(m--){
cin>>k>>a;
for(int i = ; i < k-; i++){
cin>>b;
merge0(a, b);
}
}
int ans = ;
for(int i = ; i < n; i++){
if(getfa(i) == fa[])
ans++;
}
cout<<ans<<endl;
} return ;
}

POJ1611(KB2-B)的更多相关文章

  1. 并查集 poj1611&poj2492

    poj1611 简单题 代码中id记录父节点,sz记录子树规模.一个集合为一棵树. #include <iostream> #include <cstdio> using na ...

  2. poj1611 The Suspects(并查集)

    题目链接 http://poj.org/problem?id=1611 题意 有n个学生,编号0~n-1,m个社团,每个社团有k个学生,如果社团里有1个学生是SARS的疑似患者,则该社团所有人都要被隔 ...

  3. poj1611(并查集)

    题目链接:http://poj.org/problem?id=1611 题意: SARS(非典型肺炎)传播得非常厉害,其中最有效的办法是隔离那些患病.和患病者接触的人.现在有几个学习小组,每小组有几个 ...

  4. poj1611 并查集 (路径不压缩)

    http://poj.org/problem?id=1611 题目大意: 有一个学校,有N个学生,编号为0-N-1,现在0号学生感染了非典,凡是和0在一个社团的人就会感染,并且这些人如果还参加了别的社 ...

  5. poj1611(感染病患者)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 24587   Accepted: 12046 De ...

  6. poj1611 带权并查集

    题意:病毒蔓延,现在有 n 个人,其中 0 号被认为可能感染,然后给出多个社交圈,如果某个社交圈里有人被认为可能被感染,那么所有这个社交圈里的人都被认为可能被感染,现在问有多少人可能被感染. 带权并查 ...

  7. poj-2236 Wireless Network &&poj-1611 The Suspects && poj-2524 Ubiquitous Religions (基础并查集)

    http://poj.org/problem?id=2236 由于发生了地震,有关组织组把一圈电脑一个无线网,但是由于余震的破坏,所有的电脑都被损坏,随着电脑一个个被修好,无线网也逐步恢复工作,但是由 ...

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

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

  9. poj1611 并查集

    题目链接:http://poj.org/problem?id=1611 #include <cstdio> #include <cmath> #include <algo ...

  10. poj1611 简单并查集

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 32781   Accepted: 15902 De ...

随机推荐

  1. 题解 P5091 【【模板】欧拉定理】

    欧拉定理:若 \(gcd(a,n)=1\),\(a^{\varphi(n)}\equiv 1(mod\ n)\) 设 \(1\sim n-1\) 中与 \(n\) 互素的 \(\varphi(n)\) ...

  2. Web安全测试学习手册-业务逻辑测试

    i春秋作家:Vulkey_Chen 首先感谢朋友倾璇的邀请 http://payloads.online/archivers/2018-03-21/1 ,参与了<web安全测试学习手册>的 ...

  3. Java实时监控类库Metrics

    随着系统越来越大,越来越复杂,我们需要在业务方面加上一些监控服务.Metrics作为一款监控指标的度量类库,提供了许多工具帮助开发者来完成自定义的监控工作. 使用Metrics 使用Metrics,只 ...

  4. vue教程2-04 vue实例简单方法

    vue教程2-04 vue实例简单方法 vue实例简单方法: vm.$el -> 就是元素 vm.$data -> 就是data <!DOCTYPE html> <htm ...

  5. 使用 IntelliJ IDEA 导入 Spark 最新源码及编译 Spark 源代码(博主强烈推荐)

    前言   其实啊,无论你是初学者还是具备了有一定spark编程经验,都需要对spark源码足够重视起来. 本人,肺腑之己见,想要成为大数据的大牛和顶尖专家,多结合源码和操练编程. 准备工作 1.sca ...

  6. 全网最详细的PLSQL Developer + Oracle client的客户端 或者 PLSQL Developer + Oracle server服务端的下载与安装过程(图文详解)

    不多说,直接上干货! 环境说明: 本地没有安装Oracle服务端,oracle服务端64位,是远程连接,因此本地配置PLSQL Developer64位. Oracle database使用在本机部署 ...

  7. 每一行代码都有记录—如何用git一步步探索项目的历史

    每一行代码都有一块被隐藏了的文档信息. 下面的代码片段不管是谁写的,其第4行因为某些原因要访问一个DOM结点的clientLeft属性,但却对结果不作任何处理.这十分的莫名其妙,你能告诉我他们为什么要 ...

  8. KVM部署及硬件参数更改

    一.kvm虚拟化环境搭建准备 1.硬件环境 kvm只能部署在物理机上面. 2.BIOS开启VT Virtualization Tech [Enabled] 3.查看cpu 是否支持kvm 全虚拟化 # ...

  9. Java 容器源码分析之HashMap多线程并发问题分析

    并发问题的症状 多线程put后可能导致get死循环 从前我们的Java代码因为一些原因使用了HashMap这个东西,但是当时的程序是单线程的,一切都没有问题.后来,我们的程序性能有问题,所以需要变成多 ...

  10. study design of ADNI

    AD(Alzheimer’s disease):不可逆的神经退化,患病人员会由于脑部问题的恶化而导致心智功能不健全. ADNI:阿尔茨海默氏症神经成像项目 ADNI的总体目标是验证用于阿尔茨海默病临床 ...