ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1018. Binary Apple Tree

Recursion
Posted by marat 19 Apr 2011 16:58
Let C[i][j] be weight of the edge( i, j). A( n, k ) - maximal amount of apples under k-th node after n cuts. B(k) - amount of edges under k-th node. Then ( k+ - right child and k-  - left child of the k-th nod )

A(n,k) = max{ a(n - B(k-), k+ ), if ( n > B(k-))                              }
            { a(n - B(k+), k- ), if ( n > B(k+))                              }                                                                           .           { MAX i FROM( 0) TO (n) OF a(n-i,k+)+a(i,k-) + C[k][k-]+C[k][k+]  }
            { A(0,k+) + A(0, k- ) + C[k][k-]+C[k][k+], if n = 0               }
            { - MAXAMOUNTOFAPPLES, if (k+ or k- doesn't exist AND n>0)        }

1-th and 2-th lines: one can cut left or rigth edge, if so we have to cut n-B(k+-) edges in the other subtree.

3-th line:           one can save both left and right edges, if so we have to cut i edges in left and n-i edges in the rigth subtrees. Choose maximal value changing i variable.

4-th line:           in this case we should count amout of apples under k-th node. It's equal to amount of apples in subtrees plus edge weights.

5-th line:           k is a leaf. if n>0 there is nothing to cut => deny this situation by MAXAMOUNTOFAPPLES with negative sign. I used MAXAMOUNTOFAPPLES  = 3000000

After that -  DP.
Re: Recursion
Posted by Denis Astanin (KPI) 3 May 2011 22:45
Can you write it on russian?