poj 1611 The Suspects

来源:互联网 发布:js 折叠 展开 编辑:程序博客网 时间:2024/06/05 18:43
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 42 1 25 10 13 11 12 142 0 12 99 2200 21 55 1 2 3 4 51 00 0

Sample Output

411

【题意】现在一个学校中有很多人,这些人按照0~n-1进行编号,现在0号感染了某种传染病,只要一个圈子里面有一个人得了这种病,那么这个圈子里所有的人都会得这种病,有很多圈子,现在告诉你每个圈子里有那些人,问你有多少人会得病。用样例说话:多组输入,对于每一组,首先是n和m分别是总人数和圈子的个数,接下来m行,对于每一行,首次是一个数字k,表示这个圈子中的总人数,然后是k个数字分别是圈子里的人。

【分析】裸的并查集,稍微注意一下数据操作就行。

【代码】

#include <cstdio>#include <cstring>#include <cmath>using namespace std;int pre[30005];//统计爸爸int fin(int x){    return x==pre[x]?x:fin(pre[x]);}int n,m;void inti(void){    for(int i=0;i<n;i++)        pre[i]=i;}void add(int a,int b){    int aa=fin(a);    int bb=fin(b);    if(aa!=bb)        pre[aa]=bb;}int main(){//    freopen("in.txt","r",stdin);    while(scanf("%d%d",&n,&m)!=EOF&&(n||m))    {         inti();        int a,b,len;        for(int i=0;i<m;i++)        {            scanf("%d",&len);            for(int j=0;j<len;j++)            {                if(j==0)                    scanf("%d",&a);                else                {                    scanf("%d",&b);                    add(a,b);                }            }        }        int ans=0;        int parent=fin(0);//直接找到0的祖先而非爸爸        for(int i=0;i<n;i++)//只要两者祖先相同则两者症状相同            if(fin(i)==parent)                ans++;        printf("%d\n",ans);    }    return 0;}

【续】其实还有一个方法就是开一个num数组,用来统计和当前数字相同症状的人数,初始化为1,然后每次add的时候在判断为true里加上对应的num就行,然后就可以省掉最后的那个循环。

原创粉丝点击