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
这题和How Many Tables不同
How Many Tables的题是需要找出每个子集合就可以知道桌子的个数为多少;
而这题还需要继续找到0对应的那个嫌疑犯的集合的人,即感染人数,
根据题意,需要在一组人数的集合(总人数),开始时让每个人构成一个单元素的集合,然后按一定顺序将属于同一组的人所在的集合合并,其间要反复查找一个元素在哪个集合中。,此类问题用到并查集;
并查集
在一些有N个元素的集合应用问题中,我们通常是在开始时让每个元素构成一个单元素的集合,然后按一定顺序将属于同一组的元素所在的集合合并,其间要反复查找一个元素在哪个集合中。这一类问题近几年来反复出现在信息学的国际国内赛题中,其特点是看似并不复杂,但数据量极大,若用正常的数据结构来描述的话,往往在空间上过大,计算机无法承受;即使在空间上勉强通过,运行的时间复杂度也极高,根本就不可能在比赛规定的运行时间(1~3秒)内计算出试题需要的结果,只能用并查集来描述。并查集是一种树型的数据结构,用于处理一些不相交集合(Disjoint Sets)的合并及查询问题。常常在使用中以森林来表示。
- 初始化
把每个点所在集合初始化为其自身。
通常来说,这个步骤在每次使用该数据结构时只需要执行一次,无论何种实现方式,时间复杂度均为O(N)。
- 查找
找元素所在的集合,即根节点。
- 合并
将两个元素所在的集合合并为一个集合。
通常来说,合并之前,应先判断两个元素是否属于同一集合,这可用上面的“查找”操作实现。
这个是找出至少需要的桌子的数
#include<stdio.h>
#include<iostream>
using namespace std;
int s[30004];
int ans;
void sun(int n)//初始化把每个点所在集合初始化为其自身。
{
for(int i=0;i<n;i++)
s[i]=i;
}
int fond(int r)//查找找元素所在的集合,即根节点。
{
if(s[r]==r)
{
s[r]=r;
return r;
}
return fond(s[r]);
}
void fing(int p,int t)//将两个元素所在的集合合并为一个集合。
通常来说,合并之前,应先判断两个元素是否属于同一集合,这可用上面的“查找”操作实现。
{
int i=fond(p);
int j=fond(t);
if(i!=j)
s[j]=i;
}
void sum(int n)//查找0对应的嫌疑犯所在的集合的人,即感染的人数;
{
for(int i=0; i<n; i++)
{
if(fond(i)==fond(0))
ans++;
}
}
int main()
{
int a,b,m,n,c;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(m==0&&n==0)
break;
sun(n);
for(int i=0; i<m; i++)
{
scanf("%d%d",&a,&b);
for(int j=1; j<a; j++)
{
scanf("%d",&c);
fing(b,c);
}
}
ans=0;
sum(n);
printf("%d\n",ans);
}
return 0;
}