From 2a3d87599c74aadbece080cfa7eec1a80456ab5e Mon Sep 17 00:00:00 2001 From: Jonghwan Lee <123362165+0224LJH@users.noreply.github.com> Date: Tue, 23 Sep 2025 18:47:59 +0900 Subject: [PATCH] =?UTF-8?q?[20250923]=20BOJ=20/=20G5=20/=20=EC=84=A0?= =?UTF-8?q?=EC=88=98=EA=B3=BC=EB=AA=A9=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 --- ...40\354\210\230\352\263\274\353\252\251.md" | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 "0224LJH/202509/23 BOJ \354\204\240\354\210\230\352\263\274\353\252\251.md" diff --git "a/0224LJH/202509/23 BOJ \354\204\240\354\210\230\352\263\274\353\252\251.md" "b/0224LJH/202509/23 BOJ \354\204\240\354\210\230\352\263\274\353\252\251.md" new file mode 100644 index 00000000..0e468190 --- /dev/null +++ "b/0224LJH/202509/23 BOJ \354\204\240\354\210\230\352\263\274\353\252\251.md" @@ -0,0 +1,92 @@ +```java +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.*; + +public class Main { + + static int nodeCnt, preCnt,ans; + static Node[] nodes;; + static StringBuilder sb = new StringBuilder(); + + + static class Node{ + int semester; + int num; + int parentCnt; + HashSet children; + + public Node(int num) { + semester = 1; + parentCnt = 0; + this.num = num; + children = new HashSet<>(); + + } + } + + public static void main(String[] args) throws NumberFormatException, IOException { + init(); + process(); + print(); + + + } + + public static void init() throws NumberFormatException, IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + nodeCnt = Integer.parseInt(st.nextToken()); + preCnt = Integer.parseInt(st.nextToken()); + nodes = new Node[nodeCnt+1]; + for (int i = 1; i <= nodeCnt; i++) { + nodes[i] = new Node(i); + } + + for (int i = 0; i < preCnt; i++) { + st = new StringTokenizer(br.readLine()); + int parent = Integer.parseInt(st.nextToken()); + int child = Integer.parseInt(st.nextToken()); + + nodes[parent].children.add(nodes[child]); + nodes[child].parentCnt++; + + } + + } + + public static void process() { + Queue pq = new LinkedList<>(); + + for (int i = 1; i <= nodeCnt; i++ ) { + + Node node = nodes[i]; + if (node.parentCnt ==0) pq.add(node); + + } + + while (!pq.isEmpty()) { + Node node = pq.poll(); + for (Node n: node.children ) { + n.parentCnt--; + if (n.parentCnt == 0) { + n.semester = node.semester+1; + pq.add(n); + } + } + } + + + for (int i = 1; i<= nodeCnt; i++) { + sb.append(nodes[i].semester+ " "); + } + } + + + + public static void print() { + System.out.println(sb.toString()); + } +} +```