我是靠谱客的博主 热情香氛,最近开发中收集的这篇文章主要介绍B - The Suspects (并查集),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

来源:vjudge [kuangbin带你飞] 专题5 并查集

B - The Suspects

链接:https://vjudge.net/contest/320571#problem/B

题目描述:

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.

Time limit

1000 ms

Memory limit

20000 kB

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

 

题意:

某地爆发非典,在当地有一个学校,学校里的学生分为许多个团体,每个团体中的学生会经常进行频繁的交流。一个学生有可能加入多个团体。现在有一个学生感染了非典,他被标记为0号,他加入的团体的成为都会成为可能被感染者,并且和这些可能的被感染者接触的学生也有可能被认为是感染者。现在给你学生人数,学生团体数量以及每个团体中加入的学生编号,请你求出所有可能的感染者数量。

 

分析:

这个题是个基本的并查集,属于简单题。在构建并查集时,我们可以将每个团体中的第一个学生取出,然后团体中的其他学生进行合并节点。并查集构建完成后,标记0号学生结点的根节点,然后遍历所有节点,所有根节点和0号节点相同的节点都是可能的感染者,统计输出即可。

#include<iostream>
#include<cstdio>
using namespace std;
const int M=30000+10;
int a[M];
void init_set(int n){	//初始化并查集
for(int i=0;i<n;i++){
a[i]=i;
}
}
int find_set(int x){	//查找根节点
if(x!=a[x]){
a[x]=find_set(a[x]);
}
return a[x];
}
void union_set(int x,int y){	//合并节点
x=find_set(x);
y=find_set(y);
if(x!=y){
a[x]=y;
}
}
int main(){
int n,m;
while(~scanf("%d%d",&n,&m)&&(n||m)){
init_set(n);
for(int i=0;i<m;i++){
int k;
scanf("%d",&k);
if(k==0){	//可能会有没有成员的团体
continue;
}
int t1;
scanf("%d",&t1);
//取出每个团体中的第一个学生,使其和团体内其他所有学生合并节点
for(int j=1;j<k;j++){
int t2;
scanf("%d",&t2);
union_set(t1,t2);
}
}
int srs=find_set(0);	//标记0号学生的根节点
int ans=0;
for(int i=0;i<n;i++){
if(find_set(i)==srs){
//查找所有根节点和0号节点相同的节点
ans++;
}
}
printf("%dn",ans);
}
}

 

最后

以上就是热情香氛为你收集整理的B - The Suspects (并查集)的全部内容,希望文章能够帮你解决B - The Suspects (并查集)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部