From 432d7a68e80a03aaebb9f40a39b2d6ae548255a7 Mon Sep 17 00:00:00 2001 From: Jonghwan Lee <123362165+0224LJH@users.noreply.github.com> Date: Mon, 15 Sep 2025 21:51:01 +0900 Subject: [PATCH] =?UTF-8?q?[20250915]=20BOJ=20/=20G2=20/=20=EB=AC=B8?= =?UTF-8?q?=EC=A0=9C=EC=A7=91=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 --- ...J \353\254\270\354\240\234\354\247\221.md" | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 "0224LJH/202509/15 BOJ \353\254\270\354\240\234\354\247\221.md" diff --git "a/0224LJH/202509/15 BOJ \353\254\270\354\240\234\354\247\221.md" "b/0224LJH/202509/15 BOJ \353\254\270\354\240\234\354\247\221.md" new file mode 100644 index 00000000..27f35eb7 --- /dev/null +++ "b/0224LJH/202509/15 BOJ \353\254\270\354\240\234\354\247\221.md" @@ -0,0 +1,83 @@ +```java +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.*; + +public class Main { + static int nodeCnt, infoCnt; + static Node[] nodes; + static StringBuilder sb = new StringBuilder(); + static class Node implements Comparable{ + int num; + int beforeCnt = 0; // 이 노드 전에 나와야 하는 노드 수 + HashSet after = new HashSet<>(); + + + + public Node(int num) { + this.num = num; + } + + @Override + public int compareTo(Node n) { + return Integer.compare(this.num, n.num); + } + } + + + 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()); + infoCnt = 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 < infoCnt; i++) { + st = new StringTokenizer(br.readLine()); + int before = Integer.parseInt(st.nextToken()); + int after = Integer.parseInt(st.nextToken()); + + nodes[after].beforeCnt++; + nodes[before].after.add(nodes[after]); + } + + } + + public static void process() { + PriorityQueue pq = new PriorityQueue<>(); + for (int i = 1; i<= nodeCnt; i++) { + if (nodes[i].beforeCnt != 0 )continue; + pq.add(nodes[i]); + } + + while(!pq.isEmpty()) { + Node n =pq.poll(); + if ( n.beforeCnt != 0) { + pq.add(n); + continue; + } + + sb.append(n.num).append(" "); + + for (Node b: n.after) { + b.beforeCnt--; + if (b.beforeCnt == 0) pq.add(b); + } + } + + } + + + public static void print() { + System.out.println(sb.toString()); + } +} +```