http://poj.org/problem?id=1611
Time Limit: 1000MS | Memory Limit: 20000K | |
Total Submissions: 15428 | Accepted: 7364 |
Description
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
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.
Output
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
Source
有很多组学生,在同一个组的学生经常会接触,也会有新的同学的加入。但是SARS是很容易传染的,只要在改组有一位同学感染SARS,那么该组的所有同学都被认为得了SARS。现在的任务是计算出有多少位学生感染SARS了。假定编号为0的同学是得了SARS的。
解题思路---->显然并查集了。采用num[]存储该集合中元素个数,并在集合合并时更新num[]即可。然后找出0所在的集合的根节点x,因此,num[x]就是answer了。
//并查集主要操作1初始化2查找3合并
#include<stdio.h>
int father[30001],num[30001];//num存储节点所在集合元素的个数father[]存储节点的父节点
void Make_Set(int x)//初始化集合(把每个点所在集合初始化为其自身)
{
}
int Find_Set(int x)//2查找x元素所在的集合,回溯时压缩路径 返回x所在集合的根节点
{
}
void Union_Set(int a,int b)/// 3合并a,b所在的集合
{
}
int main()
{
}