poj 2524 求连通分量(并查集模板题)

求连通分量

Sample Input

10 9
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
10 4
2 3
4 5
4 8
5 8
0 0
Sample Output

Case 1: 1
Case 2: 7

 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 = 50010;
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 ;
40     int Case = 0 ;
41     while(scanf("%d %d", &n , &m) != EOF)
42     {
43          Case++ ;
44          if (n == 0 && m == 0)
45              break ;
46
47          int i ;
48          for(i = 1 ; i <= n ; i++)
49          {
50              F[i] = i ;
51              num[i] = 1 ;
52          }
53         int u , v ;
54         while(m--)
55         {
56             scanf("%d %d" , &u , &v) ;
57             bing(u , v) ;
58
59         }
60         int res = 0 ;
61         for(i = 1 ; i <= n ; i++)
62             if (F[i] == i)
63                res++ ;
64         printf("Case %d: %d\n" , Case , res) ;
65     }
66     return 0;
67 }

时间: 2024-09-28 13:31:10

poj 2524 求连通分量(并查集模板题)的相关文章

POJ 2524 Ubiquitous Religions (幷查集)

Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23090   Accepted: 11378 Description There are so many different religions in the world today that it is difficult to keep track of them all. You are interested in findi

hdu 1213 求连通分量(并查集模板题)

求连通分量 Sample Input2 //T5 3 //n m1 2// u v2 34 5 5 12 5 Sample Output24 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <cmath> 6 # include <queue> 7 # define LL long

POJ 2524 (简答并查集) Ubiquitous Religions

题意:有编号为1到n的学生,然后有m组调查,每组调查中有a和b,表示该两个学生有同样的宗教信仰,问最多有多少种不同的宗教信仰 简单并查集 1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 using namespace std; 6 7 const int maxn = 50000 + 10; 8 int parent[maxn], n, m; 9 10

PAT甲题题解-1114. Family Property (25)-(并查集模板题)

题意:给出每个人的家庭成员信息和自己的房产个数与房产总面积,让你统计出每个家庭的人口数.人均房产个数和人均房产面积.第一行输出家庭个数,随后每行输出家庭成员的最小编号.家庭人口数.人均房产个数.人均房产面积. 并查集,合并的时候编号小的作为父亲节点,最后父亲节点一样的即属于一个家庭,其它都是细节处理没啥好说了. #include <iostream> #include <cstdio> #include <algorithm> #include <string.h

hdu 1232 变成生成树至少还要加几条边 (并查集模板题)

求一个图 变成生成树至少还要加几条边(成环的边要删掉,但不用统计) Sample Input4 2 //n m1 3//u v4 33 31 21 32 35 21 23 5999 00 Sample Output102998 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <cmath> 6

How Many Tables——并查集模板题

题目链接 题意: n个人参加晚宴;完全不认识的两个人不能被分配在同一餐桌;认识具有传递性:A认识B B认识C,那么A和C也认识. 题解: 将认识两个人合并到同一集合:最后统计有多少个不同的集合即可; 代码: #include<iostream> #include<stdio.h> #include<math.h> using namespace std; typedef long long ll; const int maxn=5e5+5; int f[maxn]; i

[ACM] POJ 1611 The Suspects (并查集,输出第i个人所在集合的总人数)

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

[ACM] POJ 3295 Ubiquitous Religions (并查集)

Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23093   Accepted: 11379 Description There are so many different religions in the world today that it is difficult to keep track of them all. You are interested in findi

并查集模板

//普通并查集模板 #include<iostream> using namespace std; const int MAX=10004; int fat[MAX];//存放每个节点的根节点 //找x的根节点并且把路径上的每个节点的父节点改成根节点 int find(int x) //while找根节点 { int rt=x; while(fat[rt]!=rt) rt=fat[rt]; int i=x,j; while(i!=rt){ j=fat[i]; fat[i]=rt; i=j; }