Cube stacking POJ1988 "and the application of check set"

Source: Internet
Author: User

http://poj.org/problem?id=1988

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 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

6M 1 6C 1M 2 4M 2 6C 3C 4

Sample Output

102

Find out the number of subordinates of any one node

/* Take the right and check set, a bunch of the topmost block as the parent node, using dis[x] to count X to the Father node distance, Num[fa[x]] to indicate the size of the regiment, the subtraction is the answer */#include <stdio.h> #include <    string.h> #define N 30000+100//number of nodes int num[n];//records the number of this team int dis[n];//record a point to the root node of the distance int per[n];//represents the parent node void init () {        for (int i=0;i<n;++i) {num[i]=1;        dis[i]=0;    Per[i]=i;    }}int find (int x) {if (x==per[x]) return x;    int t=per[x];    Per[x]=find (Per[x]); dis[x]+=dis[t];//when looking for a number, return per[x];}    void join (int x,int y) {int fx=find (x);    int Fy=find (y);        if (fx!=fy) {per[fy]=fx;        DIS[FY]=NUM[FX];    NUM[FX]+=NUM[FY];    }}int Main () {int n;    int A, B;    Char ch;        while (~SCANF ("%d", &n)) {init ();            while (n--) {GetChar ();            scanf ("%c", &ch);                if (ch== ' M ') {scanf ("%d%d", &a,&b);            Join (A, b);                } else if (ch== ' C ') {scanf ("%d", &a);   int X=find (a);             printf ("%d\n", num[x]-dis[a]-1); }}} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Cube stacking POJ1988 "and the application of check set"

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.