Poj1988-cube stacking (with right and check set)

Source: Internet
Author: User
Tags faa
Cube Stacking
Time Limit: 2000MS Memory Limit: 30000K
Total Submissions: 18858 Accepted: 6547
Case Time Limit: 1000MS

Description 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 in the stack with Cube x is under the cube X and report that value.  br>
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
Test instructions: N Blocks (n<=30000), p Group operation (p<=100000), the operation of two, M a B will contain a stacked on the heap containing B, there is a C a statistic a how many blocks below
Idea: With the right and check set, a bunch of the topmost block as the parent node, with Dist[x] statistics X to the Father node distance, Rank[fa[x]] to indicate the size of the regiment, the subtraction is the answer
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <string&
Gt
#include <algorithm> #include <queue> using namespace std;
const int MAXN = 30000+10;
int FA[MAXN];
int RANK[MAXN];
int DIST[MAXN];
        void Init () {for (int i = 0; i < MAXN; i++) {fa[i] = i;
        Rank[i] = 1;
    Dist[i] = 0;
        }} int find (int x) {if (x = fa[x]) {int t = fa[x];
        FA[X] = find (fa[x]);
    DIST[X] + = dist[t];
} return fa[x];
    } int main () {int n;
        while (~SCANF ("%d", &n)) {init ();
        Char op;
            while (n--) {cin >> op;
            int A, B;
                if (op== ' M ') {scanf ("%d%d", &a,&b);
                int FaA = Find (a);
                int FaB = find (b);
                    if (FAA! = FaB) {Fa[fab] = FAA;
                    Dist[fab] = Rank[faa];
                RANK[FAA] + = Rank[fab];}}else{scanf ("%d", &a);
                int x = Find (a);
            printf ("%d\n", rank[x]-dist[a]-1);
}}} 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.