問題: BOJ 1949 — 優秀な村 · English · 한국어
各村には人口が与えられます。隣接する村を同時に選ばないという条件のもとで、選んだ村の人口合計を最大化します。入力は N、各村の人口 N 個、そして双方向の道路 N - 1 本です。
村1を根として、木を反復処理でたどります。訪問順を保存し、その逆順で処理すれば、再帰を使わずに子から親の順で計算できます。そのため、長い一本道の木でも呼び出しスタックがあふれません。各村 u について、次の2つの値を管理します。
take[u]:uを選んだ場合の人口合計の最大値です。子は選べないため、take[u] = population[u] + sum(skip[child])です。skip[u]:uを選ばない場合の人口合計の最大値です。各子は選んでも選ばなくてもよいため、skip[u] = sum(max(take[child], skip[child]))です。
答えは根の max(take[root], skip[root]) です。人口は正なので、根だけを選んでも有効な正の合計になります。したがって、何も選ばない場合が正の答えを上回ることはありません。村が1つだけなら、その村の人口を返します。一本道では隣接する選択が2つの状態間で競合し、星形では中心を選ぶ場合と葉を選ぶ場合を比較します。
隣接リスト、訪問順、DP配列はいずれも O(N) の領域を使います。各頂点と辺を定数回処理するため、時間計算量は O(N) です。人口の合計が int の範囲を超えないよう、DPの合計には long を使います。
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(input.readLine().trim());
long[] population = new long[n];
StringTokenizer values = new StringTokenizer(input.readLine());
for (int i = 0; i < n; i++) {
population[i] = Long.parseLong(values.nextToken());
}
ArrayList<Integer>[] graph = new ArrayList[n];
for (int i = 0; i < n; i++) {
graph[i] = new ArrayList<>();
}
for (int i = 0; i < n - 1; i++) {
StringTokenizer edge = new StringTokenizer(input.readLine());
int a = Integer.parseInt(edge.nextToken()) - 1;
int b = Integer.parseInt(edge.nextToken()) - 1;
graph[a].add(b);
graph[b].add(a);
}
int[] parent = new int[n];
int[] order = new int[n];
int size = 0;
order[size++] = 0;
parent[0] = -1;
for (int i = 0; i < size; i++) {
int node = order[i];
for (int neighbor : graph[node]) {
if (neighbor == parent[node]) {
continue;
}
parent[neighbor] = node;
order[size++] = neighbor;
}
}
long[] take = new long[n];
long[] skip = new long[n];
for (int i = n - 1; i >= 0; i--) {
int node = order[i];
take[node] = population[node];
for (int child : graph[node]) {
if (parent[child] == node) {
take[node] += skip[child];
skip[node] += Math.max(take[child], skip[child]);
}
}
}
System.out.println(Math.max(take[0], skip[0]));
}
}