POJ 1611-The Suspects(並查集-同一集合)

kewlgrl發表於2017-01-17

The Suspects
Time Limit: 1000MS   Memory Limit: 20000K
Total Submissions: 35531   Accepted: 17239

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


題目意思:

嚴重急性呼吸綜合徵(SARS)是一種未知病因的非典型肺炎,在2003年3月中旬被確認為全球性威脅。為了儘量減少對他人的傳播,最好的策略是將嫌疑人與他人分開。
在不擴散大學(NSYSU),有很多學生組。同一組中的學生經常互相溝通,學生可以加入幾個小組。為了防止SARS的可能傳輸,NSYSU收集所有學生組的成員列表,並在其標準操作過程(SOP)中制定以下規則:
一旦群組中的成員是可疑人員,群組中的所有成員都是可疑人員。
然而,他們發現,當學生被認定為嫌疑犯時,識別所有嫌疑犯並不容易。你的工作是寫一個程式,找到所有的嫌疑犯。

輸入檔案包含多種情況。每個測試用例以一個行中的兩個整數n和m開始,其中n是學生的數量,m是組的數量。您可以假設0 <n <= 30000和0 <= m <= 500。每個學生都使用0和n-1之間的唯一整數進行編號,最初學生0在所有情況下都被識別為可疑人員。此行後面是組的m個成員列表,每個組一行。每行以一個整數k開始,它表示組中的成員數。按照成員數量,有k個整數表示該組中的學生。一行中的所有整數至少由一個空格分隔。
其中n = 0且m = 0的情況下表示輸入的結束,並且不需要進行處理。

解題思路:

並查集將學生按分組,然後統計與0號學生在同一組的學生的人數(包括自身)。

#include <stdio.h>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
int set[50000+5];
int set_find(int p)
{
    if(set[p]==0)
        return p;
    return set[p]=set_find(set[p]);
}
void join(int p,int q)
{
    int p1,q1;
    p1=set_find(p);
    q1=set_find(q);
    if(p1!=q1)
        set[p1]=q1;
}

int main()
{
    int n, m, i, j,ans;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(m==0&&n==0) break;
        ans=0;
        memset(set,0,sizeof(set));
        while(m--)//輸入各組
        {
            int t;
            scanf("%d", &t);
            --t;
            scanf("%d",&i);
            while(t--)
            {
                scanf("%d",&j);
                join(i,j);
            }
        }
        for(int i=1; i<n; i++)
            if(set_find(i)==set_find(0))//統計與0號在同一組的人數
                ans++;
        printf("%d\n",++ans);
    }
    return 0;
}
/*
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
*/



相關文章