POJ-1988 cube Stacking (weighted and checked)

Source: Internet
Author: User

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

Main topic:

I'm going to give you a cube with the same size numbered from 1 to 30000, and now I have 2 operations:

1.move 1,3 says put 1 on top of 3.

Another situation is: if there is a 2,3 below 1 and there is a 4, then move1,3 means that 1 of all the cubes placed on the top of 3 all cubes, and maintain the original 1 and 3 in the heap of the order of the cube. Move from top to bottom and then to 1,2,3,4. And only in this case.

2.count 3 means ask 3 there are several cubes underneath

First we consider that they stack together after the order can not change, can only move the whole, to ask is the number of cubes below. Similar to the number of nodes under the root node, so we can consider using and check set to solve the problem, so we need to use the pre array and the son array, to represent the node's parent node and the node of the total number of child nodes, and we open a child node to the root node of the distance array, Each time you update, you simply update the distance array of the child nodes that are merged into the root node to include the number of cubes in the pre-merged root node; When querying, you only need to use the total number of root nodes-the distance from the root node and then 1 to find out the number of cubes below this node.

Through this problem, I have a new understanding of the path compression of the set, and I found that in the right value of the path compression in the set when the change of its weight can only be changed when the path is compressed, other times can not change the weight of the compression path.

#include <stdio.h> #include <string.h> #include <algorithm> using namespace std;
const int N = 30000+10;
    struct node {int parent;
    int dis;
int son;
}p[n];
    int findset (int x) {if (p[x].parent = = x) return x;
    int rootx = p[x].parent;
P[x].parent = Findset (rootx);//compression path P[x].dis + = p[rootx].dis;//Change the weight value return p[x].parent;
    } int main () {int T, x, Y, C;
    char s;
        for (int i = 1; i < N; i++) {p[i].parent = i;
        P[i].dis = 0;
    P[i].son = 1;
    } scanf ("%d", &t);
        while (t--) {scanf ("%*c%c", &s);
            if (s = = ' M ') {scanf ("%d%d", &x, &y);
            int rootx = Findset (x);
            int rooty = Findset (y);
                if (rootx! = Rooty) {p[rooty].parent = Rootx;
        P[rooty].dis = p[rootx].son;//The distance from each update of the merged child node to the root node P[rootx].son + = p[rooty].son;//Each update the total number of root nodes}
      }  else {scanf ("%d", &c);
        printf ("%d\n", P[findset (c)].son-p[c].dis-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.