From 28afdee79490a3c662baa27acc9fcef7de7ed332 Mon Sep 17 00:00:00 2001 From: Ukj0ng <90972240+Ukj0ng@users.noreply.github.com> Date: Thu, 22 Jan 2026 15:12:53 +0900 Subject: [PATCH] =?UTF-8?q?[20260122]=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=ED=95=9C?= =?UTF-8?q?=EC=A2=85=EC=9A=B1?= 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" | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 "Ukj0ng/202601/22 BOJ G5 \355\212\270\353\246\254\354\231\200 \354\277\274\353\246\254.md" diff --git "a/Ukj0ng/202601/22 BOJ G5 \355\212\270\353\246\254\354\231\200 \354\277\274\353\246\254.md" "b/Ukj0ng/202601/22 BOJ G5 \355\212\270\353\246\254\354\231\200 \354\277\274\353\246\254.md" new file mode 100644 index 00000000..dd75f2e0 --- /dev/null +++ "b/Ukj0ng/202601/22 BOJ G5 \355\212\270\353\246\254\354\231\200 \354\277\274\353\246\254.md" @@ -0,0 +1,63 @@ +``` +import java.io.*; +import java.util.*; + +public class Main { + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + private static List[] graph; + private static int[] nodes; + private static boolean[] visited; + private static int N, R, Q; + + public static void main(String[] args) throws IOException { + init(); + + countSubNodes(R); + while (Q-->0) { + int q = Integer.parseInt(br.readLine()); + bw.write(nodes[q] + "\n"); + } + + bw.flush(); + bw.close(); + br.close(); + } + + private static void init() throws IOException { + StringTokenizer st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + R = Integer.parseInt(st.nextToken()); + Q = Integer.parseInt(st.nextToken()); + + graph = new List[N+1]; + nodes = new int[N+1]; + visited = new boolean[N+1]; + + for (int i = 1; i <= N; i++) { + graph[i] = new ArrayList<>(); + } + + Arrays.fill(nodes, 1); + + for (int i = 0; i < N-1; i++) { + st = new StringTokenizer(br.readLine()); + int U = Integer.parseInt(st.nextToken()); + int V = Integer.parseInt(st.nextToken()); + + graph[U].add(V); + graph[V].add(U); + } + } + + private static void countSubNodes(int start) { + visited[start] = true; + + for (int next : graph[start]) { + if (visited[next]) continue; + countSubNodes(next); + nodes[start] += nodes[next]; + } + } +} +```