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. Unity UGUI基础之Toggle

    Toggle组合按钮(单选框),可以将多个Toggle按钮加入一个组,则他们之间只能有一个处于选中状态(Toggle组合不允许关闭的话). 一.Toggle组件: Toggle大部分属性等同于Butt ...

  2. JS中回调函数的写法

    <!DOCTYPE HTML> <html><head>  <meta charset="GBK" /><title>回 ...

  3. Block高级用法:Block传值UI_12(3)

    1.简单复习Block的定义.赋值.调用做学习传值铺垫: //声明一个函数 无返无参void printfHello(int a);//函数的实现void printfHello(int a){    ...

  4. 存储那些事儿(一):异构虚拟化一种实现SMIS

    1. 背景 企业存储是企业信息系统管理很重要的组成部分.企业存储包含了大量的数据,供大量人使用.对于航空系统和金融系统来说,信息存储就更加重要了. 作为企业信息存储,扩展性是非常重要的,因为现在企业对 ...

  5. pig的cogroup详解

    从实例出发 %default file test.txt A = load '$file' as (date, web, name, food); B = load '$file' as (date, ...

  6. Oracle R12 多组织访问的控制 - MOAC(Multi-Org Access Control)

    什么是MOAC MOAC(Multi-Org Access Control)为多组织访问控制,是Oracle EBS R12的重要新功能,它可以实现在一个Responsibility下对多个Opera ...

  7. 【Unity 3D 游戏开发】Unity3D 入门 - 工作区域介绍 与 入门示例

    一. 工作区域详解 1. Scence视图 (场景设计面板) scence视图简介 : 展示创建的游戏对象, 可以对所有的游戏对象进行 移动, 操作 和 放置; -- 示例 : 创建一个球体, 控制摄 ...

  8. 经过一段的努力,终于成为CSDN博客专家,感谢大家支持

    感谢CSDN提供这么好的一个技术学习平台,通过各路大神的博客我成长了许多,同时也感谢支持我的朋友们,我会继续努力,用心去写好博客.还请继续关注我~ 谢谢!

  9. Swift基础之UITabBarController(这是在之前UITableView中直接添加的)

    这些基础内容基本已经可以搭建项目框架,剩下的就是一些优化,细节和数据请求问题,慢慢更新.... 在AppDelegate中创建方法 //创建方法执行UITabBarController func cr ...

  10. RabbitMQ 队列

    http://blog.chinaunix.net/uid/22312037/sid-163962-abstract-1.html http://bobo896.blog.163.com/blog/# ...