Cube Stacking P0J 1988(加权并查集)

Posted wkfvawl

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Cube Stacking P0J 1988(加权并查集)相关的知识,希望对你有一定的参考价值。

Description

Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)identical cubes labeled 1 through N. They start with N stacks, each containing a single cube. Farmer John asks Betsy to perform P (1<= P <= 100,000) operation. There are two types of operations:  moves and counts.  * In a move operation, Farmer John asks Bessie to move the stack containing cube X on top of the stack containing cube Y.   * In a count operation, Farmer John asks Bessie to count the number of cubes on the stack with cube X that are under the cube X and report that value. 
Write a program that can verify the results of the game. 

Input

* Line 1: A single integer, P 
* Lines 2..P+1: Each of these lines describes a legal operation. Line 2 describes the first operation, etc. Each line begins with a ‘M‘ for a move operation or a ‘C‘ for a count operation. For move operations, the line also contains two integers: X and Y.For count operations, the line also contains a single integer: X. 
Note that the value for N does not appear in the input file. No move operation will request a move a stack onto itself. 

Output

Print the output from each of the count operations in the same order as the input file. 

Sample Input

6
M 1 6
C 1
M 2 4
M 2 6
C 3
C 4

Sample Output

1
0
2

题目意思:有n块方块,p次操作,每次操作有两种类型,‘M a b‘ 意思是将含有方块a的堆挪到b上:‘C a’意思是查询方块a下面有几个方块。
解题思路:

本题目使用并查集来进行计算。 sum[n]数组来统计当前堆n中方块个数。 under[n]数组来统计当前n下面的方块个数。 在进行计算的时候,路径压缩和合并均更新under的值。 未进行路径压缩的时候,方块n所记录的under并非final value, 仅仅只是包含了到root[n]的方块个数。 通过路径压缩的过程,不断的增加当前n的根节点(递归)的under值,求出最终的under值。进行路径合并的时候,更新sum值和under值。当一个堆放在另一个堆上时,被移动的堆的under值一定为0,  此时更新under值为另一个堆的根的sum值,即计算出了此处的部分under值。然后更新合并堆的sum值


 1 #include<cstdio>
 2 #include<cstring>
 3 #define maxs 30010
 4 using namespace std;
 5 int pre[maxs];
 6 int sum[maxs];
 7 int under[maxs];
 8 void init()
 9 {
10     int i;
11     for(i=0; i<maxs; i++)
12     {
13         pre[i]=i;
14         under[i]=0;
15         sum[i]=1;
16     }
17 }
18 int Find(int x)
19 {
20     int t;
21     if(x==pre[x])
22     {
23         return x;
24     }
25     t=Find(pre[x]);
26     under[x]+=under[pre[x]];
27     pre[x]=t;
28     return pre[x];
29 }
30 void Union(int x,int y)
31 {
32     int a=Find(x);
33     int b=Find(y);
34     if(a==b)
35     {
36         return ;
37     }
38     pre[a]=b;
39     under[a]=sum[b];///将a堆放在b堆之上,更新此时a堆下面的数量
40     sum[b]+=sum[a];///将a堆和b堆合为一个整体,更新整体新堆的数量
41 }
42 
43 int main()
44 {
45     int n;
46     char q;
47     int a,b;
48     scanf("%d",&n);
49     getchar();
50     init();
51     while(n--)
52     {
53         scanf("%c",&q);
54         if(q==M)
55         {
56             scanf("%d%d",&a,&b);
57             getchar();
58             Union(a,b);
59         }
60         else if(q==C)
61         {
62             scanf("%d",&a);
63             getchar();
64             b=Find(a);
65             printf("%d
",under[a]);
66         }
67     }
68     return 0;
69 }

 

 

以上是关于Cube Stacking P0J 1988(加权并查集)的主要内容,如果未能解决你的问题,请参考以下文章

poj 1988 Cube Stacking

POJ 1988 Cube Stacking

poj1988_Cube Stacking

POJ 1988 Cube Stacking (带权并查集)

POJ 1988 Cube Stacking

POJ 1988 Cube Stacking