• The Suspects POJ


    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

    翻译:有n个人,m个集合,每个人的标号为0到n,其中0带有传染病,如果再一个集合中有人有传染病,那么这个集合里的所有人都会有这种传染病
    ,问每个测试中有传染病的有但是人,输入第一行为n,m。往下为m行,每行第一个为该集合的人数,其他为该集合中人的标号。输入以0 0结束。

    思路:简单的并查集,套用模板即可,结果只需判断与0同集合的有但是人。

    代码:
     1 #include <cstdio>
     2 #include <fstream>
     3 #include <algorithm>
     4 #include <cmath>
     5 #include <deque>
     6 #include <vector>
     7 #include <queue>
     8 #include <string>
     9 #include <cstring>
    10 #include <map>
    11 #include <stack>
    12 #include <set>
    13 #include <sstream>
    14 #include <iostream>
    15 #define mod 998244353
    16 #define eps 1e-6
    17 #define ll long long
    18 #define INF 0x3f3f3f3f
    19 using namespace std;
    20 
    21 //fa[x]表示x的最远祖先
    22 int fa[30005];
    23 //初始化,一开始每个点单独成集合
    24 void build(int qwq) 
    25 {
    26     for(int i=0;i<=qwq;i++)
    27     {
    28         fa[i]=i;
    29     }
    30     return ;
    31 } 
    32 //找到x的最远祖先,并且压缩路径
    33 int find(int x)
    34 {
    35     if(fa[x]==x)
    36     {
    37         return x;
    38     }
    39     return fa[x]=find(fa[x]);
    40 }
    41 //判断x,y是不是在同一个集合里,直接判断最远祖先是不是一样的 
    42 bool che(int x,int y)
    43 {
    44     return find(x)==find(y);
    45 }
    46 //合并x,y,我们在判断x和y是不是同一个集合里,
    47 //路径压缩之后fa[x],fa[y]已经是最远祖先了,
    48 //所以直接将fa[x]的父亲连接在fa[y]的祖先上
    49 void mer(int x,int y)
    50 {
    51     if(!che(x,y)) 
    52     {
    53         fa[fa[x]]=fa[y];
    54     }
    55     return ;
    56 }
    57 
    58 int main()
    59 {
    60     int n,m;
    61     while(scanf("%d %d",&n,&m)&&(n||m))
    62     {
    63         //初始化
    64         build(n);
    65         int k,a,b;
    66         for(int i=0;i<m;i++)
    67         {
    68             scanf("%d",&k);
    69             //每组第一个数
    70             scanf("%d",&a);
    71             //将每组其他数与本组的第一个数合并
    72             for(int j=1;j<k;j++)
    73             {
    74                 scanf("%d",&b);
    75                 mer(a,b);
    76             }
    77         }
    78         //0本身也是一个人
    79         int ans=1;
    80         //遍历寻找与0同集合的数
    81         for(int i=1;i<=n;i++)
    82         {
    83             if(find(i)==find(0))
    84             {
    85                 ans++;
    86             }
    87         }
    88         printf("%d
    ",ans);
    89     }
    90 }
  • 相关阅读:
    haar特征(转)
    《统计学习那些事》转
    假设空间与概念空间(版本空间)
    opencv入门指南(转载)
    C++ 中的指针、引用以及函数调用中的问题
    关于python中的module
    Bugzilla 使用指南
    创新方法培训 学习总结
    集成产品开发-IPD简介
    SVN之文件同步更新
  • 原文地址:https://www.cnblogs.com/mzchuan/p/11594195.html
Copyright © 2020-2023  润新知