From f8bb64babd34c5ad3f8a7e204f4423b7cd4767bf Mon Sep 17 00:00:00 2001 From: lkhyun <102892446+lkhyun@users.noreply.github.com> Date: Tue, 24 Jun 2025 11:06:05 +0900 Subject: [PATCH] =?UTF-8?q?[20250624]=20BOJ=20/=20G5=20/=20=ED=8A=B8?= =?UTF-8?q?=EB=A6=AC=EC=99=80=20=EC=BF=BC=EB=A6=AC=20/=20=EC=9D=B4?= =?UTF-8?q?=EA=B0=95=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\354\231\200 \354\277\274\353\246\254.md" | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 "lkhyun/202506/24 BOJ G5 \355\212\270\353\246\254\354\231\200 \354\277\274\353\246\254.md" diff --git "a/lkhyun/202506/24 BOJ G5 \355\212\270\353\246\254\354\231\200 \354\277\274\353\246\254.md" "b/lkhyun/202506/24 BOJ G5 \355\212\270\353\246\254\354\231\200 \354\277\274\353\246\254.md" new file mode 100644 index 00000000..528c8948 --- /dev/null +++ "b/lkhyun/202506/24 BOJ G5 \355\212\270\353\246\254\354\231\200 \354\277\274\353\246\254.md" @@ -0,0 +1,55 @@ +```java +import java.util.*; +import java.io.*; +public class Main { + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + static StringTokenizer st; + static int N,R,Q; + static List[] adjList; + static int[] count; + static boolean[] visited; + + + public static void main(String[] args) throws IOException { + st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + R = Integer.parseInt(st.nextToken()); + Q = Integer.parseInt(st.nextToken()); + + adjList = new List[N+1]; + for (int i = 1; i <= N; i++) { + adjList[i] = new ArrayList<>(); + } + + for (int i = 1; i < N; i++) { + st = new StringTokenizer(br.readLine()); + int u = Integer.parseInt(st.nextToken()); + int v = Integer.parseInt(st.nextToken()); + adjList[u].add(v); + adjList[v].add(u); + } + count = new int[N+1]; + visited = new boolean[N+1]; + find(R); + + for (int i = 0; i < Q; i++) { + int start = Integer.parseInt(br.readLine()); + bw.write(count[start] + "\n"); + } + bw.close(); + } + static int find(int u) { + if(visited[u]) return 0; + int cnt = 1; + visited[u] = true; + + for(int v : adjList[u]) { + cnt += find(v); + } + count[u] = cnt; + return cnt; + } +} + +```