From cdd0599e1f0b35aedfeca02eab9efd36f474be38 Mon Sep 17 00:00:00 2001 From: JHLEE325 <82587652+JHLEE325@users.noreply.github.com> Date: Sat, 25 Oct 2025 23:12:52 +0900 Subject: [PATCH 1/2] =?UTF-8?q?Create=2025=20BOJ=20G5=20=ED=8A=B8=EB=A6=AC?= =?UTF-8?q?.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../25 BOJ G5 \355\212\270\353\246\254.md" | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 "JHLEE325/202510/25 BOJ G5 \355\212\270\353\246\254.md" diff --git "a/JHLEE325/202510/25 BOJ G5 \355\212\270\353\246\254.md" "b/JHLEE325/202510/25 BOJ G5 \355\212\270\353\246\254.md" new file mode 100644 index 00000000..190ffa2c --- /dev/null +++ "b/JHLEE325/202510/25 BOJ G5 \355\212\270\353\246\254.md" @@ -0,0 +1,61 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + static int N; + static int dnode; + static List[] tree; + static boolean[] visited; + static int leafcount = 0; + static int root; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + N = Integer.parseInt(br.readLine()); + tree = new ArrayList[N]; + visited = new boolean[N]; + for (int i = 0; i < N; i++) { + tree[i] = new ArrayList<>(); + } + + StringTokenizer st = new StringTokenizer(br.readLine()); + root = -1; + for (int i = 0; i < N; i++) { + int parent = Integer.parseInt(st.nextToken()); + if (parent == -1) { + root = i; + } else { + tree[parent].add(i); + } + } + + dnode = Integer.parseInt(br.readLine()); + + if (dnode == root) { + System.out.println(0); + return; + } + + dfs(root); + System.out.println(leafcount); + } + + static void dfs(int now) { + visited[now] = true; + int childCount = 0; + + for (int next : tree[now]) { + if (next == dnode) continue; + if (!visited[next]) { + dfs(next); + childCount++; + } + } + + if (childCount == 0) { + leafcount++; + } + } +} +``` From 96b3a7facafb334380c6e3c0e320f1c16fcb4946 Mon Sep 17 00:00:00 2001 From: JHLEE325 <82587652+JHLEE325@users.noreply.github.com> Date: Sat, 25 Oct 2025 23:13:26 +0900 Subject: [PATCH 2/2] =?UTF-8?q?[20251025]=20BOJ=20/=20G5=20/=20=ED=8A=B8?= =?UTF-8?q?=EB=A6=AC=20/=20=EC=9D=B4=EC=A4=80=ED=9D=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "JHLEE325/202510/25 BOJ G5 \355\212\270\353\246\254.md" | 1 + 1 file changed, 1 insertion(+) diff --git "a/JHLEE325/202510/25 BOJ G5 \355\212\270\353\246\254.md" "b/JHLEE325/202510/25 BOJ G5 \355\212\270\353\246\254.md" index 190ffa2c..10915cc9 100644 --- "a/JHLEE325/202510/25 BOJ G5 \355\212\270\353\246\254.md" +++ "b/JHLEE325/202510/25 BOJ G5 \355\212\270\353\246\254.md" @@ -58,4 +58,5 @@ public class Main { } } } + ```