码迷,mamicode.com
首页 > 其他好文 > 详细

poj 1611 :The Suspects经典的并查集题目

时间:2020-01-21 23:30:26      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:repr   sel   follow   standard   查找   may   父节点   memset   tor   

 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

提供两个模板:

模板一:

 1 int ufs[MAXN];    //并查集
 2 
 3 void Init(int n)    //初始化
 4 {
 5     int i;
 6     for(i=0;i<n;i++){
 7         ufs[i] = i;
 8     }
 9 }
10 
11 int GetRoot(int a)    //获得a的根节点。路径压缩
12 {
13     if(ufs[a]!=a){    //没找到根节点
14         ufs[a] = GetRoot(ufs[a]);
15     }
16     return ufs[a];
17 }
18 
19 void Merge(int a,int b)    //合并a和b的集合
20 {
21     ufs[GetRoot(b)] = GetRoot(a);
22 }
23 
24 bool Query(int a,int b)    //查询a和b是否在同一集合
25 {
26     return GetRoot(a)==GetRoot(b);
27 }

模板二:

 1 int pre[30000];
 2 int find(int x){//查找x父节点 
 3     int r=x; //委托r去找父节点 
 4     while(pre[r]!=r) //如果r的上级不是r自己(也就是说找到的节点他不是父节点 ) 
 5      r=pre[r]; // r 接着找他的上级,直到找到父节点 为止
 6      return r;//父节点驾到~~~
 7 }
 8 void join(int x,int y){  //我想让x节点和节点连成一条线
 9     int fx=find(x),fy=find(y);//寻找x,y的父节点 
10     if(fx!=fy)//x和y的父节点显然不是同一个
11     pre[fx]=fy;//让x的父节点成为y的子节点 
12 } 

题意:
  有n个学生属于m个团体,(0<n<=30000,0<=m<=500)一个学生可以属于多个团体。一个学生疑似患病,则他属于整个团体都疑似患病,已知0号学生疑似患病,求一共多少个学生患病。
思路:
  很经典的并查集的题目,找一个pre[]数组记录存储每一个以当前下标为根节点的集合的个体数目,最后输出0号的根节点对应的sum值,就是0号学生所在团体的人数。
  代码:

 1 #include"iostream"
 2 #include"algorithm"
 3 #include"cstdio"
 4 #include"cstring"
 5 using namespace std;    
 6 int pre[30000];
 7 int find(int x){//查找x父节点 
 8     int r=x; //委托r去找父节点 
 9     while(pre[r]!=r) //如果r的上级不是r自己(也就是说找到的节点他不是父节点 ) 
10      r=pre[r]; // r 接着找他的上级,直到找到父节点 为止
11      return r;//父节点驾到~~~
12 }
13 void join(int x,int y){  //我想让x节点和节点连成一条线
14     int fx=find(x),fy=find(y);//寻找x,y的父节点 
15     if(fx!=fy)//x和y的父节点显然不是同一个
16     pre[fx]=fy;//让x的父节点成为y的字节点 
17 } 
18 int main(){
19     std::ios::sync_with_stdio(false);
20     int n,m;    
21     while(cin>>n>>m){    
22     memset(pre,0,sizeof(pre));
23     if(n==0&&m==0)break;
24     for(int i=0;i<n;i++) pre[i] = i; 
25     while(m--){
26         int t, a,b;
27         cin>>t>>a;
28         for(int i=1;i<t;i++){
29             cin>>b;
30             join(a,b);
31             a=b;
32         }    
33     }
34         int sum=0;
35         for(int i=1;i<n;i++){
36             if(find(i)==find(0))
37             sum++;
38         }
39     cout<<sum+1<<endl;
40    }
41     return 0; 
42 }

poj 1611 :The Suspects经典的并查集题目

标签:repr   sel   follow   standard   查找   may   父节点   memset   tor   

原文地址:https://www.cnblogs.com/huangdf/p/12227121.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!