The Suspects

Time Limit: 1000MS Memory Limit: 20000K
Total Submissions: 36817 Accepted: 17860

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

Asia Kaohsiung 2003

题意:非典传染给出n个人,m组人,规定0号人为非典的携带者,与他一组的人都将感染

m组人中输入第一个数为k,表示这组的人数

 #include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
int fa[];
int find(int x)
{
return x == fa[x] ? x : find(fa[x]);
} void mix(int x, int y)
{
int fx = find(x), fy = find(y);
fa[fx] = fy;
}
int main()
{
int n,m,k,a,b;
while(scanf("%d %d",&n,&m),n||m)
{
int ans=;
for(int i=;i<n;i++)
{
fa[i]=i;
}
for(int i=;i<m;i++)
{
scanf("%d",&k);
scanf("%d",&a);
for(int i=;i<k-;i++)
{
scanf("%d",&b);
mix(a,b);
}
}
int temp=find();
for(int i=;i<n;i++)
{
if(find(i)==temp)
ans++;
}
printf("%d\n",ans);
} system("pause");
return ;
}

并查集模板题(The Suspects )HZNU寒假集训的更多相关文章

  1. PAT题解-1118. Birds in Forest (25)-(并查集模板题)

    如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...

  2. POJ1611 The Suspects 并查集模板题

    题目大意:中文题不多说了 题目思路:将每一个可能患病的人纳入同一个集合,然后遍历查找每个点,如果改点点的根节点和0号学生的根节点相同,则该点可能是病人. 模板题并没有思路上的困难,只不过在遍历时需要额 ...

  3. HDU 1213 - How Many Tables - [并查集模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...

  4. 并查集模板题----P3367 【模板】并查集

    题目描述 如题,现在有一个并查集,你需要完成合并和查询操作. 输入格式 第一行包含两个整数N.M,表示共有N个元素和M个操作. 接下来M行,每行包含三个整数Zi.Xi.Yi 当Zi=1时,将Xi与Yi ...

  5. 种族并查集模板题分析 -----P2024 [NOI2001]食物链

    本文参考了:洛谷p2024题解 题目描述 动物王国中有三类动物 A,B,C,这三类动物的食物链构成了有趣的环形.A 吃 B,B 吃 C,C 吃 A. 现有 N 个动物,以 1 - N 编号.每个动物都 ...

  6. 杭电ACM省赛集训队选拔赛之热身赛-How Many Tables,并查集模板题~~

    How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  7. PAT甲题题解-1114. Family Property (25)-(并查集模板题)

    题意:给出每个人的家庭成员信息和自己的房产个数与房产总面积,让你统计出每个家庭的人口数.人均房产个数和人均房产面积.第一行输出家庭个数,随后每行输出家庭成员的最小编号.家庭人口数.人均房产个数.人均房 ...

  8. HDU-1232/NYOJ-608畅通工程,并查集模板题,,水过~~~

    畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) http://acm. ...

  9. HDU1232 畅通工程 (并查集模板题)

    畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

随机推荐

  1. bootmgr解压缩

    主要参考以下两个文章: 1.  http://bbs.wuyou.com/forum.php?mod=viewthread&tid=211314 2.  http://reboot.pro/f ...

  2. 不窃取用户隐私的搜索引擎: DuckDuckGo

    不窃取用户隐私的搜索引擎: DuckDuckGo https://duckduckgo.com/ 最近goggle不给力, baidu搜出来的很多都是垃圾, bing用久了很烦. 于是用上了DuckD ...

  3. Oracle生成查询包含指定字段名对应的所有数据表记录语句

    应用场合:已知字段名字,查询数据库中所有数据表中包含该字段名的所有数据表 操作办法:指定字段名,数据库表用户,执行下面查询语句即可 --Oracle生成查询包含指定字段名对应的所有数据表记录语句 de ...

  4. Chapter 1 Securing Your Server and Network(13):配置端点安全性

    原文出处:http://blog.csdn.net/dba_huangzj/article/details/38489765,专题目录:http://blog.csdn.net/dba_huangzj ...

  5. 12.3、Libgdx的图像之截屏

    (官网:www.libgdx.cn) 接下来的示例说明怎样进行截屏并且保存为PNG格式. public class ScreenshotFactory { private static int cou ...

  6. XML跨平台,你懂的

    XML跨平台,你懂的 [引子]       90后小妹,问我,"都说XML跨平台,偶真的,不理解.XML语言的这大优势,倒是深深记在脑海里了."      当然,偶立马应声答到,& ...

  7. 手机广告投放(phone advertising)唯一标识

    手机标示,为了识别用户,方面advertising. 使用设备id,相当于暴露用户隐私.慢慢已不允许使用.. (长时间跟踪用户无异于暴露用户隐私,虽然大量数据适合兴趣建模,广告个性化推荐.但复杂多样会 ...

  8. 【一天一道LeetCode】#79. Word Search

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  9. DBUtils学习总结

    这几天闲着无聊,就看了一下DBUtils这个数据库组件.中间有了一些想法,现在记录下来. 文章主要分几部分 1 最简单同时也是最经常使用的一些范例 2 学习源码前的一些知识储备 3 我自己写的mydb ...

  10. 03_TortoiseGit冲突和补丁演示,补丁冲突

     1 下载TortoiseGit,下载地址: http://tortoisegit.soft32.com/free-download/ 2 创建一个GIT仓库 3 创建克隆,创建两个用于克隆的仓库 ...