From 15afb297a365cc13cc0d36c2f066b5c7ad359aa7 Mon Sep 17 00:00:00 2001 From: suyeun84 <81475092+suyeun84@users.noreply.github.com> Date: Mon, 4 Aug 2025 16:38:08 +0900 Subject: [PATCH] =?UTF-8?q?[20250804]=20BOJ=20/=20G5=20/=20=EC=84=A0?= =?UTF-8?q?=EC=88=98=EA=B3=BC=EB=AA=A9=20/=20=EA=B9=80=EC=88=98=EC=97=B0?= 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" | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 "suyeun84/202508/04 BOJ G5 \354\204\240\354\210\230\352\263\274\353\252\251.md" diff --git "a/suyeun84/202508/04 BOJ G5 \354\204\240\354\210\230\352\263\274\353\252\251.md" "b/suyeun84/202508/04 BOJ G5 \354\204\240\354\210\230\352\263\274\353\252\251.md" new file mode 100644 index 00000000..2e1ee009 --- /dev/null +++ "b/suyeun84/202508/04 BOJ G5 \354\204\240\354\210\230\352\263\274\353\252\251.md" @@ -0,0 +1,44 @@ +```java +import java.util.*; +import java.io.*; + +public class boj14567 { + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static StringTokenizer st; + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} + static int nextInt() {return Integer.parseInt(st.nextToken());} + + public static void main(String[] args) throws Exception { + nextLine(); + int N = nextInt(); + int M = nextInt(); + List> graph = new ArrayList<>(); + Queue q = new LinkedList<>(); + int[] degree = new int[N+1]; + int[] answer = new int[N+1]; + + for (int i = 0; i <= N; i++) graph.add(new ArrayList<>()); + for (int i = 0; i < M; i++) { + nextLine(); + int a = nextInt(); + int b = nextInt(); + graph.get(a).add(b); + degree[b]++; + } + for (int i = 1; i <= N; i++) if (degree[i] == 0) q.offer(i); + int cnt = 1; + while (!q.isEmpty()) { + int size = q.size(); + while (size-- > 0) { + int cur = q.poll(); + answer[cur] = cnt; + for (int next : graph.get(cur)) { + if (--degree[next] == 0) q.offer(next); + } + } + cnt++; + } + for (int i = 1; i <= N; i++) System.out.print(answer[i] + " "); + } +} +```