博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 1611 The Suspects
阅读量:4837 次
发布时间:2019-06-11

本文共 2788 字,大约阅读时间需要 9 分钟。

Time Limit: 1000MS   Memory Limit: 20000K
Total Submissions: 25197   Accepted: 12355

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 裸的并查集 CODE:
#include 
#include
#include
#define REP(i, s, n) for(int i = s; i <= n; i ++)#define REP_(i, s, n) for(int i = n; i >= s; i --)#define MAX_N 30000 + 10using namespace std;int n, m, k;int F[MAX_N], R[MAX_N];int find(int x){ if(x == F[x]) return x; return F[x] = find(F[x]);}void Union(int x, int y){ int rx = find(x), ry = find(y); if(rx == ry) return; F[ry] = rx; R[rx] += R[ry];}int main(){ while(scanf("%d%d", &n, &m) != EOF){ if(n == 0 && m == 0) break; REP(i, 0, n) F[i] = i, R[i] = 1; int x, y; REP(i, 1, m){ scanf("%d", &k); scanf("%d", &x); REP(i, 2, k){ scanf("%d", &y); Union(x, y); } } printf("%d\n", R[find(0)]); } return 0;}

 

 

转载于:https://www.cnblogs.com/ALXPCUN/p/4552120.html

你可能感兴趣的文章
Spark Streaming笔记整理(三):DS的transformation与output操作
查看>>
全面对比微服务配置中心,哪一个更适合你?
查看>>
C++的正则
查看>>
Codeforces Round #323 (Div. 1) B. Once Again... DP
查看>>
我的第一个python web开发框架(11)——工具函数包说明(二)
查看>>
我有一个idea,但是没有钱,又没技术怎么办?
查看>>
面向对象设计模式与原则
查看>>
解决数组中重复的数字
查看>>
无法连接到 OPC服务器[无此类接口支持(异常来自HRESULT:0x80004002(E_NOINTERFACE))]...
查看>>
查看nginx cache命中率
查看>>
synchronized是什么
查看>>
Coredata — 入门使用
查看>>
Bootstrap modal
查看>>
java集合介绍(List,Set,Map)
查看>>
转载:Zend Framework 配置文件怎么写呢?
查看>>
linux mysql 远程访问权限问题
查看>>
Blog文章待看
查看>>
安卓环境搭建
查看>>
每天一个Linux命令之mkdir
查看>>
Windows7 IIS安装与配置asp和asp.net(图解)教程
查看>>