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

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


 
  并查集,经典题
  并查集模板(来自北大课件)
 int ufs[MAXN];    //并查集

 void Init(int n)    //初始化
 {
     int i;
     ;i<n;i++){
         ufs[i] = i;
     }
 }

 int GetRoot(int a)    //获得a的根节点。路径压缩
 {
     if(ufs[a]!=a){    //没找到根节点
         ufs[a] = GetRoot(ufs[a]);
     }
     return ufs[a];
 }

 void Merge(int a,int b)    //合并a和b的集合
 {
     ufs[GetRoot(b)] = GetRoot(a);
 }

 bool Query(int a,int b)    //查询a和b是否在同一集合
 {
     return GetRoot(a)==GetRoot(b);
 }
  题意
  
 
  思路
  很经典的并查集的题目,找一个sum[]数组记录每一个以当前下标为根节点的集合的个体数目,最后输出0号的根节点对应的sum值,就是0号学生所在团体的人数。
 
  代码
 #include <iostream>
 #include <stdio.h>
 using namespace std;
 #define MAXN 30010
 int sum[MAXN];    //集合总数
 int ufs[MAXN];    //并查集

 void Init(int n)    //初始化
 {
     int i;
     ;i<n;i++){
         ufs[i] = i;
         sum[i] = ;
     }
 }

 int GetRoot(int a)    //获得a的根节点。路径压缩
 {
     if(ufs[a]!=a){    //没找到根节点
         ufs[a] = GetRoot(ufs[a]);
     }
     return ufs[a];
 }

 void Merge(int a,int b)    //合并a和b的集合
 {
     int x = GetRoot(a);
     int y = GetRoot(b);
     if(x!=y){
         ufs[y] = x;
         sum[x] += sum[y];
     }
 }

 int main()
 {
     int n,m;
     while(scanf("%d%d",&n,&m)!=EOF){
          && m==) break;
         Init(n);    //初始化并查集
         while(m--){    //读入m行
             int t,one,two;
             scanf("%d",&t);    //每一行有t个数需要输入
             scanf("%d",&one);
             t--;
             while(t--){
                 scanf("%d",&two);
                 Merge(one,two);    //合并集合
             }
         }
         printf()]);
     }
     ;
 }

Freecode : www.cnblogs.com/yym2013

poj 1611:The Suspects(并查集,经典题)的更多相关文章

  1. poj 1611 The Suspects 并查集

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

  2. POJ1611 The Suspects 并查集模板题

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

  3. 【转】并查集&amp;MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...

  4. POJ 1611 The Suspects (并查集)

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

  5. 【HDU1231】How Many Tables(并查集基础题)

    什么也不用说,并查集裸题,直接盲敲即可. #include <iostream> #include <cstring> #include <cstdlib> #in ...

  6. [并查集] POJ 1611 The Suspects

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

  7. poj 1611 The Suspects(并查集)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 21598   Accepted: 10461 De ...

  8. POJ 1611 The Suspects(并查集,简单)

    为什么ACM的题意都这么难懂,就不能说的直白点吗?还能不能好好的一起刷题了? 题意:你需要建一个n的并查集,有m个集合,最后要输出包含0的那个集合的元素的个数. 这是简单并查集应用,所以直接看代码吧! ...

  9. POJ - 1182 食物链 并查集经典

    思路:设r(x)表示节点x与根结点的关系,px表示x的根结点.记录每个节点与其父节点的关系,就能很方便知道每个节点以及和它的父节点的关系. struct node{ int par; //父亲节点 i ...

随机推荐

  1. AOP和IOC的实现原理(用到的设计模式)

    文章来源:http://blog.csdn.NET/longyulu/article/details/36174979 用过spring的朋友都知道spring的强大和高深,都觉得深不可测,其实当你真 ...

  2. Linux软raid创建

    RAID: HBA:基于主机的适配器 RAID:Redundent Array of Inexpensive Disks 廉价磁盘阵列 Independent       独立磁盘阵列 Level:仅 ...

  3. 嵌入式linux学习笔记1—内存管理MMU之虚拟地址到物理地址的转化

    一.内存管理基本知识 1.S3C2440最多会用到两级页表:以段的方式进行转换时只用到一级页表,以页的方式进行转换时用到两级页表.页的大小有三种:大页(64KB),小页(4KB),极小页(1KB).条 ...

  4. asp.net core开发环境准备

    1.1  安装sdk和运行时 浏览器打开网址https://www.microsoft.com/net/download, 到.Net Core下载页面. 根据操作系统,下载对应的SDK进行安装.安装 ...

  5. 【视频处理】YUV格式说明

    YUV,是一种颜色编码方法,Y表示明亮度(Luminance.Luma),U和V则是色度.浓度(Chrominance.Chroma). YUV,Y`UV,YCbCr,YPbPr等都可以称为YUV,彼 ...

  6. Js 的 this 是什么

    this指的是调用函数的那个对象,谁调用函数,谁就是this 更多: http://www.cnblogs.com/justany/archive/2012/11/01/the_keyword_thi ...

  7. Maven3下的java web项目

    咱们使用Maven3构建一个j2ee项目,项目的成果是一个war包,只需把它部署在服务器上,就可以使用浏览器访问. 具体详细信息 参考  http://www.mossle.com/docs/mave ...

  8. Meaningful Use 中与HL7相关的消息及医疗文档

    汇总 HL7 消息 入出转消息 ADT A01,A03,A04,A08 免疫消息 VXU V04 电子处方消息 NEWRX Message v8.1 EDIFACT, v8.1 XML,  v10.6 ...

  9. PHP header函数的几大作用

    先看看官方文档的定义 (PHP 4, PHP 5, PHP 7) header - 发送原生 HTTP 头 void header ( string $string [, bool $replace ...

  10. C语言语法之占用字节

    指针占用字节 指针即为地址,存的是变量的地址,在同一架构下地址长度都是相同的(cpu的最大寻址内存空间),所以不同类型的指针长度都一样. 指针占用几个字节跟语言无关,而是跟系统的寻址能力有关,16为地 ...