HDU 2818 Building Block (and check set)

Source: Internet
Author: User

Every time there is a new understanding.

Think there is no heap at the bottom for ancestors.

About the Findset is how to maintain, this time and a good understanding, the problem is each update x and X of the last layer of the node, from the bottom up (from the ancestor layer to itself).

1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <cstring>5#include <string>6#include <queue>7#include <algorithm>8#include <map>9#include <iomanip>Ten#include <climits> One#include <string.h> A#include <cmath> -#include <stdlib.h> -#include <vector> the#include <stack> -#include <Set> - #defineINF 1e7 - #defineMAXN 10010 + #defineMAXN 1000010 - #defineMod 1000007 + #defineN 1010 A using namespacestd; attypedefLong LongLL; -  - intN, M; - intfa[333333]; - intnum[333333]; - intun[333333]; in  - //take the bottom of every pile as ancestors to //update from ancestor to layer down one level + intFindset (intx) - { the     if(x = = Fa[x])returnx; *     intTMP =Findset (fa[x]); $UN[X] + =Un[fa[x]];Panax NotoginsengFA[X] =tmp; -     returnFa[x]; the } +  A voidMerg (intAintb) the { +     intx =Findset (a); -     inty =Findset (b); $     if(X! =y) { $UN[X] =Num[y]; -Num[y] + =Num[x]; -FA[X] =y; the     } - }Wuyi  the intMain () - { Wu     intA, B; -     Charch; About      while(~SCANF ("%d",&N)) { $ GetChar (); -          for(inti =0; I <= N; ++i) -Fa[i] = i, num[i] =1, un[i] =0; -  A          for(inti =0; I < n; ++i) { +scanf"%c",&ch); the             if(ch = ='M') { -scanf"%d%d",&a,&b); $ GetChar (); the Merg (A, b); the             } the             Else { thescanf"%d",&a); - GetChar (); in Findset (a); theprintf"%d\n", Un[a]); the             } About         } the     } the     return 0; the}

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