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. 使用gitlab构建基于docker的持续集成(二)

    使用gitlab构建基于docker的持续集成(二) gitlab docker aspnetcore Centos配置gitlab镜像并且启动 Centos配置防火墙 windows上访问gitla ...

  2. 后端程序员之路 5、.conf、libconfig

    .conf在linux里随处可见,作用基本跟windows的.ini差不多 libconfighttp://www.hyperrealm.com/libconfig/libconfig_manual. ...

  3. Element 文档中的 Markdown 解析

    Element 的文档站是讲Markdown解析成vue组件在页面中渲染出来,转换过程如下图所示: 红框部分势必要对 Markdown 进行特殊的订制,订制过的 Markdown 像下面这样. ::: ...

  4. new String("abc"),到底在不在常量池中存储"abc"?

    String str = new String("Hello World"); 问之:这行代码到底有没有在字符串常量池中创建"Hello World"字符串呢? ...

  5. 《吃透MQ系列》核心基础全在这里了

    这是<吃透XXX>技术系列的开篇,这个系列的思路是:先找到每个技术栈最本质的东西,然后以此为出发点,逐渐延伸出其他核心知识.所以,整个系列侧重于思考力的训练,不仅仅是讲清楚 What,而是 ...

  6. [00]数字图像处理-matlab速成

    原本听的是mooc武汉大学的数字图像处理课程,但是无奈老师读ppt的能力太强,不太适应,后面的课程对于实验方面的要求甚低,无奈之下到处找课程,终于找到了一个适合自己的教程<王伟强-数字图像处理& ...

  7. 2020年12月-第02阶段-前端基础-Day06

    CSS Day06 定位(position) 理解 能说出为什么要用定位 能说出定位的4种分类 能说出四种定位的各自特点 能说出我们为什么常用子绝父相布局 应用 能写出淘宝轮播图布局 1. CSS 布 ...

  8. 通达OA 任意文件上传-2013/2015版本

    参考 http://wiki.0-sec.org/0day/%E9%80%9A%E8%BE%BEoa/11.html 影响版本 2013版本 2015版本 漏洞文件 general/vmeet/wbU ...

  9. Nebula Storage 2.0 存储格式

    随着 2.0 各版本的陆续发布,Nebula Graph 迎来了一系列的改动,在存储方面,影响最大的改动就是底层编码格式进行了修改.Nebula Graph 的底层存储是基于 KV 保存在 Rocks ...

  10. PHP配置 3. 配置open_basedir

    open_basedir将网站限定在指定的目录,做目录的隔离 先在php.ini中设置open_basedir: # vim /usr/local/php/etc/php.ini //搜索open_b ...