diff --git a/BOJ-ios/README.md b/BOJ-ios/README.md index 713d2dc..2f8877f 100644 --- a/BOJ-ios/README.md +++ b/BOJ-ios/README.md @@ -6,4 +6,5 @@ | 2차시 | 2024.03.22 | 라빈카프 | [반복 부분문자열](https://www.acmicpc.net/problem/1605) [가장 긴 문자열](https://www.acmicpc.net/problem/3033) | [#8](https://github.com/AlgoLeadMe/AlgoLeadMe-9/pulls/8) | | 3차시 | 2024.03.26 | 펜윅트리 | [구간 합 구하기](https://www.acmicpc.net/problem/2042) | [#13](https://github.com/AlgoLeadMe/AlgoLeadMe-9/pulls/13) | | 4차시 | 2024.03.30 | 볼록껍질 | [볼록 껍질](https://www.acmicpc.net/problem/1708) | [#14](https://github.com/AlgoLeadMe/AlgoLeadMe-9/pulls/14) | -| 5차시 | 2025.04.29 | 스위핑 | [여러 직사각형의 전체 면적 구하기](https://www.acmicpc.net/problem/2672) | [#19](https://github.com/AlgoLeadMe/AlgoLeadMe-9/pulls/19) | \ No newline at end of file +| 5차시 | 2024.04.29 | 스위핑 | [여러 직사각형의 전체 면적 구하기](https://www.acmicpc.net/problem/2672) | [#19](https://github.com/AlgoLeadMe/AlgoLeadMe-9/pulls/19) | +| 6차시 | 2024.05.13 | 위상정렬 | [줄 세우기](https://www.acmicpc.net/problem/2252) | [#20](https://github.com/AlgoLeadMe/AlgoLeadMe-9/pulls/20) | \ No newline at end of file diff --git "a/BOJ-ios/\354\234\204\354\203\201\354\240\225\353\240\254/P2252\353\262\210_\354\244\204_\354\204\270\354\232\260\352\270\260/Main.java" "b/BOJ-ios/\354\234\204\354\203\201\354\240\225\353\240\254/P2252\353\262\210_\354\244\204_\354\204\270\354\232\260\352\270\260/Main.java" new file mode 100644 index 0000000..6493f8c --- /dev/null +++ "b/BOJ-ios/\354\234\204\354\203\201\354\240\225\353\240\254/P2252\353\262\210_\354\244\204_\354\204\270\354\232\260\352\270\260/Main.java" @@ -0,0 +1,53 @@ +package BAEKJOON.Gold.Gold_III.P2252번_줄_세우기; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.LinkedList; +import java.util.Queue; +import java.util.StringTokenizer; + +public class Main { + protected static int[] inDegree; + protected static LinkedList[] graph; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + int N = Integer.parseInt(st.nextToken()); + int M = Integer.parseInt(st.nextToken()); + inDegree = new int[N + 1]; + graph = new LinkedList[N + 1]; + for (int i = 0; i < N + 1; i++) { + graph[i] = new LinkedList<>(); + } + for (int j = 0; j < M; j++) { + st = new StringTokenizer(br.readLine()); + int A = Integer.parseInt(st.nextToken()); + int B = Integer.parseInt(st.nextToken()); + graph[A].add(B); + inDegree[B]++; + } + System.out.println(topologySort(N)); + } + + public static String topologySort(int N) { + StringBuilder sb = new StringBuilder(); + Queue queue = new LinkedList<>(); + for (int i = 1; i < N + 1; i++) { + if (inDegree[i] == 0) { + queue.add(i); + } + } + while (!queue.isEmpty()) { + int now = queue.poll(); + sb.append(now).append(' '); + for (int next : graph[now]) { + if (--inDegree[next] == 0) { + queue.add(next); + } + } + } + return sb.toString(); + } +}