← 返回 netflix 的题目列表Topological Sort / Dependency Resolution
类型:online_judge
Coding: Topological Sort / Dependency Resolution
Given a directed graph representing dependencies among tasks labeled 0..n-1.
Input
First line: two integers n m (number of tasks and edges).
Next m lines: two integers u v meaning u must be done before v (edge u -> v).
Output
If a valid ordering exists, print any topological order (space-separated n integers).
If the graph contains a cycle, print IMPOSSIBLE.
Constraints
1 <= n <= 2e5
0 <= m <= 2e5
Example
Input:
4 3
0 1
1 2
0 3
One valid output:
0 1 3 2
Example
Input
4 3
0 1
1 2
0 3
Output
0 1 3 2