Unique topological ordering

Write codeMedium30 minGoogle and Meta style

Your task

Given n items labeled 0..n-1 and a list of ordering constraints [a, b] meaning a must come before b, determine whether the constraints pin down exactly ONE valid total ordering of all items. Return True if a unique topological order exists, and False if there are zero valid orders (a cycle) or more than one. Duplicate constraints should be treated as a single constraint. There are at most 10^4 items.

Examples

unique_topo_order(n: int, edges: list[list[int]]) -> bool
Example 1
Inputunique_topo_order(3, [[0, 1], [1, 2]])
OutputTrue