Poj-1988--cube Stacking

Source: Internet
Author: User
Cube Stacking

Farmer John and Betsy is 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 is 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 is under the C Ube 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 the INTEGERS:X and y.for count operations, the line also contains a single in Teger:x.

Note that the value of N does not appear in the input file. No move operation would 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

Main topic:

There are some stacks (up to 30,000) that give you m operations, each with two commands: "M x y" and move, move the stack containing element x to the top of the stack containing the element y, "C x" and query, querying element x below the number of elements.

Problem Solving Ideas:

It is obvious that the stack element relationship is judged and the set is checked. The F array stores the root node, another two array top and Ans,top stores how many elements are above the element, and the ANS array stores the total number of stack elements that the element resides on. So, the element below the x element = the number of total elements on the stack of the X element-the element above the X element-1 (itself). See code comments for specific execution.

AC Code:

#include <stdio.h> #include <iostream> #include <string.h> using namespace std;
#define MAXN 30010 int F[MAXN], TOP[MAXN], ANS[MAXN]; An F array represents the topmost element (root) of the stack on which the element is located//top an array representing how many elements are above the element//ans array represents the total number of elements of the stack that contains the topmost element of the stack int getf (int v) {if (v = f[v])//If the element is not The topmost element of the stack (root), need to update the array {int t = f[v];//The element's current root f[v] = GETF (F[v]);//update the element at the top of the stack (root) top[v] + = Top [t];//the number of elements above this element is the number of itself plus the number of root elements} return f[v];//returns the topmost (root) element} void Lian (int t1, int t2) {int v1 = GETF (t1);// The topmost (root) element of the stack that needs to be moved is int v2 = GETF (t2);//the top (root) element of the stack before the stack is added if (v1! = V2)//Two is not the same stack (it is certainly not the same stack) {F[v 
    2] = v1;//the topmost (root) element of the new stack top[v2] = ans[v1];//all elements of the stack before the stack root element ans[v1] + = ans[v2];//The total number of elements of the new stack is the sum of the two stack elements
    }} int main () {int n; while (~SCANF ("%d", &n)) {for (int i = 1; i < MAXN; i++) {F[i] = i;//Initialize all the root nodes are self- Ans[i] = 1;//The number of elements on the stack where all elements are initialized is 1 (own one) top[i] = 0;//initializationNo elements above all elements} char cz;
        int T1, T2;
            for (int i = 0; i < n; i++) {cin>>cz;
                if (cz = = ' M ') {scanf ("%d%d", &t1,&t2);
            Lian (T1,T2);
                } else {scanf ("%d", &t1);
                printf ("%d\n", ANS[GETF (t1)]-top[t1]-1);
The number of elements below the T1 is the total number of elements on the stack-t1 the number of elements above the T1-itself}}} return 0;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.