HDU 2818 Building Block (with right and check set)

Source: Internet
Author: User

"topic link": Click here~~

"Test Instructions":

Give n bricks, start each pile, two operations:

1. Put the bricks in the pile of boxes where X is located on the pile where Y is.

2. Ask how many bricks there are under X.

"problem-solving ideas": It seems that everyone called it with right and check set, that for convenience, here also called it, because of the number of cases involved in the front and back, the corresponding search operation, initially want to use the structure to write, in the structure of the definition of each box precursor and successor, Each time the input of the corresponding precursor and the number of successors, and later found no, because it involves the merger operation, for example, M 2 4 m 2 6 consecutive two 2, with the structure is not easy to achieve, at this time to play and check the effect of the set, when the merger of the update (1) sum[i]=1;// I where the total number of boxes, (2) upsum[i]=0;//i the number of boxes below, the last c a when the direct output UPSUM[A]

Code:

/*author:hrw with right and check set */#include <bits/stdc++.h>using namespace Std;const int N=30005;int Father[n],sum[n],upsum[n]        , N,m,a,b,t;char ch[4];int Find (int x) {if (x!=father[x]) {int temp=find (father[x]);        UPSUM[X]+=UPSUM[FATHER[X]];    Father[x]=temp; } return father[x];}    void Union (int a,int b) {int pa=find (a);    int Pb=find (b);    if (PA!=PB) {upsum[pa]=sum[pb];//Merges the total number of boxes under the PB for all chests sum[pb]+=sum[pa];//pb the heap father[pa]=pb;//merge root node        }}void Init () {for (int i=0; i<n; i++) {father[i]=i;        sum[i]=1;//i the total number of boxes upsum[i]=0;//i the number of boxes below}}int main () {while (scanf ("%d", &t)!=eof) {init ();            for (int i=0; i<t; i++) {scanf ("%s", ch);                if (ch[0]== ' M ') {scanf ("%d%d", &a,&b);            Union (A, b);                } else{scanf ("%d", &a);                Find (a);            printf ("%d\n", Upsum[a]); }}} return 0;} 


HDU 2818 Building Block (with right and 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.