"poj2486" "Apple tree" "tree dp"

Source: Internet
Author: User
<span id="Label3"></p>Apple Tree<p><p>Time limit:1000ms Memory limit:65536k<br>Total submissions:7893 accepted:2642<br>Description</p></p><p><p>WSHXZT is a lovely girl. She likes Apple very Much. One day HX takes she to an Apple Tree. There is N nodes in the Tree. Each node has a amount of apples. Wshxzt starts her happy trips at one Node. She can eat up all the apples in the nodes she Reaches. HX is a kind guy. He knows that eating too many can make the lovely girl become fat. So he doesn ' t allow wshxzt to go more than K steps in the Tree. It costs one step when she goes from one node to another adjacent Node. Wshxzt likes Apple very Much. So she wants to eat as many as she Can. Can many apples she can eat in to the most K steps.<br>Input</p></p><p><p>There is several test cases in the input<br>Each test case contains three Parts.<br>The first part is a numbers N K, whose meanings we have a talked about just NOW. We denote the nodes by 1 2 ... N. Since It is a tree with each node can reach any and other on only one Route. (1<=n<=100, 0<=k<=200)<br>The second part contains N integers (all integers is nonnegative and not bigger than 1000). The ith number is the amount of apples in Node I.<br>The third part contains N-1 LINE. There is numbers a, A, a, and node B are adjacent, meaning.<br>Input would be ended by the end of File.</p></p><p><p>Note:wshxzt starts at Node 1.<br>Output</p></p><p><p>For each test case, output the maximal numbers of apples wshxzt can eat at a line.<br>Sample Input</p></p><p><p>2 1<br>0 11<br>1 2<br>3 2<br>0 1 2<br>1 2<br>1 3<br>Sample Output</p></p><p><p>11<br>2</p></p><p><p>The problem I started thinking about was using f[i][j] to indicate the maximum number of apples that were taken when J was taken at the I Node. But I found that doing this will repeat the calculation for two of the distance from the LCA of the nodes in different subtrees to the root Node.<br>So let's change the state that we need to say for the equation of motion: f[i][j] indicates that when I was in the subtree of i, I had taken the most apples when I had walked J-step.<br>We will consider how to use the subtree to update: because this situation is more, so we can consider using a backpack to solve the Problem.<br>But we need to pay attention to a problem: it is said that the topic may go to a node after the no longer go back, for this problem, we also need to improve the motion regulation equation: f[i][j][0] Indicates to the node I in the subtree of I have walked J step and can not go back to this node to fetch the most apples; f[i][j][1] it means you can come back.<br>Then the Motion-gage equation is:<br>F[i][j][1]=max (f[i][j][1],f[i][j-t][1]+f[son][t-2][1]) (means go to the child node and then walk back, in the process of going to the sub-node to drink back, need to walk two sides, so Minus)<br>F[i][j][0]=max (f[i][j][0],f[i][j-t][1]+f[son][t-1][1]) (indicates that from another byte point to the root node, go to son node, not back, because just go down no more back, so Just-1)<br>F[i][j][0]=max (f[i][j][0],f[i][j-t][1]+f[son][t-2][1]) (indicates The maximum value of the time from the sending node back to the root node, and then down from the root node, because this time, as in the first way, it went back and forth a total of two times, So-2)<br>So we can solve this problem later.</p></p><pre class="prettyprint"><code class=" hljs cpp"><span class="hljs-preprocessor"><span class="hljs-preprocessor">#include <iostream></span></span><span class="hljs-preprocessor"><span class="hljs-preprocessor">#include <cstdio></span></span><span class="hljs-preprocessor"><span class="hljs-preprocessor">#include <cstring></span></span><span class="hljs-preprocessor"><span class="hljs-preprocessor">#include <vector></span></span><span class="hljs-keyword"><span class="hljs-keyword">using</span></span> <span class="hljs-keyword"><span class="hljs-keyword">namespace</span></span> <span class="hljs-built_in"><span class="hljs-built_in">STD</span></span>;<span class="hljs-keyword"><span class="hljs-keyword">Const</span></span> <span class="hljs-keyword"><span class="hljs-keyword">int</span></span>n=<span class="hljs-number"><span class="hljs-number"></span> the</span>;<span class="hljs-keyword"><span class="hljs-keyword">Const</span></span> <span class="hljs-keyword"><span class="hljs-keyword">int</span></span>k=<span class="hljs-number"><span class="hljs-number"></span> about</span>;<span class="hljs-stl_container"><span class="hljs-stl_container"> <span class="hljs-built_in">vector</span> <<span class="hljs-keyword">int</span>></span></span>tr[n];<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>t,n,k,a[n],f[n][k][<span class="hljs-number"><span class="hljs-number">2</span></span>];<span class="hljs-keyword"><span class="hljs-keyword">void</span></span>dp<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>X<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>Last) {<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>i,j,u,l;<span class="hljs-keyword"><span class="hljs-keyword"></span> for</span>(i=<span class="hljs-number"><span class="hljs-number">0</span></span>; i<tr[x].size (); ++i) {u=tr[x][i];<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(u==last)<span class="hljs-keyword"><span class="hljs-keyword">Continue</span></span>; DP (tr[x][i],x);<span class="hljs-keyword"><span class="hljs-keyword"></span> for</span>(j=k;j>=<span class="hljs-number"><span class="hljs-number">1</span></span>;--j) {<span class="hljs-keyword"><span class="hljs-keyword"></span> for</span>(l=<span class="hljs-number"><span class="hljs-number">1</span></span>; L<=j;++l) {f[x][j][<span class="hljs-number"><span class="hljs-number">1</span></span>]=max (f[x][j][<span class="hljs-number"><span class="hljs-number">1</span></span>],f[x][j-l][<span class="hljs-number"><span class="hljs-number">1</span></span>]+f[u][l-<span class="hljs-number"><span class="hljs-number">2</span></span>][<span class="hljs-number"><span class="hljs-number">1</span></span>]); f[x][j][<span class="hljs-number"><span class="hljs-number">0</span></span>]=max (f[x][j][<span class="hljs-number"><span class="hljs-number">0</span></span>],f[x][j-l][<span class="hljs-number"><span class="hljs-number">1</span></span>]+f[u][l-<span class="hljs-number"><span class="hljs-number">1</span></span>][<span class="hljs-number"><span class="hljs-number">0</span></span>]); f[x][j][<span class="hljs-number"><span class="hljs-number">0</span></span>]=max (f[x][j][<span class="hljs-number"><span class="hljs-number">0</span></span>],f[x][j-l][<span class="hljs-number"><span class="hljs-number">0</span></span>]+f[u][l-<span class="hljs-number"><span class="hljs-number">2</span></span>][<span class="hljs-number"><span class="hljs-number">1</span></span>]); } } }}<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>Main () {<span class="hljs-keyword"><span class="hljs-keyword"></span> while</span>(<span class="hljs-built_in"><span class="hljs-built_in">scanf</span></span>(<span class="hljs-string"><span class="hljs-string">"%d%d"</span></span>, &n,&k) = =<span class="hljs-number"><span class="hljs-number">2</span></span>){<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>i,j,x,y;<span class="hljs-keyword"><span class="hljs-keyword"></span> for</span>(i=<span class="hljs-number"><span class="hljs-number">1</span></span>; I<n;++i) Tr[i].clear ();<span class="hljs-built_in"><span class="hljs-built_in">memset</span></span>(f,<span class="hljs-number"><span class="hljs-number">0</span></span>,<span class="hljs-keyword"><span class="hljs-keyword">sizeof</span></span>(f));<span class="hljs-keyword"><span class="hljs-keyword"></span> for</span>(i=<span class="hljs-number"><span class="hljs-number">1</span></span>; I<=n;++i) {<span class="hljs-built_in"><span class="hljs-built_in">scanf</span></span>(<span class="hljs-string"><span class="hljs-string">"%d"</span></span>, &a[i]);<span class="hljs-keyword"><span class="hljs-keyword"></span> for</span>(j=<span class="hljs-number"><span class="hljs-number">0</span></span>; J<=k;++j) {f[i][j][<span class="hljs-number"><span class="hljs-number">0</span></span>]=f[i][j][<span class="hljs-number"><span class="hljs-number">1</span></span>]=a[i]; } }<span class="hljs-keyword"><span class="hljs-keyword"></span> for</span>(i=<span class="hljs-number"><span class="hljs-number">1</span></span>; I<n;++i) {<span class="hljs-built_in"><span class="hljs-built_in">scanf</span></span>(<span class="hljs-string"><span class="hljs-string">"%d%d"</span></span>, &x,&y); Tr[x].push_back (y); Tr[y].push_back (x); } DP (<span class="hljs-number"><span class="hljs-number">1</span></span>,<span class="hljs-number"><span class="hljs-number">0</span></span>);<span class="hljs-built_in"><span class="hljs-built_in">printf</span></span>(<span class="hljs-string"><span class="hljs-string">"%d\n"</span></span>, Max (f[<span class="hljs-number"><span class="hljs-number">1</span></span>][k][<span class="hljs-number"><span class="hljs-number">0</span></span>],f[<span class="hljs-number"><span class="hljs-number">1</span></span>][k][<span class="hljs-number"><span class="hljs-number">1</span></span>])); }}</code></pre> <p><p>"poj2486" "Apple tree" "tree dp"</p></p></span>

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.