From 79d81a8ad41bc5e5734cfdbf000a51fbe1ddd8f7 Mon Sep 17 00:00:00 2001 From: lkhyun <102892446+lkhyun@users.noreply.github.com> Date: Sun, 6 Jul 2025 14:07:55 +0900 Subject: [PATCH] =?UTF-8?q?[20250706]=20BOJ=20/=20G2=20/=20=EB=AC=B8?= =?UTF-8?q?=EC=A0=9C=EC=A7=91=20/=20=EC=9D=B4=EA=B0=95=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...2 \353\254\270\354\240\234\354\247\221.md" | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 "lkhyun/202507/6 BOJ G2 \353\254\270\354\240\234\354\247\221.md" diff --git "a/lkhyun/202507/6 BOJ G2 \353\254\270\354\240\234\354\247\221.md" "b/lkhyun/202507/6 BOJ G2 \353\254\270\354\240\234\354\247\221.md" new file mode 100644 index 00000000..9d19b575 --- /dev/null +++ "b/lkhyun/202507/6 BOJ G2 \353\254\270\354\240\234\354\247\221.md" @@ -0,0 +1,56 @@ +```java +import java.util.*; +import java.io.*; +public class Main { + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + static StringTokenizer st; + static int N,M; + static List[] adj; + static int[] degree; + + public static void main(String[] args) throws IOException { + st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + adj = new List[N+1]; + degree = new int[N+1]; + + for (int i = 1; i <= N; i++) { + adj[i] = new ArrayList<>(); + } + + for (int i = 0; i < M; i++) { + st = new StringTokenizer(br.readLine()); + int a = Integer.parseInt(st.nextToken()); + int b = Integer.parseInt(st.nextToken()); + adj[a].add(b); + degree[b]++; + } + + + BFS(); + bw.close(); + } + static void BFS() throws IOException { + PriorityQueue pq = new PriorityQueue<>(); + for (int i = 1; i <= N; i++) { + if(degree[i] == 0){ + pq.add(i); + } + } + + while(!pq.isEmpty()){ + int u = pq.poll(); + bw.write(u+" "); + + for(int v : adj[u]){ + if(--degree[v] == 0){ + pq.add(v); + } + } + } + } +} + +```