poj 1988 Cube Stacking
Cube Stacking
| Time Limit: 2000MS | Memory Limit: 30000K | |
| Total Submissions: 13922 | Accepted: 4695 | |
| 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
并查集的题目,但是需要加一个小技巧,用deep来保存该节点在整个一叠冰块中从上到下的位置,用count保存以该节为根时下面的子节点数目
每次更新时需要更新deep的值,而count的值只要更新根节点的那一个就可以了,中间节点不用管。
做这种题尽量用按秩合并的方法,不然很麻烦 还会很慢
就这么稍微变了一下又费了我n久。。最后还是学了网上的代码才过了。。。
cin>>非常慢,差点超时,用scanf好些
#include <iostream> #include <cstdio> using namespace std; const int N=30010; int father[N+1]; int deep[N+1]; int count[N+1]; void make_set(int x) { father[x]=x; deep[x]=0; count[x]=1; } int find_father(int x) { int temp; if(x!=father[x]) { temp=father[x]; father[x]=find_father(father[x]); deep[x]+=deep[temp]; } return father[x]; } void union_set(int a, int b) { int pa, pb; pa=find_father(a); pb=find_father(b); father[pb]=pa; deep[pb]=count[pa]; count[pa]+=count[pb]; } int main() { int p; int a,b; char op; int pa,pb; int cnt; freopen("e:\\data.txt", "r", stdin); freopen("e:\\out.txt", "w", stdout); cin>>p; for(int i=1; i<=N; i++) { make_set(i); } while(p--) { cin>>op; if(op=='M') { cin>>a>>b; pa=find_father(a); pb=find_father(b); if(pa!=pb) { union_set(a,b); } cout<<"count"<<endl; for(int i=1;i<10;i++) cout<<count[i]<<" "; cout<<endl; cout<<"deep"<<endl; for(int i=1;i<10;i++) cout<<deep[i]<<" "; cout<<endl; } else if(op=='C') { cin>>a; cnt=0; pa=find_father(a); cnt=count[pa]-deep[a]-1; cout<<cnt<<endl; } } return 0; }
浙公网安备 33010602011771号