From 8ab323ebeb3c69a864d246c96a6783bbfb4670ed Mon Sep 17 00:00:00 2001 From: Jonghwan Lee <123362165+0224LJH@users.noreply.github.com> Date: Mon, 8 Dec 2025 14:38:41 +0900 Subject: [PATCH] =?UTF-8?q?[20251208]=20BOJ=20/=20G5=20/=20=ED=8A=B8?= =?UTF-8?q?=EB=A6=AC=20/=20=EC=9D=B4=EC=A2=85=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../08 BOJ \355\212\270\353\246\254.md" | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 "0224LJH/202512/08 BOJ \355\212\270\353\246\254.md" diff --git "a/0224LJH/202512/08 BOJ \355\212\270\353\246\254.md" "b/0224LJH/202512/08 BOJ \355\212\270\353\246\254.md" new file mode 100644 index 00000000..858176c0 --- /dev/null +++ "b/0224LJH/202512/08 BOJ \355\212\270\353\246\254.md" @@ -0,0 +1,75 @@ +```java + +import java.util.StringTokenizer; +import java.util.*; +import java.io.*; + +public class Main { + + static int nodeCnt,ans; + static Node[] nodes; + static Node root; + + static class Node { + Node parent; + boolean alive = true; + Set children = new HashSet<>(); + + public void checkLeaf() { + int childCnt = 0; + for (Node n: children) { + if (!n.alive) continue; + childCnt++; + n.checkLeaf(); + } + if (childCnt==0) ans++; + } + + } + + + public static void main(String[] args) throws IOException { + init(); + process(); + print(); + + } + + private static void init() throws IOException{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + nodeCnt = Integer.parseInt(br.readLine()); + ans = 0; + + nodes = new Node[nodeCnt]; + for (int i = 0; i < nodeCnt; i++) { + nodes[i] = new Node(); + } + StringTokenizer st = new StringTokenizer(br.readLine()); + for (int i = 0; i < nodeCnt; i++) { + int parent = Integer.parseInt(st.nextToken()); + if (parent == -1) { + root = nodes[i]; + continue; + } + Node p = nodes[parent]; + nodes[i].parent = p; + p.children.add(nodes[i]); + } + + int targetNum = Integer.parseInt(br.readLine()); + Node target = nodes[targetNum]; + target.alive = false; + + } + + private static void process() throws IOException { + if (root.alive) root.checkLeaf(); + + } + + private static void print() { + System.out.println(ans); + } + +} +```