POJ - 1988 Cube Stacking(带权并查集)
2015-09-13 00:57
399 查看
Cube Stacking
Submit Status
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
Sample Output
题意:N个立方体放在N个堆里。有两种操作:
M a b:把a所在的堆移到b所在的堆上,堆本身的顺序不变。
C a:询问a下面有多少个立方体。
开两个数组deep[i]记录i点到根节点的距离,son[i]表示i节点下有多少儿子(包括自己),于是对询问可以得出答案为:
son[root]-deep[i]-1
压缩路径的时候要更新deep的值,合并吧b并到a节点的下面,更新deep和son。
Time Limit: 2000MS | Memory Limit: 30000KB | 64bit IO Format: %I64d & %I64u |
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个立方体放在N个堆里。有两种操作:
M a b:把a所在的堆移到b所在的堆上,堆本身的顺序不变。
C a:询问a下面有多少个立方体。
开两个数组deep[i]记录i点到根节点的距离,son[i]表示i节点下有多少儿子(包括自己),于是对询问可以得出答案为:
son[root]-deep[i]-1
压缩路径的时候要更新deep的值,合并吧b并到a节点的下面,更新deep和son。
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int MAXN = 30100; int fa[MAXN]; int deep[MAXN]; int son[MAXN]; void init() { for (int i = 0; i < MAXN; i++) { fa[i] = i; deep[i] = 0; son[i] = 1; } } int find(int t) { if (fa[t] == t) return t; int temp = fa[t]; fa[t] = find(temp); deep[t] += deep[temp]; return fa[t]; } void Move_A_to_B_top(int a, int b) { int roota = find(a); int rootb = find(b); if (roota != rootb) { fa[rootb] = roota; deep[rootb] = son[roota]; son[roota] += son[rootb]; } } int main() { char op; int a, b; int p; while (scanf("%d", &p) != EOF) { init(); for (int i = 0; i < p; i++) { cin >> op; if (op == 'M') { scanf("%d%d", &a, &b); Move_A_to_B_top(a, b); } else { scanf("%d", &a); int root = find(a); printf("%d\n", son[root] - deep[a] - 1); } } } }
相关文章推荐
- 阿尼玛
- 《华油能源OA系统数据同步和扩展的设计与实现_张宇峰》阅读笔记
- C语言之函数值传递的好处
- c#泛型冒泡排序法
- [Android] AudioTrack::start
- 第一阶段总结
- C语言之argument和parameter的区别
- C语言之char类型研究
- 牛客——分遗产
- 在系统编程ISP与在应用编程IAP
- org.junit.runner.RunWith的bug
- ZOJ 3820 Building Fire Stations(二分+BFS)
- 0913-关于百度注册的案例
- lua5.2模块注册
- opencv2.0 随机数产生器用法
- PHP中两个整形的赋值所占用空间大小不一样,求解
- JAVA基础学习(九)--内部类
- 【学习笔记】Cocos2d-JS 如何创建精灵?(一)
- Android应用开发之(通过ClipboardManager, ClipData进行复制粘贴)
- 基础学习day08---多态、简单工厂、Object类equals和toString