Test Instructions:Link
Method:Can and Heap
parsing:The first day of the second to forget, said can and heap, so to find the naked topic to brush. For a merge operation, if one is empty then the root is another. Then we may as well assume that the X key value is less than Y. So the left-leaning tree is the left-leaning tree, so the whole process forms a recursive one that merges the right subtree of x with the subtree of y as the root. After merging, the left side is maintained, that is, the left side of NPL is greater than right. The root node's NPL is then updated. In fact, this is the whole process of merging, only to deepen the memory of the narrative.
Code:
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define N 1000010using namespace STD;intch[n][2],h[n],key[n],fa[n];BOOLCol[n];intFindintx) {returnX==fa[x]?x:fa[x]=find (Fa[x]);}intMergeintXintY) {if(!x)returnYif(!y)returnXif(Key[y]<key[x]) swap (x, y); ch[x][1]=merge (ch[x][1],y);if(h[ch[x][1]]>h[ch[x][0]]) Swap (ch[x][0],ch[x][1]);if(!h[ch[x][1]]) h[x]=0;Elseh[x]=h[ch[x][1]]+1;returnx;}intN,q;Chars[5];intMain () {scanf("%d", &n); for(intI=1; i<=n;i++)scanf("%d", &key[i]); for(intI=1; i<=n;i++) fa[i]=i; h[0]=-1;scanf("%d", &q); for(intI=1; i<=q;i++) {scanf('%s ', s);if(s[0]==' M ') {intx, y;scanf("%d%d", &x,&y);if(col[x]| | Col[y])Continue;intFx=find (x), Fy=find (y);if(FX!=FY) {intT=merge (FX,FY); fa[fx]=t,fa[fy]=t; } }Else{intXscanf("%d", &x);if(Col[x])printf("%d\n",0);Else{intFx=find (x); col[fx]=1;printf("%d\n", Key[fx]); Fa[fx]=merge (ch[fx][0],ch[fx][1]); FA[FA[FX]]=FA[FX]; } } }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Bzoj 1455 Roma games can be stacked naked