Skip to content

Commit 5c2e28f

Browse files
authored
Merge pull request #606 from AlgorithmWithGod/suyeun84
[20250804] BOJ / G5 / 선수과목 / 김수연
2 parents 586f3af + 15afb29 commit 5c2e28f

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
public class boj14567 {
6+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
static StringTokenizer st;
8+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
9+
static int nextInt() {return Integer.parseInt(st.nextToken());}
10+
11+
public static void main(String[] args) throws Exception {
12+
nextLine();
13+
int N = nextInt();
14+
int M = nextInt();
15+
List<List<Integer>> graph = new ArrayList<>();
16+
Queue<Integer> q = new LinkedList<>();
17+
int[] degree = new int[N+1];
18+
int[] answer = new int[N+1];
19+
20+
for (int i = 0; i <= N; i++) graph.add(new ArrayList<>());
21+
for (int i = 0; i < M; i++) {
22+
nextLine();
23+
int a = nextInt();
24+
int b = nextInt();
25+
graph.get(a).add(b);
26+
degree[b]++;
27+
}
28+
for (int i = 1; i <= N; i++) if (degree[i] == 0) q.offer(i);
29+
int cnt = 1;
30+
while (!q.isEmpty()) {
31+
int size = q.size();
32+
while (size-- > 0) {
33+
int cur = q.poll();
34+
answer[cur] = cnt;
35+
for (int next : graph.get(cur)) {
36+
if (--degree[next] == 0) q.offer(next);
37+
}
38+
}
39+
cnt++;
40+
}
41+
for (int i = 1; i <= N; i++) System.out.print(answer[i] + " ");
42+
}
43+
}
44+
```

0 commit comments

Comments
 (0)