HDU 1520 Anniversary Party (tree DP, Getting started)

Source: Internet
Author: User

Test instructions: To a tree, each node has the right value, requires the selection of some nodes out, so that the sum of the weight of the largest, but each pair (father, son) can only pick one.

Ideas:

Compare the Getting Started topic, each node can be selected or not selected. If the current node is selected, the child must not select all, and if the current node is not selected, then the child can choose or not to choose.

1#include <bits/stdc++.h>2 #definePII pair<int,int>3 #defineINF 0x3f3f3f3f4 #defineLL Long Long5 using namespacestd;6 Const intn=7010;7 intW[n], dp[n][2], pre[n];8vector<int>Tree[n];9 Ten voidDasointt) One { A      for(intI=0; I<tree[t].size (); i++) DFS (Tree[t][i]); -     intsum[2]={0,0}; -      for(intI=0; I<tree[t].size (); i++) the     { -sum[0]+=max (dp[tree[t][i]][0], dp[tree[t][i]][1]);//Point T try not to take, then the child can be taken can also not take, take large person can.  -sum[1]+=max (dp[tree[t][i]][0],0);//Point T try to fetch, then the child must not take all -     } +dp[t][0]=sum[0]; -dp[t][1]=sum[1]+W[t]; + } A  at  - intMain () - { -     //freopen ("Input.txt", "R", stdin); -     intN, a, b, root; -      while(cin>>N) in     { -memset (PRE,0,sizeof(pre)); toMemset (DP,0,sizeof(DP)); +          for(intI=0; i<=n; i++) tree[i].clear (); -  the          for(intI=1; i<=n; i++) scanf ("%d",&w[i]); *          while(SCANF ("%d%d", &a,&b), A +b) $         {Panax Notoginseng Tree[b].push_back (a); -pre[a]=b; the         } +          for(intI=1; i<=n; i++)if(pre[i]==0) root=i; A DFS (root); theprintf"%d\n", Max (dp[root][0],dp[root][1]) ); +     } -     return 0; $}
AC Code

HDU 1520 Anniversary Party (tree DP, Getting started)

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.