POJ1988 Cube Stacking

Source: Internet
Author: User

    • The main topic: some cubes, initially each cube in a separate stack, a total of n. Supports two operations, that is, to move the stack containing the cube to the top of the stack containing the cube (two stack merges) and the number of cubes below the query cube.

    • Idea: The stack top element is the representative element of each collection, and the path compresses the distance (dist[]) of each node to represent the element, and the number of elements (cnt[]) that are represented by each element.

    • The code is as follows:

#include <iostream>#include <cstdio>using namespace STD;Const intmaxn=30005;intM,FA[MAXN],CNT[MAXN],DIST[MAXN];intx, y;intFindintx) {if(fa[x]==x)returnXintFx=find (Fa[x]); DIST[X]+=DIST[FA[X]];returnFA[X]=FX;}voidUniteintXintY) {intFx=find (x);intFy=find (y);    FA[FY]=FX;    DIST[FY]=CNT[FX]; CNT[FX]+=CNT[FY];}intMain () {intx, y;CharChscanf("%d\n", &m); for(intI=1; i<=maxn;++i) {fa[i]=i; cnt[i]=1; dist[i]=0; } while(m--) {scanf("%c", &ch);if(ch==' M ')        {scanf("%d%d\n", &x,&y);        Unite (x, y); }Else{scanf("%d\n", &x);intFx=find (x);printf("%d\n", cnt[fx]-dist[x]-1); }    }return 0;}

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

POJ1988 Cube Stacking

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.