Rokua P1040 plus two forks of trees

Source: Internet
Author: User

Title Description

The middle order traversal of a two-ary tree with an n node is (,..., N), where the number,..., n is a node number. Each node has a fraction (all positive integers), the score for the I node is di,tree and each of its subtrees has an addition, and any subtrees tree subtree (which also contains the tree itself) is calculated as follows:

The subtree of the left subtree of the Xsubtree is added to the right sub-tree to add the score of the +subtree root.

If a subtree is empty, it is specified to be divided into 1, and the addition of the leaves is the fraction of the leaf node itself. Without regard to its empty trees.

Try to find a tree of two trees that matches the middle order traversal (,..., N) and has the highest added score. Required output;

(1) Maximum bonus points for tree

(2) The tree's pre-sequence traversal

Input/output format

Input format:

Line 1th: An integer n (n<30), which is the number of nodes.

Line 2nd: n spaces separated by integers, for each node's score (fractional <100).

Output format:

Line 1th: An integer that is the highest plus score (the result will not exceed 4,000,000,000).

Line 2nd: N A space-separated integer, for the tree's pre-order traversal.

Input and Output Sample input example # #:
55 7 1) 2 10
Sample # # of output:
1453 1 2) 4 5


In fact, it is the problem of water, because it is the middle sequence traversal, so, the root node in the middle, then is an enumeration root node partition, a bit like a stone merge.
As for how to get the pre-order traversal, it can be stored again at the time of recursion, once the value is updated, the ancestors also updated processing, not difficult.
1#include <iostream>2#include <fstream>3#include <cstdlib>4#include <cstring>5 /*Run this program using the console Pauser or add your own getch, System ("pause") or input loop*/6 using namespacestd;7 intCnt_node=0;8 intgra_node[ -]={0};9 intjiyi[ -][ -]={0};Ten introot[ -][ -]={0}; One intdgintKsintJS); A voidOUT_DLR (intKsintJS); -  -  the intdgintKsintJS) { - if(KS==JS)returnGra_node[ks];  - if(ks+1==JS)returngra_node[ks]+Gra_node[js]; - if(jiyi[ks][js]!=-1)returnJiyi[ks][js]; + inttem=0; -TEM=DG (ks+1, JS) +Gra_node[ks]; +root[ks][js]=KS;  A  for(intx=ks+1; x<js;x++){ at  intB=DG (ks,x-1) *DG (x+1, JS) +Gra_node[x];  -  if(B>tem) {tem=b;root[ks][js]=x;}  - }     - intB=DG (ks,js-1); - if(B>tem) {tem=b;root[ks][js]=JS;} -jiyi[ks][js]=tem; in returntem; - } to  +  - voidOUT_DLR (intKsintJS) { the if(KS==JS) {cout<<ks<<" ";return;}  * if(ks+1==JS) {cout<<ks<<" "<<js<<" ";return;}  $cout<<root[ks][js]<<" ";Panax NotoginsengOUT_DLR (ks,root[ks][js]-1); -OUT_DLR (root[ks][js]+1, JS);  the return;  + } A  the  + intMainintargcChar**argv) { -Cin>>Cnt_node; $  for(intx=1; x<=cnt_node;x++) cin>>Gra_node[x]; $  -memset (jiyi,-1,sizeof(Jiyi)); - intANS=DG (1, Cnt_node); thecout<<ans<<Endl; -OUT_DLR (1, Cnt_node);Wuyi return 0; the}

Rokua P1040 plus two forks of trees

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.