• B


    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
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <iomanip>
    #include <deque>
    #include <bitset>
    //#include <unordered_set>
    //#include <unordered_map>
    //#include <bits/stdc++.h>
    //#include <xfunctional>
    #define ll              long long
    #define PII             pair<int, int>
    #define rep(i,a,b)      for(int  i=a;i<=b;i++)
    #define dec(i,a,b)      for(int  i=a;i>=b;i--)
    #define pb              push_back
    #define mk              make_pair
    using namespace std;
    int dir1[6][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 },{ 1,1 },{ -1,1 } };
    int dir2[6][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 },{ 1,-1 },{ -1,-1 } };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    const double pi = 3.14159265358979;
    const int mod = 1e9 + 7;
    const int N = 30005;
    //if(x<0 || x>=r || y<0 || y>=c)
    
    inline ll read()
    {
        ll x = 0; bool f = true; char c = getchar();
        while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
        while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
        return f ? x : -x;
    }
    
    int n, m;
    int pa[N],num[N],rankn[N];
    
    void makeset(int x)
    {
        pa[x] = x;
        num[x] = 1;
        rankn[x] = 0;
    }
    
    int findset(int x)
    {
        int r = x, tmp;
        while (pa[r] != r)
            r = pa[r];
        while (x != r)
        {
            tmp = pa[x];
            pa[x] = r;
            x = tmp;
        }
        return x;
    }
    
    void unionset(int x,int y)
    {
        x = findset(x);
        y = findset(y);
        if (x == y)
            return;
        if (rankn[x] > rankn[y])
        {
            pa[y] = x;
            num[x] += num[y];
        }
        else
        {
            pa[x] = y;
            if (rankn[x] == rankn[y])
                rankn[y]++;
            num[y] += num[x];
        }
    }
    
    int main()
    {
        while (cin >> n >> m)
        {
            if (m == n && n == 0)
                break;
            if (m == 0)
            {
                cout << 1 << endl;
                continue;
            }
            for (int i = 0; i < n; i++)
            {
                makeset(i);
            }
            for (int i = 0; i < m; i++)
            {
                int t, x, y;
                cin >> t >> x;
                for (int j = 1; j < t; j++)
                {
                    cin >> y;
                    unionset(x, y);
                    x = y;
                }
            }
            int root = findset(0);
            cout << num[root] << endl; 
        }
        return 0;
    }
  • 相关阅读:
    设计模式——装饰器模式
    设计模式——适配器模式
    Java IO概述
    Java中的注解
    痞子衡嵌入式:恩智浦i.MX RT1xxx系列MCU启动那些事(3)- Serial Downloader模式(sdphost/MfgTool)
    痞子衡嵌入式:恩智浦i.MX RT1xxx系列MCU启动那些事(2)- Boot配置(BOOT Pin/eFUSE)
    痞子衡嵌入式:恩智浦i.MX RT1xxx系列MCU启动那些事(1)- Boot简介
    痞子衡嵌入式:ARM Cortex-M内核那些事(5)- 一表搜罗指令集
    痞子衡嵌入式:SEGGER J-Link仿真器硬件版本变迁
    痞子衡嵌入式:恩智浦i.MX RT1xxx系列MCU特性那些事(4)- RT105x选型
  • 原文地址:https://www.cnblogs.com/dealer/p/12741589.html
Copyright © 2020-2023  润新知