Topological order
Xem dạng PDF
Gửi bài giải
Điểm:
1,00 (OI)
Giới hạn thời gian:
1.0s
Giới hạn bộ nhớ:
256M
Input:
stdin
Output:
stdout
Người đăng:
Dạng bài
Ngôn ngữ cho phép
C, C++, Java, Kotlin, Pascal, PyPy, Python
Given a directed graph, find a valid topological ordering of its vertices. A topological order is a linear ordering of vertices such that for every directed edge u → v, vertex u comes before v in the ordering.
Input
The first line contains two positive integers: n, the number of vertices (0 < n < 10^5), and m, the number of edges (m ≤ 2 x 10^5)
The next m lines each contains two integers u, v (0 ≤ u, v < n), representing an edge from node u to node v.
Output
Print n space-separated integers representing a valid topological order of the vertices.
If there is more than one valid ordering, any of them is acceptable.
If no valid topological ordering exists, print -1.
Example
Input
6 6
5 2
5 0
4 0
4 1
2 3
3 1
Output
4 5 2 3 1 0
Bình luận