Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f0d09a3

Browse files
committed
이예진: [BOJ] 2533 사회망 서비스(SNS)_241129
1 parent cea060c commit f0d09a3

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

‎BOJ/1000-5000번/YJ_2533.java‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class YJ_2533 {
5+
static List<Integer>[] graph;
6+
static int[][] dp; //dp[index][0] : 얼리 아답터 X , dp[index][1] : 얼리 아답터 O
7+
static boolean[] visited;
8+
public static void main(String[] args) throws IOException {
9+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
int n = Integer.parseInt(br.readLine());
11+
12+
dp = new int[n+1][2];
13+
visited = new boolean[n+1];
14+
graph = new ArrayList[n+1];
15+
for(int i = 1; i < n+1; i++) {
16+
graph[i] = new ArrayList<>();
17+
}
18+
for(int i = 0; i < n-1; i++) {
19+
StringTokenizer st = new StringTokenizer(br.readLine());
20+
int u = Integer.parseInt(st.nextToken());
21+
int v = Integer.parseInt(st.nextToken());
22+
graph[u].add(v);
23+
graph[v].add(u);
24+
}
25+
26+
dfs(1);
27+
System.out.println(Math.min(dp[1][0], dp[1][1]));
28+
}
29+
30+
static void dfs(int u) {
31+
visited[u] = true;
32+
dp[u][0] = 0; //얼리 아답터 X
33+
dp[u][1] = 1; //얼리 아답터 O
34+
35+
for(int i=0; i<graph[u].size(); i++) {
36+
if(graph[u].get(i) == null || visited[graph[u].get(i)]) { //연결된 친구가 없거나, 이미 친구를 방문했다면 패스
37+
continue;
38+
}
39+
dfs(graph[u].get(i));
40+
dp[u][0] += dp[graph[u].get(i)][1]; //얼리어답터가 아니라면 나와 연결된 노드는 모두 얼리어답터O
41+
dp[u][1] += Math.min(dp[graph[u].get(i)][1],dp[graph[u].get(i)][0]); //얼리어답터라면 나와 연결된 노드는 얼리어답터O or 얼리어답터X
42+
}
43+
}
44+
45+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /