Skip to content

Commit a8b9317

Browse files
authored
Merge pull request #823 from AlgorithmWithGod/zinnnn37
[20250905] BOJ / G3 / 사회망 서비스(SNS) / 김민진
2 parents 6631b53 + c970a49 commit a8b9317

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
```java
2+
import java.io.BufferedReader;
3+
import java.io.BufferedWriter;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.io.OutputStreamWriter;
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import java.util.StringTokenizer;
10+
11+
public class BJ_2533_사회망_서비스_SNS {
12+
13+
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
14+
private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
15+
private static StringTokenizer st;
16+
17+
private static int N;
18+
private static int[][] dp; // [노드][0(일반인)/1(얼리어답터)]
19+
20+
private static boolean[] visited;
21+
private static List<Integer>[] tree;
22+
23+
public static void main(String[] args) throws IOException {
24+
init();
25+
sol();
26+
}
27+
28+
private static void init() throws IOException {
29+
N = Integer.parseInt(br.readLine());
30+
31+
tree = new List[N + 1];
32+
for (int i = 1; i <= N; i++) {
33+
tree[i] = new ArrayList<>();
34+
}
35+
36+
for (int i = 0; i < N - 1; i++) {
37+
st = new StringTokenizer(br.readLine());
38+
39+
int u = Integer.parseInt(st.nextToken());
40+
int v = Integer.parseInt(st.nextToken());
41+
42+
tree[u].add(v);
43+
tree[v].add(u);
44+
}
45+
visited = new boolean[N + 1];
46+
dp = new int[N + 1][2];
47+
}
48+
49+
private static void sol() throws IOException {
50+
find(1);
51+
52+
bw.write(Math.min(dp[1][0], dp[1][1]) + "");
53+
bw.flush();
54+
bw.close();
55+
br.close();
56+
}
57+
58+
private static void find(int u) {
59+
visited[u] = true;
60+
dp[u][0] = 1; // 초기화: 필요한 얼리어답터 수 = 1
61+
62+
for (int v : tree[u]) {
63+
if (visited[v]) {
64+
continue;
65+
}
66+
find(v);
67+
dp[u][1] += dp[v][0]; // 내가 얼리어답터면 친구가 얼리어답터일 필요 없음
68+
dp[u][0] += Math.min(dp[v][0], dp[v][1]);
69+
}
70+
}
71+
72+
}
73+
```

0 commit comments

Comments
 (0)