【原】 POJ 1611 The Suspects 并查集 解题报告

http://poj.org/problem?id=1611


方法:
由于需要求得0元素所在划分的元素数,所以采用union by size的方法
将所有等价元素union到一起,然后再输出0元素所在划分的元素数

注意:
union时需要事先判定两元素是否已经在同一个划分中,不然会出错

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

   1: #include <stdio.h>
   2: #include <iostream>
   3:  
   4: using namespace std ;
   5:  
   6: #define N 30000
   7:  
   8: int P[N] ;
   9:  
  10: int Find( int x )
  11: {
  12:     if( P[x] < 0 )
  13:         return x ;
  14:     return P[x] = Find( P[x] ) ;
  15: }
  16:  
  17: void UnionBySize( int x, int y )
  18: {
  19:     //找root
  20:     int root1 = Find(x) ;
  21:     int root2 = Find(y) ;
  22:     if( root1==root2 )  //*******注意当两个节点在相同root下的情况。因此re两次
  23:         return ;
  24:  
  25:     //求出两根之间的size比较
  26:     int bRoot = P[root1]<P[root2] ? root1 : root2 ;
  27:     int sRoot = P[root1]<P[root2] ? root2 : root1 ;
  28:  
  29:     P[bRoot] += P[sRoot] ;  //更新大小
  30:     P[sRoot] = bRoot ;      //小根指向大根
  31: }
  32:  
  33: void run1611()
  34: {
  35:     int n,m ;
  36:     int cnt,val1,val2 ;
  37:     int i,j ;
  38:  
  39:     while( scanf("%d%d", &n,&m) &&  n!=0 )
  40:     {
  41:         for( i=0 ; i<n ; ++i )
  42:             P[i] = -1 ;
  43:         for( i=0 ; i<m ; ++i )  //对于每一组
  44:         {
  45:             scanf( "%d%d" , &cnt,&val1 ) ;
  46:             --cnt ;
  47:             while( cnt-- && scanf( "%d", &val2 ) )
  48:                 UnionBySize(val1,val2) ;
  49:         }
  50:         printf( "%d\n", -P[Find(0)] );
  51:     }
  52: }
原文地址:https://www.cnblogs.com/allensun/p/1870090.html