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相连的节点的个数

题解:通过并查集将所有的结点连接在一起,在连接两个不同的组时,将边数少的连接到变数少的连接的变数多的的上面

AC代码:

 1 #include<iostream>
2
3 using namespace std;
4
5 int pre[30005];
6 int Rank[30005];
7
8 int Find(int x) {
9 return pre[x] = pre[x] == x ? x : Find(pre[x]);
10 }
11
12 void Union(int x, int y) {
13 int t1, t2;
14 t1 = Find(x);
15 t2 = Find(y);
16 if (t1 == t2)
17 return ;
18 if (Rank[t1] > Rank[t2]) {
19 pre[t2] = t1;
20 }
21 else {
22 pre[t1] = t2;
23 if (Rank[t1] == Rank[t2]) {
24 pre[t1] = t2;
25 Rank[t2]++;
26 }
27 }
28 }
29
30 int main() {
31 int n, m;
32 while(cin >> n >> m) {
33
34 if (n == 0 && m == 0)
35 break;
36 // 初始化父亲节点
37 for (int i = 0; i <= n; i++) {
38 pre[i] = i;
39 Rank[i] = 0;
40 }
41
42 for (int i = 1; i <= m; i ++) {
43 int t, k, h, r, flag;
44 cin >> t;
45 if (t > 0)
46 cin >> r;
47
48 for (int j = 1; j < t; j++) {
49 cin >> k;
50 Union(r, k);
51 }
52
53 }
54 int ans = 1, cmp = Find(0);
55
56 for (int i = 1; i < n; i++) {
57 if (cmp == Find(i))
58 ans++;
59 }
60 cout << ans << endl;
61 }
62
63 return 0;
64 }

C - The Suspects POJ - 1611(并查集)的更多相关文章

  1. The Suspects(POJ 1611 并查集)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 30158   Accepted: 14665 De ...

  2. poj 1611(并查集)

    http://poj.org/problem?id=1611 题意:有个学生感染病毒了,只要是和这个学生接触过的人都会感染,而和这些被感染者接触的人,也会被感染,现在给定你一些协会的人数,以及所在学生 ...

  3. POJ 1611(并查集+知识)

    并查集主要是两个过程,一个是并,一个是查 原理是用一个数组p[i]保存每个i的根节点,如果根节点一样则在同一个集合里,所以只有根节点p[i]=i; 查: int find(int x){return ...

  4. POJ 1611并查集

    我发现以后写题要更细心,专心! #include<iostream>#include<algorithm>#include<stdio.h>#include< ...

  5. poj 1984 并查集

    题目意思是一个图中,只有上下左右四个方向的边.给出这样的一些边, 求任意指定的2个节点之间的距离. 就是看不懂,怎么破 /* POJ 1984 并查集 */ #include <stdio.h& ...

  6. poj 1611 :The Suspects经典的并查集题目

    Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized ...

  7. 【POJ】The Suspects(裸并查集)

    并查集的模板题,为了避免麻烦,合并的时候根节点大的合并到小的结点. #include<cstdio> #include<algorithm> using namespace s ...

  8. poj 1797(并查集)

    http://poj.org/problem?id=1797 题意:就是从第一个城市运货到第n个城市,最多可以一次运多少货. 输入的意思分别为从哪个城市到哪个城市,以及这条路最多可以运多少货物. 思路 ...

  9. POJ 2492 并查集扩展(判断同性恋问题)

    G - A Bug's Life Time Limit:10000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u S ...

随机推荐

  1. 基于μcOS-II实时操作系统源码实现RMS和EDF调度(共享资源)

    μcOS-II多任务实验报告(RMS.EDF调度) 目录 μcOS-II多任务实验报告(RMS.EDF调度) 一.实验概述 二.环境搭建 三.代码分析 四.实验步骤 1 给TCB块添加扩展 2 创建并 ...

  2. STM32学习笔记——序言

    写AVR已经两年了.如果初中时候玩Arduino也算的话,就是6年. 两年以来,我用AVR单片机完成了两个大项目: AVR单片机教程,一时兴起写的,效果不好: MEDS,参赛用的课题,半完成,比赛都结 ...

  3. Java 程序员每天都在做什么?

    作为一名 在大.中.小微企业都待过 的 Java 开发者,今天和大家分享下自己在不同公司的工作日常和收获.包括一些个人积累的工作提升经验,以及一些 Java 学习的方法和资源. 先从我的第一份 Jav ...

  4. CCF(压缩编码):动态规划+平行四边形优化

    压缩编码 201612-4 一开始看这题还以为是哈夫曼编码的题目,结果是哈夫曼题目的变形. 哈夫曼编码是每次合并任意两堆石子,而这里的题目是合并相邻的两堆石子,而且这里的合并花费是合并两堆石子加上所有 ...

  5. [个人总结]pip安装tensorboard太慢

    在执行pip install语句的时候直接指定国内豆瓣的镜像源进行下载: pip install -i https://pypi.douban.com/simple 你想下载的包的名称 例如下载ten ...

  6. 白嫖微软Azure12个月服务器

    前言 Azure是微软提供的一个云服务平台.是全球除了AWS外最大的云服务提供商.Azure是微软除了windows之外另外一个王牌,微软错过了移动端,还好抓住了云服务.这里的Azure是Azure国 ...

  7. javamelody简单介绍

    JavaMelody 能够监测Java或Java EE应用程序服务器,并以图表的方式显示:Java内存和Java  CPU使用情况,用户Session数量,JDBC连接数,和http请求.sql请求. ...

  8. 2018.8.30 nowcoder oi赛制测试1

    2018.8.30 nowcoder oi赛制测试1 普及组难度,发现了一些问题 A 题目大意:求斐波那契数列\(f(k-1)f(k+1)-f(k)^2\),范围极大 打表可得规律 其实是卡西尼恒等式 ...

  9. 【Django必备01】——什么是Django框架?有什么优势?模块组成介绍。

    01.什么是Django框架? Django是一个开放源代码的Web应用框架,由Python写成.采用了MTV的框架模式.使用这种架构,程序员可以方便.快捷地创建高品质.易维护.数据库驱动的应用程序. ...

  10. 2019HDU多校第一场 6582 Path 【最短路+最大流最小割】

    一.题目 Path 二.分析 首先肯定要求最短路,然后如何确定所有的最短路其实有多种方法. 1 根据最短路,那么最短路上的边肯定是可以满足$dist[from] + e.cost = dist[to] ...