์์ ์ ๋ ฌ ์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ ์ธ๋ฐ ๋จผ์ ํ์ด์ผ ํ๋ ๋ฌธ์ ๊ฐ ์๋ ๋ฌธ์ ๋ค ์ค์์ ๊ฐ์ฅ ์ฌ์ด ๋ฌธ์ (์ซ์๊ฐ ๋ฎ์ ๋ฌธ์ )๋ถํฐ ํ์ด์ผ ํ๊ธฐ ๋๋ฌธ์ ์กฐ๊ฑด์ ํด๋นํ๋ ๋ฌธ์ ๋ค์ ๋ด์ ๋ ํ๋ฅผ ์ฐ์ ์์ ํ๋ฅผ ์ฌ์ฉํ๋ค. ๋งค๋ฒ ์ ๋ ฌํ๋ ๋์ ๋งค๋ฒ ํ์ด์ผ ํ๋ ๋ฌธ์ ๊ฐ ๊ฐ์ฅ ์์ ๋ฌธ์ ๋ถํฐ ๋์ฌ ์ ์๋๋ก ํ๋ค.
import java.io.*;
import java.util.*;
public class Main {
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());
int A;
int B;
List<List<Integer>> workbook = new ArrayList<>();
int[] priority = new int[N+1];
for (int i = 0; i <= N; i++) {
workbook.add(new ArrayList<>());
}
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
A = Integer.parseInt(st.nextToken());
B = Integer.parseInt(st.nextToken());
priority[B]++;
workbook.get(A).add(B);
}
PriorityQueue<Integer> pq = new PriorityQueue<>();
for (int i = 1; i <= N; i++) {
if (priority[i] == 0) {
pq.add(i);
}
}
StringBuilder sb = new StringBuilder();
while (!pq.isEmpty()) {
int problem = pq.poll();
sb.append(problem + " ");
for (int nextProblem : workbook.get(problem)) {
priority[nextProblem]--;
if (priority[nextProblem] == 0) {
pq.add(nextProblem);
}
}
}
System.out.println(sb);
}
}