[Daily Blue Bridge] 33. One-five-year provincial Java group real title "Tree of Life"

Hello, I am the little gray ape, a programmer who can write bugs!

Welcome everyone to pay attention to my column " Daily Blue Bridge ". The main function of this column is to share with you the real questions of the Blue Bridge Cup provincial competitions and finals in recent years, analyze the algorithm ideas, data structures and other content that exist in it, and help you learn To more knowledge and technology!

Title: Tree of Life

In the X forest, God created the tree of life.

He marked each node (the leaf is also called-a node) of each tree with an integer, which represents the harmony value of this point.

God wants to select a non-empty node set S in this tree, so that for any two points a and b in S, there is a point

The column {a,vl,v2.... vk, b} makes each point in this point column an ​​element in S, and there is an edge connected between two adjacent points in the sequence.

Under this premise, God wants to make the sum of the integers corresponding to the points in S as large as possible.

This largest sum is God's score for the tree of life.

After atm’s efforts, he already knew the integer that God gave to each node on each tree. But because atm is not good at

Calculating, he doesn’t know how to efficiently score. He needs you to write a program for him to calculate the score of a tree.

[Input format]

An integer n in the first line indicates that the tree has n nodes.

The n integers in the second row represent the score of each node in turn.

In the next n-1 lines, each line contains 2 integers u, v, indicating that there is an edge from u to v. Since this is a tree,

There is no ring.

[Output format]

Output a number on a line to represent the score given to this tree by God.

 

【Sample input】

5

1 -2 -3 4 5

4 2

3 1

1 2

2 5

[Sample output]

8

【data range】

For 30% of the data, n<=10

For 100% data, 0<n<=10^5 The absolute value of the score of each node does not exceed 10^6

【Resource Agreement】

Peak memory consumption (including virtual machines) <256M

CPU consumption <3000ms

Please output in strict accordance with the requirements, and do not print superfluous content like "please enter...".

All the codes are placed in the same source file. After the debugging is passed, copy and submit the source code.

Note: Do not use package statement, do not use jdk1.6 and above version features

Note: The name of the main class must be Main, otherwise it will be treated as an invalid code.

Problem-solving ideas:

In the process of solving this problem, you need to understand the depth traversal process of the tree. In fact, this problem is about the traversal of numbers. Taking the example in the problem as an example, the solution is as follows:

Answer source code:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Year2015_Bt10 {

	static int n;	//树的节点数
	static int MaxN = 100000;	//最大的权值
	static int [] w = new int[MaxN+1];	//存放每一个节点的权值
	static int [] ww = new int[MaxN+1];	//记录每个节点的总体权重
	static List<Integer> [] g;	//记录两节点之间的路径
	static int ans;
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		n = scanner.nextInt();
		for (int i = 1; i <= n; i++) {
			w[i] = scanner.nextInt();
		}
		g = new ArrayList[n+1];
		initG();//初始化记录路径的g列表
		//利用列表来记录每两个点之间的路径关系
		for (int i = 1; i <= n-1 ; i++) {
			int u = scanner.nextInt();
			int v = scanner.nextInt();
			g[u].add(v);
			g[v].add(u);
		}
		ans = 0;
		dfs(1,0);//令第一个节点是根节点,它的父亲节点是0
		System.out.println(ans);
	}
	
	//对树进行深度遍历
	private static void dfs(int root, int fg) {
		ww[root] = w[root];
		for (int i = 0; i < g[root].size(); i++) {
			int son = g[root].get(i);	//获取到root节点的儿子节点
			//如果获取到的节点不是root节点的父亲节点
			if (son!=fg) {
				dfs(son, root);//让儿子节点作为根节点,root节点作为它的父节点继续遍历
				if (ww[son]>0) {//如果该节点的权重大于0,则他的父节点的权重就加上它的权重
					ww[root]+=ww[son];
				}
			}
		}
		
		if (ww[root]>ans) {
			ans = ww[root];
		}
		
	}

	//初始化g列表
	private static void initG() {
		for (int i = 1; i < g.length; i++) {
			g[i] = new ArrayList<Integer>();
		}
	}

}

 

Sample output:

There are deficiencies or improvements, and I hope that my friends will leave a message and learn together!

Interested friends can follow the column!

Little Gray Ape will accompany you to make progress together!

Guess you like

Origin blog.csdn.net/weixin_44985880/article/details/114984649