Skip to content

Commit 9ef6629

Browse files
authored
[20260112] BOJ / G3 / 트리를 복잡하게 색칠하는 최소 비용 / 이강현
1 parent f95305c commit 9ef6629

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
public class Main {
6+
static int N;
7+
static List<Integer>[] adjList;
8+
static long[][] dp;
9+
static int[] whiteCost;
10+
static int[] blackCost;
11+
static StringTokenizer st;
12+
13+
public static void main(String[] args) throws Exception {
14+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
15+
N = Integer.parseInt(br.readLine());
16+
adjList = new List[N];
17+
for (int i = 0; i < N; i++) {
18+
adjList[i] = new ArrayList<>();
19+
}
20+
dp = new long[N][2];
21+
whiteCost = new int[N];
22+
blackCost = new int[N];
23+
24+
for (int i = 1; i < N; i++) {
25+
st = new StringTokenizer(br.readLine());
26+
int from = Integer.parseInt(st.nextToken());
27+
int to = Integer.parseInt(st.nextToken());
28+
adjList[from].add(to);
29+
}
30+
31+
for (int i = 0; i < N; i++) {
32+
st = new StringTokenizer(br.readLine());
33+
whiteCost[i] = Integer.parseInt(st.nextToken());
34+
blackCost[i] = Integer.parseInt(st.nextToken());
35+
}
36+
37+
dfs(0);
38+
System.out.println(Math.min(dp[0][0],dp[0][1]));
39+
}
40+
public static void dfs(int cur){
41+
dp[cur][0] = whiteCost[cur];
42+
dp[cur][1] = blackCost[cur];
43+
for (Integer next : adjList[cur]) {
44+
dfs(next);
45+
dp[cur][0] += Math.min(dp[next][0],dp[next][1]);
46+
dp[cur][1] += dp[next][0];
47+
}
48+
}
49+
}
50+
```

0 commit comments

Comments
 (0)