并查集专题:POJ-1161 The Suspects

与世无争的帅哥 提交于 2020-10-29 18:21:22

并查集专题训练的最后一天,其实基础题还有 10 道未完成,我在 vjudge 上已经开了专题训练:https://vjudge.net/contest/314923#overview,点击原文链接可跳转,密码:guagua

并查集专题:POJ-1161 The Suspects

题目:

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 42 1 25 10 13 11 12 142 0 12 99 2200 21 55 1 2 3 4 51 00 0

Sample Output

411

题解:

题目大意就是,和 0 一组的同学都会感染病毒,并且这个病毒可以继续扩散。

所以我们在建集合的时候,不用按照题目要求的集合来简历关系,只需要当做每个人建立就行。因为每组人都在一个集合内,所以边读入数据,边建集。

注意几个 corner case:

1.m == 0直接输出 1 就好了;2.并查集可以稍微处理一下,做一个 num 数组来记录每个点所在集合的长度。在合并时也进行一个数量合并就行。这个非常像线段树的 push_up 操作;

代码:

#include <iostream>#include <cstring>#include <cstdio>
using namespace std;
const int MAX_N = 30000 + 5;
int par[MAX_N], num[MAX_N];int Rank[MAX_N];
void init(int n) { memset(par, -1, sizeof(par)); for (int i = 0; i <= n; ++ i) { par[i] = i; Rank[i] = 1; num[i] = 1; }}
int find(int x) { if (par[x] == x) { return x; } else { return par[x] = find(par[x]); }}
void uniq(int p, int q) { p = find(p); q = find(q); if (p == q) return ; if (Rank[p] < Rank[q]) { par[p] = q; num[p] += num[q]; num[q] = num[p]; } else { par[q] = p; if (Rank[p] == Rank[q]) { Rank[p] ++; } num[q] += num[p]; num[p] = num[q]; }}
int main() { int n, m, x, y; while (~scanf("%d %d", &n, &m)) { if (n == 0 && m == 0) break; if (m == 0) { puts("1"); continue; } init(n); while (m --) { int rt; scanf("%d %d", &x, &rt); for (int i = 0; i < x - 1; ++ i) { scanf("%d", &y); uniq(rt, y); // root 转换 rt = y; } } x = find(0); printf("%d\n", num[x]); }
return 0;}


本文分享自微信公众号 - 一瓜技术(tech_gua)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!