Skip to content

Commit 9020e28

Browse files
committed
BOJ_2533 : 사회망 서비스
1 parent 4e83759 commit 9020e28

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
6+
private static boolean[] visited;
7+
private static int N;
8+
private static int[][] d;
9+
private static List<List<Integer>> tree;
10+
11+
public static void main(String[] args) throws IOException {
12+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13+
N = Integer.parseInt(br.readLine());
14+
d = new int[N+1][2];
15+
tree = new ArrayList<>();
16+
visited = new boolean[N+1];
17+
for(int i=0; i<=N; i++) {
18+
tree.add(new ArrayList<>());
19+
d[i][1] = 1;
20+
}
21+
for(int i=0; i<N-1; i++) {
22+
StringTokenizer st = new StringTokenizer(br.readLine());
23+
int v1 = Integer.parseInt(st.nextToken());
24+
int v2 = Integer.parseInt(st.nextToken());
25+
tree.get(v1).add(v2);
26+
tree.get(v2).add(v1);
27+
}
28+
// d[i][0] : i 노드가 얼리어답터가 아닌 경우의 최소 얼리어답터 수
29+
// d[i][1] : i 노드가 얼리어답터인 경우의 최소 얼리어답터 수
30+
visited[1] = true;
31+
dfs(1);
32+
System.out.println(Math.min(d[1][0], d[1][1]));
33+
}
34+
35+
public static void dfs(int n) {
36+
List<Integer> child = tree.get(n);
37+
for(int i=0; i<child.size(); i++) {
38+
int next = child.get(i);
39+
if(!visited[next]) {
40+
visited[next] = true;
41+
dfs(next);
42+
d[n][0] += d[next][1];
43+
d[n][1] += Math.min(d[next][0], d[next][1]);
44+
}
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)