Algorithm note _076: Blue Bridge Cup practice node selection (Java)

Source: Internet
Author: User

Directory

1 Problem Description

2 Solutions

  1 problem description Problem Description

There is a tree of n nodes, each node in the tree has a positive integer weight. If a point is selected, then the points adjacent to it in the tree cannot be selected. What is the weight and maximum of the selected points?

Input Format

The first line contains an integer N.

The next line contains n positive integers, and the second positive integer represents the weight of the point I.

The next line is n-1, each of which describes an edge on the Tree.

output Formatoutputs an integer representing the maximum value of the selected Point's weights and Values. Sample Input5
1 2 3) 4 5
1 2
1 3
2 4
2 5Sample Output aSample DescriptionSelect points 3, 4, 5th, weights and 3+4+5 = 12. data size and conventions

For 20% of data, N <= 20.

For 50% of data, N <= 1000.

For 100% of data, N <= 100000.

The weights are positive integers that do not exceed 1000.

2 Solutions

This paper mainly examines the dynamic programming method, the core of which is how to construct the state transfer Equation.

Cited in reference 2:

DP, using dp[i][0] to indicate that when I point is not selected, I point and its sub-tree can be selected maximum weight, dp[i][1] indicates the choice i point, I point and its subtree maximum weight.

State transition Equation:
For leaf node dp[k][0] = 0, dp[k][1] = k-point weights
For non-leaf node i,
dp[i][0] =∑max (dp[j][0], dp[j][1]) (j is the son of I)
dp[i][1] = I-point weight +∑dp[j][0] (j is son of I)
Maximum power value is Max (dp[0][0], dp[0][1])

The following code ultimately runs a score of $ , specific cause: run Timeout. however, in reference 1 , the C + + code runs a score of (PS: Specific understanding can refer to the end of the text Reference 1). Looking at the code in reference 1 , let me change the understanding of test instructions to: where the input n-1 bar represents the two number of edges, is the specific first i Vertex and the first J vertex, not the weight of the specific vertex; at the first time, I understood the weights for the specific vertices.

The specific code is as Follows:

Importjava.util.Scanner; public classMain { public int[] DP =New int[100002] [2];  public int[] tree =New int[100002] [300];//tree[i][3] = num indicates that the 3rd child node of the I node is the NUM node    /** Parameter point1: indicates the POINT1 node of the input, not the node weight * parameter point2: the node that represents the input point2, not the node weight * description: because the title only gives the side description, does not indicate two nodes who are parents node, so the following There are two situations*/     public voidCreattree (intpoint1,intPoint2) {        inti = 0; //when the first point1 node is a parent node         while(tree[point1][i]! = 0) i++;//if the first point1 node already has a child, add another childtree[point1][i] =point2; intj = 0; //when the first Point2 node is a parent node         while(tree[point2][j]! = 0) J + +; tree[point2][j]=point1; }    /** Parameter satrt: start node for DFS traversal of tree, for specific node location, not node weight * parameter root: for the direct parent node position of start node, root = 0 indicates parent node of root node*/     public voidDfsintStartintRoot) {        intChild = tree[start][0];//1th child node of first start node         for(inti = 0;child! = 0;i++) { child=tree[start][i]; if(child! = Root) {//prevent the child from start from becoming the father of startDFS (child, start); dp[start][1] + = dp[child][0];//start backtracking when the child node does not have a children nodedp[start][0] + = (dp[child][1] > dp[child][0]? dp[child][1]: dp[child][0]); }        }    }         public Static voidmain (string[] Args) {main test=NewMain (); Scanner in=NewScanner (system.in); intn =In.nextint ();  for(inti = 0;i < n;i++) Test.dp[i+ 1][1] =In.nextint ();  for(inti = 0;i < n-1;i++) {            intPoint1 =In.nextint (); intPoint2 =In.nextint ();        Test.creattree (point1, point2); } Test.dfs (1, 0);//DFS traversal starts from the root node of the number created (that is, the 1th vertex, 0, which represents the parent node of the root Node)        intmax = (test.dp[1][1] > test.dp[1][0]? test.dp[1][1]: test.dp[1][0]);    System.out.println (max); }}

Resources:

1. Blue Bridge Cup algorithm improves node selection (tree Dp)

2. Blue Bridge cup-algorithm training-ALGO-4 node selection (classic tree Dp)

Algorithm note _076: Blue Bridge Cup practice node selection (Java)

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.