Skip to content

Commit f95305c

Browse files
authored
[20260109] BOJ / G3 / 사회망 서비스(SNS) / 이강현
1 parent 526f1c6 commit f95305c

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 StringTokenizer st;
9+
static int[][] dp;
10+
static boolean[] visited;
11+
public static void main(String[] args) throws Exception {
12+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13+
N = Integer.parseInt(br.readLine());
14+
adjList = new ArrayList[N+1];
15+
for (int i = 0; i <= N; i++) {
16+
adjList[i] = new ArrayList<>();
17+
}
18+
dp = new int[N+1][2];
19+
visited = new boolean[N+1];
20+
21+
for (int i = 1; i < N; i++) {
22+
st = new StringTokenizer(br.readLine());
23+
int u = Integer.parseInt(st.nextToken());
24+
int v = Integer.parseInt(st.nextToken());
25+
adjList[u].add(v);
26+
adjList[v].add(u);
27+
}
28+
dfs(1);
29+
System.out.println(Math.min(dp[1][0],dp[1][1]));
30+
}
31+
public static void dfs(int cur){
32+
visited[cur] = true;
33+
dp[cur][1] = 1;
34+
35+
for (int next : adjList[cur]) {
36+
if(visited[next]) continue;
37+
dfs(next);
38+
dp[cur][0] += dp[next][1];
39+
dp[cur][1] += Math.min(dp[next][0],dp[next][1]);
40+
}
41+
}
42+
}
43+
```

0 commit comments

Comments
 (0)