Skip to content

Commit 2f82115

Browse files
authored
[20250319] BOJ / G3 / 음악프로그램 / 김수연
1 parent a682979 commit 2f82115

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class boj2623 {
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+
int[] cnt = new int[N+1];
16+
ArrayList<Integer> answer = new ArrayList<>();
17+
ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
18+
for (int i = 0; i < N+1; i++) graph.add(new ArrayList<>());
19+
20+
for (int i = 0; i < M; i++) {
21+
nextLine();
22+
int num = nextInt();
23+
int prev = nextInt();
24+
for (int j = 0; j < num-1; j++) {
25+
int curr = nextInt();
26+
graph.get(prev).add(curr);
27+
cnt[curr]++;
28+
prev = curr;
29+
}
30+
}
31+
while (answer.size() != N) {
32+
boolean flag = true;
33+
for (int i = 1; i < N+1; i++) {
34+
if (cnt[i] == 0) {
35+
cnt[i] = -1;
36+
answer.add(i);
37+
for (int next : graph.get(i)) {
38+
cnt[next]--;
39+
}
40+
flag = false;
41+
}
42+
}
43+
if (flag) {
44+
System.out.println(0);
45+
return;
46+
}
47+
}
48+
for (int i = 0; i < N; i++) {
49+
System.out.println(answer.get(i));
50+
}
51+
}
52+
}
53+
54+
```

0 commit comments

Comments
 (0)