bzoj1078 "SCOI2008" oblique heap

Source: Internet
Author: User

1078: [SCOI2008] Skew heap time limit: ten Sec Memory Limit: 162 MB
Submit: 662 Solved: 380
[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 6



can and Heap , thinking good question

Share the puzzle:http://www.cppblog.com/MatoNo1/archive/2013/03/03/192131.html

I wish you Children's Day happy!




#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath > #include <algorithm> #define F (I,j,n) for (int. i=j;i<=n;i++) #define D (i,j,n) for (int i=j;i>=n;i--) # Define ll long Long#define N 1005using namespace std;int n,root,cnt,fa[n],ls[n],rs[n],ans[n];inline int read () {int x=0,f= 1;char Ch=getchar (); while (ch< ' 0 ' | | Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();} while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} inline void Solve () {int x=root;while (rs[x]!=-1) x=ls[x];int t=ls[x];if (t!=-1&&ls[t]==-1&&rs[t]==-1) X=t;ans[++cnt]=x;if (x==root) root=ls[x];int f=fa[x];if (f!=-1) ls[f]=ls[x],fa[ls[f]]=f;while (f!=-1) Swap (ls[f],rs[f ]), f=fa[f];} int main () {memset (fa,-1,sizeof (FA)); memset (ls,-1,sizeof (LS)); Memset (Rs,-1,sizeof (RS)); N=read (); F (i,1,n) {int x=read (); if (x<100) Ls[x]=i,fa[i]=x;else rs[x-100]=i,fa[i]=x-100;} F (I,0,n) solve ();D(i,cnt,1) printf ("%d", ans[i]); return 0;}


bzoj1078 "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.