Cube Stacking
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 17959 | Accepted: 6208 | |
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<cstdio>
#define Maxn 30010
using namespace std;
int fa[Maxn],dis[Maxn],height[Maxn];
int findset(int x){
if(fa[x]!=x){
int t=fa[x];
fa[x]=findset(fa[x]);
dis[x]+=dis[t];
}
return fa[x];
}
void unionset(int x,int y){
x=findset(x),y=findset(y);
if(x==y) return;
fa[y]=x;
dis[y]=height[x];
height[x]+=height[y];
}
int main()
{
int p,a,b;
char s;
cin>>p;
for(int i=0;i<Maxn;i++) fa[i]=i,dis[i]=0,height[i]=1;
for(int i=0;i<p;i++){
cin>>s;
if(s=='M') {cin>>a>>b;unionset(a,b);}
else{
cin>>a;
int t=findset(a);
cout<<height[t]-dis[a]-1<<endl;
}
}
return 0;
}
注:这题又是一道种类并查集,思想和食物链那道类似,关键这题的突破口是想到每个元素与并查集根的距离,通过每堆总结点数,即可换算出需要的结果,维护距离是在路径压缩时,这样可以做的原因,在我看来是,需要用到这点,这点必被查找必然更新。像这种并查集有带向量运算感,因此叫偏移向量也不为过。这题很明显距离是直接可加的,而食物链那题并不明显,那几个公式都要枚举所用情况,总结得到的,而且还带有模运算,但最终的公式回归到了向量运算。
我一直在想这是巧合吗?还是必然……其实我到现在还没完全想明白,但我更偏向巧合,因为食物链那题只有三种关系,并且必须设同类关系为0,否则就没有矢量性,而且巧合的是三种不同类生物构成一个矢量三角形……如果想要推广到n种关系,那么就不止要满足矢量n边形了,最重要的是必须满足加法性质,而且加完一圈可以绕回来,甚至从n点中任取m(m>=2)点,都需满足上述性质,这样才会有偏移矢量。
以上纯属个人看法,不喜勿喷!欢迎大牛前来指正!