Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

6-Me-in-U #20

Merged
merged 4 commits into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion BOJ-ios/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
| 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) |
Original file line number Diff line number Diff line change
@@ -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<Integer>[] 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<Integer> 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();
}
}