Bzoj 1078: [SCOI2008] Oblique heap

Source: Internet
Author: User

1078: [SCOI2008] Oblique heap time limit:10 Sec Memory limit:162 MB
submit:770 solved:422
[Submit] [Status] [Discuss] Description

The inclined heap (skew heap) is a common data structure. It is also a binary tree and satisfies the same heap properties as a two-fork heap: The value of each non-root node
Is bigger than his father. Therefore, the value of the root is the smallest in the whole inclined heap. But the oblique heap does not have to be balanced, and the size of the left and right sons of each node does not have any
Regulations. In the subject, the values of each element in the oblique heap are different. The process of inserting a new element x in the oblique heap h is recursive: When H is empty or X
X becomes a new root when it is less than the root node of H, and the original root (if any) becomes the left son of X. When x is greater than the root node of H, the H-root node
Two subtrees trees are exchanged, and X (recursion) is inserted into the left subtree after the interchange. Give a slanted heap containing a node with a value of 0~n each time. Ask for a knot.
Sequence so that the oblique heap can be obtained by inserting these nodes sequentially in an empty tree. If the answer is inflexible one, the solution with the smallest dictionary order is output. The input is guaranteed to have
Solution.

Input

The first line contains an integer n. The second line contains n integers d1, d2, ..., dn, di < 100 means I is the left son of Di, di>=100 means I
It's di-100 's right son. Obviously 0 is always the root, so the input does not contain D0.

Output

Only one row, containing the n+1 integer, which is the insertion sequence with the smallest dictionary order.

Sample Input6
0 101 102 1 2Sample Output0 1 2 3 4 5 6HINT Source [Submit] [Status] [Discuss]

Since each insertion point is the first to swap the left and right subtrees on the current point, then insert the new point into the Zuozi (if the new node > current node), so you can know that each point if there is no left subtree, it will not be possible to have a sub-tree.

Consider the order of the insertion point backwards, focusing on the last inserted node in the current state. You can know that this node before reaching his position, must be constantly to Zuozi, so you can think that this point must be a "left-wing point", that is, from the root node to it needs to go all the way. And this point must have no right subtree, because the original point in this position (if any) has now moved to the new point of the left sub-tree, so the new point of the right son must be empty. The points satisfying these two properties are not necessarily unique, but we should select the minimum depth to meet the requirements of the point. Consider that if you select a point with a greater depth as the last point of insertion, one of its ancestors satisfies the two properties mentioned above, then when the point is inserted it must have passed its ancestor, and the two subtrees of its ancestors have been exchanged, and we now swap back, there are only right subtrees, and the left subtree is empty illegal case, is inconsistent with the conclusions mentioned at the beginning, so this point is not the last point to insert. But there is a special case, that is, the point is the leaf node, and its only satisfied with the two properties of the ancestor is its parent node, it is not difficult to find that the chance coincidence that the point has become a legitimate final insertion point. Based on the requirement of the minimum dictionary order, we should first output this large point to the end of the answer sequence, so we need to select this point as the last insertion point instead of its parent node.

1#include <cstdio>2#include <cstring>3#include <cstdlib>4#include <iostream>5#include <algorithm>6 7 #defineSiz 10248 9InlineintGet_c (void)Ten { One     Static CharBuf[siz]; A     Static Char*head = buf +siz; -     Static Char*tail = buf +siz; -  the     if(Head = =tail) -Fread (head = buf,1, Siz, stdin); -  -     return*head++; + } -  +InlineintGet_i (void) A { atRegisterintRET =0; -RegisterintNeg =false; -Registerintbit =Get_c (); -  -      for(; bit < -; bit =Get_c ()) -         if(bit = ='-') Neg ^=true; in  -      for(; bit > -; bit =Get_c ()) toRET = RET *Ten+ Bit- -; +  -     returnNeg? -Ret:ret; the } *  $ #defineMAXN 205Panax Notoginseng  - intN, ANS[MAXN]; the  + structnode A { theNode *Lson; +Node *Rson; -Node *father; $  $Nodevoid) -     { -Lson =NULL; theRson =NULL; -Father =NULL;Wuyi     } the  -InlinevoidSwapvoid) Wu     { -         StaticNode *temp; About  $temp =Lson; -Lson =Rson; -Rson =temp; -     } A}TREE[MAXN], *root =Tree; +  theInlineintLastvoid) - { $Node *t =Root; the  the      while(t->Rson) thet = t->Lson; the  -     if(T->lson &&!t->lson->Lson) int = t->Lson; the  the     if(T = =root) Aboutroot = t->Lson; the     Else theT->father->lson = t->Lson; the  +     if(t->Lson) -T->lson->father = t->father; the Bayi      for(Node *p = t->father; p; p = p->father) theP->swap (); the  -     return int(T-tree); - } the  theSigned Main (void) the { then =get_i (); -  the      for(inti =1; I <= N; ++i) the     { the         intFA =get_i ();94  the         if(FA < -) theTree[i].father = tree + fa, Tree[fa].lson = tree +i; the         ElseFA-= -,98Tree[i].father = tree + fa, Tree[fa].rson = tree +i; About     } - 101      for(inti = n; I >=0; -I.) Ans[i] =Last ();102 103      for(inti =0; I <= N; ++i) printf ("%d", Ans[i]);104  the     //System ("pause");106}

@Author: Yousiki

Bzoj 1078: [SCOI2008] Oblique heap

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.