Cube Stacking
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 24990 | Accepted: 8750 | |
Case Time Limit: 1000MS |
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.
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.
* 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
Source
先说下题意,在进行count操作的时候,求的是当前节点下面节点的个数,在进行move操作时,就是把x中的节点放在y栈的栈顶,题目要求的是输出在进行count操作时的结果。
思路:题目让求得是当前节点以下的节点的个数,所以可以用并查集来求解。用一个数组在记录该节点的根结点以下节点的个数,另一个数组记录该节点到根结点的距离,即与根结点之间间隔的节点数,因为所有的节点都在栈中,所以节点形成的树是线性的,所以距离就等于间隔的节点数。最后要求解的该节点以下的节点的个数=该节点根结点以下的个数-从该节点到根结点的距离-本身。
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
int par[30005],s[30005],node[30005];//s[i]用来表示当前结点与根结点之间的距离,node[i]表示以i为根结点的节点的个数
int find_set(int x){
if(x == par[x])
return x;
int t = par[x];
par[x] = find_set(par[x]);
s[x] += s[t]; //计算x节点与根结点之间的距离,因为节点存储在栈中,所以是一个线树
return par[x];
}
void UnoinSet(int u, int v){
int x = find_set(u);
int y = find_set(v);
if(x != y){
par[y] = x;
s[y] = node[x]; //当前节点与根结点之间相差原先以x为根的树的节点的个数
node[x] += node[y];//以x为根的节点的总个数
}
}
int main(){
int n;
while(~scanf("%d",&n)){
char ch;
int x,y;
for(int i = 1; i <= 30000; i ++){
par[i] = i;
s[i] = 0;
node[i] = 1;
}
for(int i = 1; i <= n; i ++){
cin >> ch;
if(ch == 'M'){
scanf("%d%d",&x,&y);
UnoinSet(x,y);
}
else{
scanf("%d",&x);
printf("%d\n",node[find_set(x)] - s[x] - 1);//全部节点数减去当前节点到根结点的节点数再减去本身
}
}
}
return 0;
}