zoukankan      html  css  js  c++  java
  • Poj1611 the Suspects

    简单并查集

    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

    分析:

    给n个人,给出m个关系,其中0号人是受感染着,所以与0在同一个集合中的所有的人都会收到感染
    
    现在等于是问与0在同一个集合中有多少人?
    
    这样想就很简单了,并查集有很多种实现方法,这里打算采用的是路径压缩的那种实现方法
    
    因为我们总是把较小的那个元素作为根,而且问的也是0,所以直接在最后扫描一遍Set数组就可以了
    

    题外话

    要注意输入,我当时注意到如果m行中以1来开头,
    
    这样就是还是他自己跟自己是一个集合,这样就没有必要在输入了,就直接continue
    
    这样导致我一直WA,后来才想明白,在continue之前还是需要加上一个cin >> test
    
    将这个值过滤掉,这样才不会影响后边的人
    

    代码:

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <cstdlib>
    
    using namespace std;
    const int maxn = 30300;
    int Set[maxn];
    
    int find(int x){
      int r = x;
      while(r != Set[r])
        r = Set[r];
    
      int t = x;
      while(t != r){
        int p = Set[t];
        Set[t] = r;
        t = p;
      }
      return r;
    }
    void merge(int a, int b){
      int fa = find(a);
      int fb = find(b);
      if(fa < fb){
        Set[fb] = fa;
      }
      else if(fa > fb)
      {
        Set[fa] = fb;
      }
      return ;
    }
    
    int main(){
      // freopen("in.txt","r",stdin);
      int n,m,t,tmp1,tmp2;
      while (cin >> n >> m && n!=0) {
        if(m == 0){
          cout << "1" << endl;
          continue;
        }
        //init
        for(int i = 0 ; i < n ; i++)
          Set[i] = i;
        for(int i = 0 ; i < m ; i++){
          cin >> t;
          if(t <= 1){
            int test;
            cin >> test;
            continue;
          }
          cin >> tmp1;
          for(int j = 0 ; j < t-1 ; j++){
            cin >> tmp2;
            merge(tmp1,tmp2);
            tmp1 = tmp2;
          }
        }
        int cnt = 0;
        for(int i = 0 ; i < n ; i++){
          if(0 == find(i))
            cnt++;
        }
        cout << cnt << endl;
      }
      return 0;
    }
    
    
  • 相关阅读:
    mui实现分页上拉加载更多 下拉刷新数据的简单实现 移动端下拉上拉
    滑动时候警告:Unable to preventDefault inside passive event listener
    vue-cli3 一直运行 /sockjs-node/info?t= 解决方案
    css设置不允许复制文本内容
    SDK manager打不开解决办法
    Android Studio 于夜神模拟器进行连接
    从零开始学 Java
    从零开始学 Java
    从零开始学 Java
    从零开始学 Java
  • 原文地址:https://www.cnblogs.com/pprp/p/8662268.html
Copyright © 2011-2022 走看看