poj 1611 求0号结点所在集合的元素个数

求0号结点所在集合的元素个数

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 <iostream>
 2 # include <cstdio>
 3 # include <cstring>
 4 # include <algorithm>
 5 # include <cmath>
 6 # include <queue>
 7 # define LL long long
 8 using namespace std ;
 9
10 const int MAXN = 30010;
11 int F[MAXN];
12 int num[MAXN] ;
13
14 int find(int x)//找x的祖先结点
15 {
16     if(F[x]==x) return x;
17     return F[x]=find(F[x]);
18 }
19 void bing(int u,int v) //按秩合并
20 {
21     int x = find(u);
22     int y = find(v);
23     if(x == y)
24         return ;
25     if(num[x] >= num[y])
26     {
27         F[y] = x;
28         num[x] += num[y];
29     }
30     else
31     {
32         F[x] = y;
33         num[y] += num[x];
34     }
35 }
36 int main()
37 {
38    // freopen("in.txt","r",stdin) ;
39     int n , m , k ;
40     while(scanf("%d %d", &n , &m) != EOF)
41     {
42          if (n == 0 && m == 0)
43              break ;
44
45          int i ;
46          for(i = 0 ; i < n ; i++)
47          {
48              F[i] = i ;
49              num[i] = 1 ;
50          }
51         int u , v ;
52         while(m--)
53         {
54             scanf("%d %d" , &k , &u) ;
55             k-- ;
56             while(k--)
57             {
58                 scanf("%d" , &v) ;
59                 bing(u , v) ;
60             }
61         }
62         printf("%d\n" , num[find(0)]) ; //集合元素的个数保存在祖先结点的num数组里
63
64
65     }
66     return 0;
67 }

时间: 2024-10-14 12:42:06

poj 1611 求0号结点所在集合的元素个数的相关文章

c语言经典算法—求0—7 所能组成的奇数个数

题目:求0—7 所能组成的奇数个数. 算法思想:这个问题其实是一个排列组合的问题,设这个数为sun=a1a2a3a4a5a6a7a8,a1-a8表示这个数的某位的数值,当一个数的最后一位为奇数时,那么这个数一定为奇数,不管前面几位是什么数字.如果最后一位数为偶数,则这个数一定为偶数.a1-a8可以取0-7这个八个数字,首位数字不为0.从该数为一位数到该数为8位数开始统计奇数的个数:1.当只有一位数时也就是该数的最后一位,奇数个数为42.当该数为两位数时,奇数个数为4*7=283.当该数为三位数时

POJ 1611 The Suspects (并查集+数组记录子孙个数 )

The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 24134   Accepted: 11787 Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. T

The Suspects POJ - 1611 并查集,同集合的数

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-Spread

More is better(hdu 1856 计算并查集集合中元素个数最多的集合)

More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others)Total Submission(s): 21167    Accepted Submission(s): 7720 Problem Description Mr Wang wants some boys to help him with a project. Because the project

并查集水题 POJ 1611

题意:有n(n<=30000)个学生,每个学生属于一个团体(可以属于多个团体),共有m(<=500)个团体,如果一个学生是嫌疑人,则他所在的团体的所有人都是嫌疑人,初始时0号学生是嫌疑人.问总共有多少个嫌疑人. 很明显同一个团体的学生可以连一条边,即求0号点所在的连通块有多少个点,用并查集可以很方便的办到,如果两个点属于同一个连通块则把他们的代表元连接起来即可,始终把较小的那个节点作为父节点,所以最后p[0]的节点数就是答案. 代码:

poj 1611:The Suspects(并查集,经典题)

The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 21472   Accepted: 10393 Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. T

kuangbin专题五:B - The Suspects POJ - 1611

B - The Suspects POJ - 1611 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 f

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

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-Spr

POJ 1611 The Suspects 并查集 Union Find

本题也是个标准的并查集题解. 操作完并查集之后,就是要找和0节点在同一个集合的元素有多少. 注意这个操作,须要先找到0的父母节点.然后查找有多少个节点的额父母节点和0的父母节点同样. 这个时候须要对每一个节点使用find parent操作.由于最后状态的时候,节点的parent不一定是本集合的根节点. #include <stdio.h> const int MAX_N = 30001; struct SubSet { int p, rank; }sub[MAX_N]; int N, M; v