B - The Suspects(并查集)详解

题目描述:n个学生分属m个团体,一个学生可以属于多个团体。一个学生疑似患病则它所属的整个团体都疑似患病。已知0号学生疑似患病,以及每个团体都由哪些学生构成,求一共多少个学生疑似患病。

解题思路:本题的话如果我们用简单的并查集就会出现很多问题,因此我应该在并查集的基础上在加一点东西,我们在合并两个人为同一个社团时,我们就应该默认的统计一下这个社团的人数,即拥有共同祖先的人数,因此我们在查找的过程中就可以很轻松的找到要找的人数总和。

错误分析:注意输入,因为merge()合并时是两个数相比较,所以输入要分开。

 题目

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 0

Sample Output

4

1

1

AC代码 

#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int n,m;
int f[30010];
bool b[30010];
void init()
{
	int i;
	for(i=1;i<=n;i++)
	{
		f[i]=i;
		b[i]=false;
	}
	return ;
}
int getf(int v)
{
	if(f[v]==v)
		return v;
	else
	{
		f[v]=getf(f[v]);
		return f[v];
	}
}
void merge(int u,int v)
{
	int t1,t2;
	t1=getf(u);
	t2=getf(v);
	if(t1==t2)
		return ;
	if(t1==0)
		f[t2]=t1;
	else
		f[t1]=t2;
}
int main()
{
	int t,x,y,i,j;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		if(m==0&&n==0)
		break;
		init();
		b[1]=true;
		for(i=1;i<=m;i++)
		{
			scanf("%d",&t);
			scanf("%d",&x);
			b[x]=true;
			for(j=2;j<=t;j++)
			{
				scanf("%d",&y);
				b[y]=true;
				merge(x,y);	
			}	
		}
		int sum=0;
		for(i=0;i<n;i++)
		{
			if(b[i]&&getf(i)==0)
				sum++;
		}
		printf("%d\n",sum);
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/zz_xun/article/details/119995080
B