zoukankan      html  css  js  c++  java
  • POJ1611 The Suspects(并查集)

    The Suspects
    Time Limit: 1000MS   Memory Limit: 20000K
    Total Submissions: 66564   Accepted: 31466

    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

    思路:并查集把所有接触过的人都合并为一个集合,最后再判断有多少人跟编号为0的人在同一个集合就可以了

    (感觉用bitset处理也能过来着,不过发现要申请的空间太大了就上并查集了)

     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdlib>
     4 #include <cstdio>
     5 #include <string>
     6 #include <bitset>
     7 #include <vector>
     8 #include <map>
     9 #include <queue>
    10 using namespace std;
    11 #define MAXN 31000
    12 #define MAXM 502
    13 const int INF = 0x3f3f3f3f;
    14 
    15 int fa[MAXN], ra[MAXN];
    16 int find(int x){
    17 
    18     int t = x;
    19     while(t != fa[t]){
    20         t = fa[t];
    21     }
    22     return t;
    23 }
    24 void Union(int x, int y){
    25     x = find(x);
    26     y = find(y);
    27 
    28     if(x == y)
    29         return;
    30     if(ra[x] >= ra[y]){
    31         fa[y] = x;
    32         if(ra[x] == ra[y])
    33             ra[x] ++;
    34     }
    35     else{
    36         fa[x] = y;
    37     }
    38 }
    39 
    40 int main(){
    41     int ans;
    42     int n, m, num, x, k, h, last;
    43     while(cin >> n >> m, n){
    44         ans = 0;
    45         for(int i = 0; i < n; ++ i){
    46             fa[i] = i;
    47             ra[i] = 0;
    48         }
    49         
    50         for(int i = 0; i < m; ++ i){
    51             cin >> k;
    52             cin >> h;
    53             for(int j = 1; j < k; ++ j){
    54                 cin >> last;
    55                 Union(h, last);
    56             }
    57         }
    58 
    59         for(int i = 0; i < n; ++ i){
    60             int x = find(i), y = find(0);
    61             if(x == y)
    62                 ++ ans;
    63         }
    64         cout << ans << endl;
    65 
    66     }
    67 
    68 
    69     
    70     return 0;
    71 }
  • 相关阅读:
    675 对象的引用-浅拷贝-深拷贝
    674 vue3侦听器watch
    673 vue计算属性:缓存,setter和getter
    明明有了promise,为啥还需要async await?
    Js常用数组方法汇总
    一些非常有用的Js单行代码
    Js获取验证码倒计时
    前端截取字符串:JS截取字符串之substring、substr和slice详解
    javascript全局变量与局部变量
    JS实现快速排序算法
  • 原文地址:https://www.cnblogs.com/daremo/p/14126962.html
Copyright © 2011-2022 走看看