题目
分析
输入:
5
1 2 3 4 5
1 2
1 3
2 4
2 5
输出:
12
首先建立对应的树
依题意,不能同时选择孩子与双亲
链式向前星
struct Edge{
int next;
int to;
int w;
};
- edge[i].to:表示第i条边的终点
- edge[i].next:表示与第i条边同起点的下一条边的存储位置
- edge[i].w:表示边的权值
head[]:
加边函数:
void add(int from, int to, int w){
edge[cnt].w = w;
edge[cnt].to = to;
edge[cnt].next = head[from];
head[from] = cnt++;
}
代码
import java.util.Arrays;
import java.util.Scanner;
public class Main {
final static int max = 100010;
static class edge{
int to, next;
public edge(int to, int next) {
// TODO Auto-generated constructor stub
this.to = to;
this.next = next;
}
}
static int head[] = new int [max];
static edge e[] = new edge[2*max];
static int dp[][] = new int [max][2];
static int cnt = 0;
static void add(int from, int to) {
e[cnt] = new edge(to, head[from]);
head[from] = cnt++;
}
static void dfs(int root, int pre) {
for(int i = head[root]; i != -1; i = e[i].next) {
if(e[i].to != pre) {
dfs(e[i].to, root);
dp[root][0] += Math.max(dp[e[i].to][0], dp[e[i].to][1]);
dp[root][1] += dp[e[i].to][0];
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int n;
n = sc.nextInt();
Arrays.fill(head, -1);
for(int i=0; i<n; i++) {
dp[i][1] = sc.nextInt();
}
for(int i=0; i<n-1; i++) {
int a,b;
a = sc.nextInt() - 1;
b = sc.nextInt() - 1;
add(a,b);
add(b,a);
}
dfs(0, -1);
System.out.println(Math.max(dp[0][0], dp[0][1]));
}
}