• poj 1611 The Suspects


    .

    ..

    The Suspects
    Time Limit: 1000MS   Memory Limit: 20000K
    Total Submissions: 57071   Accepted: 27066

    Description

    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

    Source

     1 #include<stdio.h>
     2 #define MAX 30005
     3  
     4 int a[MAX],pre[MAX];
     5  
     6 int find(int x)
     7 {
     8     if(x!=pre[x])
     9  
    10         //找到其祖先节点 
    11         pre[x] = find(pre[x]);  
    12  
    13     //由父节点继续向上递归查询 ,并将其父节点变成找到的 
    14     return pre[x]; 
    15 }
    16 void merge(int x,int y)
    17 {
    18     //分别查询两点的祖先节点。 
    19     int prex = find(x);
    20     int prey = find(y);
    21  
    22     //如果二者的祖先节点不一致,那么任意让二者中某一个认另一个为祖先,保证同集合。
    23  
    24     if(prex == prey)
    25     {
    26         return ;
    27     } 
    28     //应该是祖先节点进行组合。而不是当前节点! 
    29     pre[prey] = prex;  
    30     a[prex] += a[prey];     
    31 }
    32 int main()
    33 {
    34     int n,m;
    35     int k,x,y;
    36     while(~scanf("%d%d",&n,&m))
    37     {
    38         if(n==0&&m==0)
    39         {
    40             return 0;
    41         } 
    42         for(int i=0;i<n;i++)
    43         {
    44             //先将自身作为祖先节点。 
    45             pre[i] = i;  
    46             a[i] = 1; 
    47         }
    48         for(int i=0;i<m;i++)
    49         {
    50             //给出集合每个集合人数,以及第一个人的编号 
    51             scanf("%d%d",&k,&x);
    52             k--;
    53             while(k--)
    54             {
    55                 scanf("%d",&y);
    56                 merge(x,y); 
    57             }    
    58         } 
    59         printf("%d
    ",a[find(0)]);
    60  
    61     } 
    62     return 0;
    63 }
    View Code
  • 相关阅读:
    gitignore 过滤文件
    lua语言入门之Sublime Text设置lua的Build System
    进程间通信
    临界区 事件 互斥对象等多线程编程基础
    Delphi通过Map文件查找内存地址出错代码所在行
    Delphi/C++ Builder Map文件格式解析
    深入理解计算机系统----读书笔记
    TCP/IP——内网IP
    Python——import与reload模块的区别
    Linux——grep binary file
  • 原文地址:https://www.cnblogs.com/greenaway07/p/11205003.html
Copyright © 2020-2023  润新知