A project has n tasks forming a directed acyclic graph. A directed edge u v means u must be completed before v. Define the depth level of a task as the number of edges on the longest path that ends at it, starting from some task that has no prerequisites. Tasks with no prerequisites have depth level 0. Output the depth level of every task.
Line 1: two integers n and m — the number of nodes (labeled 0 through n-1) and the number of directed edges.
Each of the next m lines: two integers u v, a directed edge from u to v (read as: u must come before v).
It is guaranteed the graph is acyclic, with no self-loops and no duplicate edges.
One line with n space-separated integers: the depth level of node 0, node 1, ..., node n-1.
Example 1
Input
4 3 0 1 1 2 0 2
Expected
0 1 2 0
Explanation
Node 0 is a source (0). Node 1 has depth 1. Node 2's longest chain is 0->1->2, depth 2. Node 3 is isolated, depth 0.
Example 2
Input
3 0
Expected
0 0 0
Explanation
No edges, so every node is a source with depth level 0.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →