hdu 2818 Building Block(并查集)

每次总有新领悟。。

认为没堆最底层为祖先。

关于findset是怎么维护的,这次又好好理解了一下,这道题是每次更新x和x的上一层的结点,自底层向上(从祖先一层一层到本身)。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<string>
 6 #include<queue>
 7 #include<algorithm>
 8 #include<map>
 9 #include<iomanip>
10 #include<climits>
11 #include<string.h>
12 #include<cmath>
13 #include<stdlib.h>
14 #include<vector>
15 #include<stack>
16 #include<set>
17 #define INF 1e7
18 #define MAXN 10010
19 #define maxn 1000010
20 #define Mod 1000007
21 #define N 1010
22 using namespace std;
23 typedef long long LL;
24
25 int n, m;
26 int fa[333333];
27 int num[333333];
28 int un[333333];
29
30 //取每一堆最底部的为祖先
31 //从祖先向下一层一层更新
32 int findset(int x)
33 {
34     if (x == fa[x]) return x;
35     int tmp = findset(fa[x]);
36     un[x] += un[fa[x]];
37     fa[x] = tmp;
38     return fa[x];
39 }
40
41 void merg(int a, int b)
42 {
43     int x = findset(a);
44     int y = findset(b);
45     if (x != y) {
46         un[x] = num[y];
47         num[y] += num[x];
48         fa[x] = y;
49     }
50 }
51
52 int main()
53 {
54     int a, b;
55     char ch;
56     while (~scanf("%d",&n)) {
57         getchar();
58         for (int i = 0; i <= n; ++i)
59             fa[i] = i, num[i] = 1,un[i] = 0;
60
61         for (int i = 0; i < n; ++i) {
62             scanf("%c ",&ch);
63             if (ch == ‘M‘) {
64                 scanf("%d%d",&a,&b);
65                 getchar();
66                 merg(a, b);
67             }
68             else {
69                 scanf("%d",&a);
70                 getchar();
71                 findset(a);
72                 printf("%d\n",un[a]);
73             }
74         }
75     }
76     return 0;
77 }
时间: 2024-12-27 10:50:49

hdu 2818 Building Block(并查集)的相关文章

hdu 2818 Building Block(带权并查集)

题目: 链接:点击打开链接 题意: 有N个积木,1到N编号.进行一些操作P次,M:X Y把X积木放到Y的上面,如果X和Y相等请忽略.C:X 计算X积木下的积木数目. 思路: 带权并查集的题目,定义数组sum[i]表示i积木下面积木的数目.遇到操作M,就把X和Y合并到同一个集合中.我们视每个结点为1个 Pile,其中rank[i]就表示每个Pile处的积木的个数,Initially, there are N piles, and each pile contains one block.所以,ra

hdu 2818 Building Block【基础带权并查集】

Building Block Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3365    Accepted Submission(s): 1009 Problem Description John are playing with blocks. There are N blocks (1 <= N <= 30000) numbe

HDU 2818 Building Block 加权并查集

Building Block                                                                      Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4548    Accepted Submission(s): 1408 Problem Description John

hdu 2818 Building Block (带权并查集,很优美的题目)

Problem Description John are playing with blocks. There are N blocks (1 <= N <= 30000) numbered 1...N.Initially, there are N piles, and each pile contains one block. Then John do some operations P times (1 <= P <= 1000000). There are two kinds

?HDU 2818 Building Block 求大神讲解 (并查集)

#include <stdio.h> #include <iostream> using namespace std; int ff[30005];//ff[x]表示x的父节点 int high[30005]; int low[30005]; void ii(int n) //初始化 { int i; for(i=0;i<=n;i++) //从1开始也是错误的 { ff[i]=i; low[i] = 0; high[i] = 1; } } int dd(int x) //带路

HDU 2818 Building Block(带权并查集)

[题目链接]:Click here~~ [题意]: 给 n 块砖头,开始各为一堆,两种操作: 1.把 X 所在的那一堆箱子里的砖头放到 Y 所在的那一堆上面. 2.询问 X 下面有多少块砖. [解题思路]:好像大家都叫它带权并查集,那为了方便,这里也这样叫吧,因为涉及前面的和后面的箱子个数,对应的查找操作,一开始想用结构体来写,在结构体里定义每个箱子的前驱和后继,每次输入的时候统计一下相应的前驱和后继的个数,后来发现不行,因为涉及到合并操作,比如说M 2 4 M 2 6连续出现两个2,用结构体是

hdu 2818 Building Block

Building Block Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3986    Accepted Submission(s): 1235 Problem Description John are playing with blocks. There are N blocks (1 <= N <= 30000) numbe

HDU 3635 Dragon Balls(并查集)

Dragon Balls Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 64   Accepted Submission(s) : 26 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Five hundred years later

hdu 3234 Exclusive-OR (并查集+异或性质)

Exclusive-OR Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2177    Accepted Submission(s): 603 Problem Description You are not given n non-negative integers X0, X1, ..., Xn-1 less than 220 ,