41 lines
1.0 KiB
Java
41 lines
1.0 KiB
Java
import java.util.*;
|
|
|
|
public class _1766 {
|
|
public static void main(String[] args) {
|
|
Scanner sc = new Scanner(System.in);
|
|
int N = sc.nextInt(), M = sc.nextInt();
|
|
|
|
int[] indegree = new int[N+1];
|
|
@SuppressWarnings("unchecked")
|
|
ArrayList<Integer>[] graph = new ArrayList[N+1];
|
|
for(int i=0; i<=N; i++) graph[i] = new ArrayList<>();
|
|
|
|
for(int i=0; i<M; i++) {
|
|
int a = sc.nextInt(), b = sc.nextInt();
|
|
indegree[b]++;
|
|
graph[a].add(b);
|
|
}
|
|
|
|
sc.close();
|
|
|
|
PriorityQueue<Integer> pq = new PriorityQueue<>();
|
|
for(int i=1; i<=N; i++) if(indegree[i] == 0) pq.add(i);
|
|
|
|
ArrayList<Integer> ans = new ArrayList<>();
|
|
|
|
while(!pq.isEmpty()) {
|
|
int now = pq.poll();
|
|
ans.add(now);
|
|
|
|
for (int next : graph[now]) {
|
|
indegree[next]--;
|
|
if(indegree[next] == 0) pq.add(next);
|
|
}
|
|
}
|
|
|
|
for (int n : ans) {
|
|
System.out.printf("%d ",n);
|
|
}
|
|
}
|
|
}
|