From 286330acb1ea0f66e7fc40c1a0888b422e3c03ec Mon Sep 17 00:00:00 2001 From: Jinyeong Seol Date: Sat, 25 Oct 2025 23:50:36 +0900 Subject: [PATCH] =?UTF-8?q?[20251025]=20BOJ=20/=20G5=20/=20=ED=8A=B8?= =?UTF-8?q?=EB=A6=AC=20/=20=EC=84=A4=EC=A7=84=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5 \355\212\270\353\246\254.md\342\200\216" | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 "Seol-JY/202510/25 BOJ G5 \355\212\270\353\246\254.md\342\200\216" diff --git "a/Seol-JY/202510/25 BOJ G5 \355\212\270\353\246\254.md\342\200\216" "b/Seol-JY/202510/25 BOJ G5 \355\212\270\353\246\254.md\342\200\216" new file mode 100644 index 00000000..b37f614b --- /dev/null +++ "b/Seol-JY/202510/25 BOJ G5 \355\212\270\353\246\254.md\342\200\216" @@ -0,0 +1,69 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + static int N; + static List[] tree; + static boolean[] removed; + static int deleteNode; + + 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]; + removed = new boolean[N]; + + for (int i = 0; i < N; i++) { + tree[i] = new ArrayList<>(); + } + + StringTokenizer st = new StringTokenizer(br.readLine()); + int 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); + } + } + + deleteNode = Integer.parseInt(br.readLine()); + + removeNode(deleteNode); + + if (removed[root]) { + System.out.println(0); + return; + } + + System.out.println(countLeaf(root)); + } + + static void removeNode(int node) { + removed[node] = true; + for (int child : tree[node]) { + removeNode(child); + } + } + + static int countLeaf(int node) { + if (removed[node]) return 0; + + int childCount = 0; + int leafCount = 0; + + for (int child : tree[node]) { + if (!removed[child]) { + childCount++; + leafCount += countLeaf(child); + } + } + + return childCount == 0 ? 1 : leafCount; + } +} +```