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
-
做题步骤:
1.1 题目题意,理清楚细节
1.2.分析题意,找出解题关键
1.3.考虑使用的算法或者解题思路
1.4.考虑时间复杂度和算法优化的可可能性
1.5.检查代码,数组大小和初始化等一些常犯的细节- 题意:
这题和How Many Tables不同
How Many Tables的题是需要找出全部朋友成一桌的集合总个数就可以知道桌子的至少个数为多少;
题目中给你学生人数和他们构成的组数,每一个组成为一个集合,对于犯病嫌疑人,初始以0为嫌疑人,一旦有与0为一组的人都成为嫌疑人,而嫌疑人所在的组里面的人也全都是嫌疑人。对于这个题目,我们需要找的是所有嫌疑人的人数。 - 分析:
对于每一个组,我们可以让是嫌疑人的人成为一个集合,那么我们需要找的是集合与集合之间的联系,也就是是否为0,一旦有联系则说明是嫌疑人,并且最后查询一下集合人数的总数,记录的个数就是嫌疑人的总人数。 - 考虑算法和思路
并查集,我们只需要将所有可以联系的点和并成为一个集合,然后找0这个所在集合的总人数即可。 - 考虑时间复杂度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;
}