poj1611----并查集的简单应用

来源:互联网 发布:普通发票软件下载 编辑:程序博客网 时间:2024/06/18 18:16

The Suspects
Time Limit: 1000MS Memory Limit: 20000K
Total Submissions: 38560 Accepted: 18707
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号学生被怀疑感染,每个学生可以参加多个社团,一个社团有一个学生被怀疑感染,那么该社团的所以其他学生也被怀疑感染。找出怀疑感染的人数。
解题思路:本题考察并查集的基本应用,将属于同一个社团的学生合并集合,最后找到0号学生所在的结合,该集合中的所有学生都是怀疑感染的。
ac代码:

#include <iostream>#include <string.h>using namespace std;const int MAX_N = 30000;const int MAX_M = 500;int father[MAX_N];int num[MAX_N];int rank[MAX_N];void make_set(int x){    father[x] = x;    num[x] = 1;    rank[x] = 0;}int find_set(int x){    if(x == father[x] || x == -1){        return x;    }else{        return father[x] = find_set(father[x]);    }}//rank代表了树的高度//将高度较高的元素作为高度较小的元素的父节点可以有效减小树的高度void union_set(int x,int y){    x = find_set(x);    y = find_set(y);    if(x == y){        return;    }else if(x == -1 || y == -1){        return;    }else if(rank[x] > rank[y]){        father[y] = x;        num[x] += num[y];    }else{        if(rank[x] == rank[y]){            rank[y]++;        }        num[y] += num[x];        father[x] = y;    }}int main(){    /*    解题思路:使用并查集找到和0号感染者在同一个组中的人即可    */    int studentNum,groupNum;    int groupStudentNum;    cin>>studentNum;    cin>>groupNum;    while(true){        //当学生人数和社团人数都为0的时候代表输入结束        if(studentNum == 0 && groupNum == 0){            break;        }else if(groupNum == 0){            //当社团个数为0的时候,应为没有社团感染者只有0号学生            cout<<1<<endl;        }else{            for(int i = 0;i < studentNum;i++){                make_set(i);            }            for(int i = 0;i < groupNum;i++){                //当前组中一共有多少人                cin>>groupStudentNum;                int preStudent = -1;                int curStudent = -1;                for(int i = 0;i < groupStudentNum;i++){                    cin>>curStudent;                    union_set(preStudent,curStudent);                    preStudent = curStudent;                }            }            cout<<num[find_set(0)]<<endl;        }        cin>>studentNum;        cin>>groupNum;    }    return 0;}
原创粉丝点击