#C10152. Project Order Determination
Project Order Determination
Project Order Determination
You are given n projects labeled from 1 to n and m dependency relationships. Each dependency is given as two integers a and b, which means that project a depends on project b (i.e. project b must be completed before project a). Your task is to determine a valid order to complete all projects. Formally, find an ordering \( O = [p_1, p_2, \ldots, p_n] \) such that for every dependency \( a \rightarrow b \), project b appears before project a in \( O \). If no such ordering exists (due to cyclic dependencies), output -1.
If multiple orders are possible, any valid order will be accepted.
inputFormat
The first line of input contains two integers n and m ((1 \leq n \leq 10^5), (0 \leq m \leq 10^5)) separated by a space, where n is the number of projects and m is the number of dependencies. Each of the following m lines contains two integers a and b ((1 \leq a, b \leq n)) indicating that project a depends on project b.
outputFormat
Output a single line containing the valid order of projects as space-separated integers. If no valid order exists, output -1.## sample
3 0
1 2 3