java implementation of Blue Bridge Cup algorithm training node selection

Keywords: Programming Java

Problem description
There is a tree with n nodes. Each node in the tree has a positive integer weight. If a point is selected, 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 i-th positive integer represents the weight of point I.

Next there are n-1 lines, each describing an edge on the tree.

Output format
Output an integer representing the maximum value of the weight sum of the selected points.
sample input
5
1 2 3 4 5
1 2
1 3
2 4
2 5
sample output
12
Sample explanation
Select points 3, 4 and 5, and the sum of weights is 3 + 4 + 5 = 12.
Data scale and agreement
For 20% of the data, n < = 20.

For 50% of the data, n < = 1000.

For 100% of the data, n < = 100000.

All weights are positive integers of no more than 1000.

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

Reference 2 explains:

DP: dp[i][0] represents the maximum weight that I point and its subtree can select when I point is not selected, and dp[i][1] represents the maximum weight of I point and its subtree when I point is selected.

State transfer equation:
For leaf node dp[k][0] = 0, dp[k][1] = k-point weight
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 the son of I)
The maximum weight is max(dp[0][0], dp[0][1])

The final running score of the following code is 50, for the specific reason: running timeout.

import java.util.Scanner;

public class Main {
    public int[][] dp = new int[100002][2];   
    public int[][] tree = new int[100002][300];  //tree[i][3] = num indicates that the third child node of the ith node is the num node
    /*
     * Parameter point1: indicates the point1 node of the input, not the node weight
     * Parameter point2: represents the node of the input point2, not the node weight
     * Note: since the topic only gives an edge description, it does not indicate who is the parent node of the two nodes, so there are two situations as follows
     */
    public void creatTree(int point1, int point2) {
        int i = 0;
        //When the point1 node is a parent node
        while(tree[point1][i] != 0) i++;  //If the point1 node already has children, add another child  
        tree[point1][i] = point2;
        int j = 0;
        //When the point2 node is the parent node
        while(tree[point2][j] != 0) j++;
        tree[point2][j] = point1;
    }
    /*
     * Parameter satrt: the starting node for DFS traversal of the tree. It is the specific node location, not the node weight.
     * Parameter root: the direct parent node location of the start node. root = 0 indicates the parent node of the root node.
     */
    public void dfs(int start, int root) {
        int child = tree[start][0];  //The first child of the start node
        for(int i = 0;child != 0;i++) {
            child = tree[start][i];
            if(child != root) {  //Prevent a child from becoming a father of start
                dfs(child, start);
                dp[start][1] += dp[child][0];  //When the child node has no child node, start backtracking
                dp[start][0] += (dp[child][1] > dp[child][0] ? dp[child][1] : dp[child][0]);
            }
        }
    }
    
    public static void main(String[] args) {
        Main test = new Main();
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        for(int i = 0;i < n;i++)
            test.dp[i + 1][1] = in.nextInt();
        for(int i = 0;i < n - 1;i++) {
            int point1 = in.nextInt();
            int point2 = in.nextInt();
            test.creatTree(point1, point2);
        }
        test.dfs(1, 0);   //DFS traversal starts from the number of root nodes created (i.e. the first vertex, 0 represents the parent node of the root node)
        int max = (test.dp[1][1] > test.dp[1][0] ? test.dp[1][1] : test.dp[1][0]);
        System.out.println(max);
    }
}

Posted by god_zun on Fri, 18 Oct 2019 08:43:55 -0700