Cube Stacking
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 24288 | Accepted: 8512 | |
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
题目分析:其实对于每一段来说,最重要的不是最上面一点或是最下面一点。想办法把他串起来是没有必要的。我们要找的只是他的节点,每一段的根节点才是最重要的。对于一个数来说,首先找他到他的父亲节点的距离,如果他的那一段被另一段压底下了,那么他的父亲节点肯定又会有一个父亲节点(因为在连接的过程中把底下段的根节点的父亲赋给上面段),然后在加上这一段距离,依次类推上去,可以找到此数字到最终节点的距离,然后用最终节点的权值减去这个距离就是这个数字节点的值。
代码如下:
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxx=30000+50;
int ji[maxx];//存储每个节点的值
int cun[maxx];//存储每个节点到他的父节点的距离
int father[maxx];
int findnum(int x)
{
if(x!=father[x])
{
int root= findnum(father[x]);
cun[x]+=cun[father[x]];
return father[x]=root;
}
else {
return x;
}
}
void init()
{
for(int i=0; i<=maxx; i++)father[i]=i,ji[i]=0;
memset(cun,0,sizeof(cun));
}
int main()
{
ios_base::sync_with_stdio(false);
int p,a,b;
cin>>p;
char c;
init();
while(p--)
{
cin>>c;
if(c=='M')
{
cin>>a>>b;
int b1=findnum(b);
int a1=findnum(a);
father[b1]=a1;
cun[b1]=ji[a1]+1;
ji[a1]=ji[a1]+ji[b1]+1;
}
else
{
cin>>a;
int root=findnum(a);
cout<<ji[root]-cun[a]<<endl;
}
}
return 0;
}