← 返回 oracle 的题目列表Deploy Packages in Dependency Order
类型:online_judge
There are n software packages numbered from 0 to n - 1. You are given dependency pairs dependencies, where [a, b] means package b must be deployed before package a.
Return any valid deployment order of all packages. If the dependency graph contains a cycle and no complete deployment is possible, return an empty list.
Input Format
First line: integers n and m, the number of packages and dependencies.
Next m lines: two integers a b, meaning a depends on b.
Output Format
Print n package IDs separated by spaces if a valid order exists.
Print an empty line if no valid order exists.
Constraints
1 <= n <= 2 * 10^5
0 <= m <= 5 * 10^5
Example
Input:
4 4
1 0
2 0
3 1
3 2
Output:
0 1 2 3
0 2 1 3 is also valid.
Example
Input
4 4
1 0
2 0
3 1
3 2
Output
0 1 2 3