Skip to content

Commit

Permalink
Merge pull request #20 from AlgoLeadMe/6-Me-in-U
Browse files Browse the repository at this point in the history
6-Me-in-U
  • Loading branch information
Me-in-U authored Jul 14, 2024
2 parents 60b11d6 + 7e0026b commit 1e4af69
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
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();
}
}

0 comments on commit 1e4af69

Please sign in to comment.