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] + " "); + } +} +```