-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1039. Anniversary Party.java
68 lines (54 loc) · 1.79 KB
/
1039. Anniversary Party.java
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
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Scanner;
/**
* Created by CEB842 on 6/22/2015.
*/
public class AniversaryParty {
static int[][] Q;
static byte[] ratings;
static ArrayList<LinkedList<Integer>> tree;
public static void DFS(int root) {
Q[root][0] = 0;
Q[root][1] = ratings[root];
for (Integer node : tree.get(root)) {
DFS(node);
Q[root][1] += Q[node][0];
Q[root][0] += Math.max(Q[node][0], Q[node][1]);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
ratings = new byte[N + 1];
int[] parents = new int[N + 1]; /* Just use it to find the root */
tree = new ArrayList<>();
/* Read ratings */
tree.add(0, new LinkedList<>());
for (int i = 1; i <= N; i++) {
ratings[i] = sc.nextByte();
tree.add(i, new LinkedList<>());
}
/* Construct the Tree */
int L = sc.nextInt();
int K = sc.nextInt();
while (L != 0 && K != 0) {
tree.get(K).add(L);
parents[L] = parents[L] + 1;
L = sc.nextInt();
K = sc.nextInt();
}
/* Now the tree would be rooted at 0 */
for (int i = 1; i <= N; i++) {
if (parents[i] == 0) {
tree.get(0).add(i);
}
}
Q = new int[N + 1][2];
/* Where Q[i][0] -> maximum rating for tree at root i, root is not included in the party
Q[i][1] -> maximum rating for tree at root i, root is included in the party
*/
DFS(0); /* start from 0, root is not included */
System.out.println(Q[0][0]);
}
}