[Link] https://www.acmicpc.net/problem/2213
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import java.util.*;
import java.io.*;
public class Main {
static BufferedReader br;
static StringBuilder sb = new StringBuilder();
static int[] fdp;
static int[] edp;
static int[] w;
static boolean[] visit;
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
int n = toi(br.readLine());
int[] arr;
Node[] nodes = new Node[n];
w = getArr();
edp = new int[n];
fdp = new int[n];
for(int i = 0; i < n; i++) fdp[i] = w[i];
visit = new boolean[n];
for(int i = 0; i < n; i++) nodes[i] = new Node(i);
for(int i = 0; i < n - 1; i++) {
arr = getArr();
int a = arr[0] - 1, b = arr[1] - 1;
nodes[a].list.add(nodes[b]);
nodes[b].list.add(nodes[a]);
}
nodes[0].parent = -1;
bfs(nodes[0]);
int ans = Math.max(edp[0], fdp[0]);
Node node = nodes[0];
ArrayList<Integer> al = new ArrayList<>();
println(ans);
getVertext(ans, node, al);
Collections.sort(al);
for(int e: al) sb.append(e).append(" ");
print(sb);
}
static void getVertext(int val, Node node, ArrayList<Integer> al) {
int sum = w[node.idx];
if(val == sum) {
al.add(node.idx + 1);
return;
}
if(val == 0) return;
for(Node nn: node.list) {
if(nn.idx == node.parent) continue;
sum += edp[nn.idx];
}
if(sum == val) {
al.add(node.idx + 1);
for(Node nn: node.list) {
if(nn.idx == node.parent) continue;
getVertext(edp[nn.idx], nn, al);
}
} else {
for(Node nn: node.list) {
if(nn.idx == node.parent) continue;
getVertext(Math.max(edp[nn.idx], fdp[nn.idx]), nn, al);
}
}
}
static void bfs(Node node) {
if(node.list.size() == 1 && node.parent != -1) {
return;
}
for(Node child: node.list) {
if(child.idx == node.parent) continue;
child.parent = node.idx;
if(!visit[child.idx]) bfs(child);
fdp[node.idx] += edp[child.idx];
edp[node.idx] += Math.max(edp[child.idx], fdp[child.idx]);
}
visit[node.idx] = true;
}
static class Node {
int idx;
int parent;
ArrayList<Node> list = new ArrayList<>();
Node(int idx) { this.idx = idx; }
}
static int toi(String s) { return Integer.parseInt(s); }
static String[] getLine() throws IOException { return br.readLine().split(" "); }
static int[] getArr() throws IOException { return Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); }
static <T> void print(T s) { System.out.print(s); }
static <T> void println(T s) { System.out.println(s); }
}