Find Array A
Time limit2sMemory limit512 MB
Find the lexicographically smallest array A that comes after B and satisfies A[i] < A[j] for every given pair, with values up to 2*10^9.
- Level
Medium7 of 10
- Topics
- Graph, Topological sort, Greedy
- Solved
- No attempts yet
Problem
Find an array A that satisfies the following conditions.
- Its size is N, and every element is a natural number less than or equal to .
- It comes after the array B of size N in lexicographical order.
- For each of the M pairs , holds.
Input
The first line contains N and M, the size of the array and the number of pairs. The second line contains . From the third line on, each of the M lines contains and , meaning must hold.
Output
Print the lexicographically smallest array A that satisfies the conditions. If no array satisfies the conditions, print -1.
Constraints
- ,