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