Cube stacking
Time limit:2000 ms |
|
Memory limit:30000 K |
Total submissions:14901 |
|
Accepted:5037 |
Case time limit:1000 ms |
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
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 6C 1 M 2 4 M 2 6C 3C 4
Sample output
102
Source
In the hyper-era, although the function is implemented, it is always time-out, because there is no real grasp and query set usage method:
/* * Method 1: timeout. Here a lower is used to locate the next one.CubeBlock, * Here we use a loop, so we will traverse the lower cube every time, resulting in timeout. */ # Include <Iostream># Include <Stdio. h> # Define Maxnum30001 Using Namespace STD; typedef Struct { Int Count; // Record the number of all cubes below the cube Int Parent; // Point to the cube at the top of the heap where the cube is located Int Lower; // The block pointing to the bottom of the current cube } Cube; cube [maxnum]; Void Init (){ For ( Int I = 1 ; I <maxnum; I ++ ) {Cube [I]. Count = 0 ; // Initialize 0 at the bottom of each cube Cube [I]. Parent = I; // Initialize each cube as a single heap and the top of the current heap Cube [I]. Lower =0 ;}} // Merge the heap of X and Y Void Union_set ( Int X, Int Y ){ Int Lower_parent; // The parent node of the grid in the heap below Int Lower_cube; // Lower lattice subscript Int Parent;// Parent node subscript of the top heap Parent = Cube [X]. parent; lower_parent = Cube [Y]. parent; lower_cube = X; // Update the stack above. All the Count values under the current cube must change. While (Cube [lower_cube]. Lower! = 0 ) {Cube [lower_cube]. Count = Cube [lower_parent]. Count + 1 ; Lower_cube =Cube [lower_cube]. Lower;} cube [lower_cube]. Count = Cube [lower_parent]. Count + 1 ; // At the same time, the bottom of the bottom block is updated, that is, the top block of the bottom stack. Cube [lower_cube]. Lower = Lower_parent; // Update the parent nodes of all the blocks below, which becomes a large heap While (Cube [lower_parent]. Lower! = 0 ) {Cube [lower_parent]. Parent = Parent; lower_parent =Cube [lower_parent]. Lower;} cube [lower_parent]. Parent = Parent ;} Int Main (){ // There are P operations Int P; // Command type, // M x Y: Move the heap where X cube is located to the heap where y cube is located. // C x outputs the number of cubes under X on the heap where X is located. Char Ch; Int X, Y; Init (); scanf ( " % D " ,& P ); While (P -- ) {Scanf ( " \ N % C " ,& Ch ); // Getchar (); // Cin> CH; If (CH = ' M ' ) {Scanf ( " % D " , & X ,& Y); union_set (x, y ); // Merge set } Else If (CH = ' C ' ) {Scanf ( " % D " ,& X); printf ( " % D \ n " , Cube [X]. Count );}} Return 0 ;}
Query the ACCode
# Include <iostream># Include <Stdio. h> # Define Maxnum30010 Using Namespace STD; typedef Struct { Int Count; // Record the number of all cubes below the cube Int Parent; // Point to the cube at the top of the heap where the cube is located Int Num;} cube; cube [maxnum]; Void Init (){ For ( Int I = 1 ; I <maxnum; I ++ ) {Cube [I]. Count = 0 ; // Initialize 0 at the bottom of each cube Cube [I]. num = 1 ; Cube [I]. Parent =- 1 ; // Initializes each cube as a single heap and is at the bottom of the current heap. }} Int Find ( Int A ){ // Point to yourself, indicating that you are the root If (Cube [A]. Parent =- 1 ) Return A; Int TEM = Cube [A]. parent; cube [A]. Parent = Find (cube [A]. Parent ); // The compression path, so that all the roots are the same Cube [A]. Count + = cube [TEM]. count; // Update the number below the current block, which is Return Cube [A]. parent; // Returns the ultimate root. } Void Union_set ( Int X, Int Y) {cube [X]. Parent = Y; cube [X]. Count + = Cube [Y]. Num; cube [Y]. Num + =Cube [X]. Num ;} Int Main (){ // There are P operations Int P; // Command type, // M x Y: Move the heap where X cube is located to the heap where y cube is located. // C x outputs the number of cubes under X on the heap where X is located. Char Ch; Int X, Y, RX, Ry; Init (); scanf ( " % D " ,& P ); While (P -- ){ // Scanf ("% s", CH ); Cin> Ch; If (CH = ' M ' ) {Scanf ( " % D " , & X ,& Y); RX = Find (x); ry = Find (y ); If (RX! = Ry) union_set (RX, ry ); // Merge set } Else If (CH = ' C ' ) {Scanf ( " % D " ,& X); find (x); printf ( " % D \ n " , Cube [X]. Count );}} Return 0 ;}