Wust-1299-node selection (tree DP)

Source: Internet
Author: User

Problem Description has a tree with n nodes. each node in the tree has a positive integer weight. If a vertex is selected, the vertex adjacent to the tree cannot be selected. What is the weight and maximum value of the selected vertex?

The first line of Input contains an integer n. The next row contains n positive integers. The I positive integers represent the weights of vertex I. Next there are a total of N-1 rows, each line describes an edge on the tree.

Output outputs an integer representing the weights and maximum values of the selected vertex.

Sample Input
51 2 3 4 51 21 32 42 5
Sample Output
12
HINT

Example
Select 3, 4, and 5. The weight is 3 + 4 + 5 = 12.

Data scale and conventions
For 20% of data, n <= 20.
For 50% of data, n <= 1000.
For 100% of data, n <= 100000.
The weights are all positive integers not greater than 1000.
Train of Thought: For each vertex I, either select val [I] + gson [I], where gson [I] indicates the sum of the dp values of all grandson nodes; or do not select: son [I], where son [I] indicates the sum of the dp values of all son nodes. There is no root tree in the question. You only need to select a node as the root node (select 1 here) to determine the parent-child relationship. Then we use an array to simulate the stack, and finally start pushing forward from the last element of the array (this is equivalent to pushing up from the bottom layer of the tree ), and the son value of the new parent node and the gson value of the grandfather node.
# Include
 
  
Using namespace std; int val [100001], dp [100001], son [100001], gson [100001], first [100001], next [200002], to [200002], que [100001], far [100001]; bool vis [100001]; int main () {int n, I, u, v, ans; while (~ Scanf ("% d", & n) {for (I = 1; I <= n; I ++) first [I] =-1, vis [I] = son [I] = gson [I] = dp [I] = 0; for (I = 1; I <= n; I ++) scanf ("% d", & val [I]); for (I = 0; I
  
   
= 0; I --) // start from the bottom layer of the tree and push up {dp [que [I] = val [que [I] + gson [que [I]> son [que [i]? Val [que [I] + gson [que [I]: son [que [I]; ans = ans> dp [que [I]? Ans: dp [que [I]; if (far [que [I]! =-1) {son [far [que [I] + = dp [que [I]; // update the son value of the parent node if (far [far [que [I]! =-1) gson [far [far [que [I] + = dp [que [I]; // update the gson value of the grandfather node} printf ("% d \ n", ans );}}
  
 


Related Article

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.