Projects Grouped by Team
A company has n projects, numbered 1 to n. Each project belongs to exactly one of k
teams, numbered 1 to k, or to no team at all.
There are m ordering rules. Each rule names two projects a and b and means project a
must be scheduled strictly before project b.
You must produce a single schedule: a permutation of all n projects, listed one per line in
the order they will be worked on, such that:
- every rule is respected (if a rule says
abeforeb, thenaappears earlier thanbin the schedule), and - for every team, the projects belonging to that team occupy a contiguous block of positions in the schedule (nothing from another team, or with no team, is interleaved between them).
A project with no team is not required to sit next to anything in particular — it is free to appear anywhere, as if it were the sole member of its own private team.
Several schedules may satisfy both requirements; print any one of them. If no schedule can
satisfy both requirements, print -1 instead.
Input
The first line contains three integers n, k and m (1 ≤ n ≤ 10^5, 0 ≤ k ≤ n,
0 ≤ m ≤ 10^5).
The second line contains n integers t_1, t_2, ..., t_n (0 ≤ t_i ≤ k), where t_i is
the team of project i, or 0 if project i has no team. Every value from 1 to k is used
by at least one project.
Each of the next m lines contains two integers a and b (1 ≤ a, b ≤ n) describing a
rule: project a must come strictly before project b. The same pair may appear more than
once, and a may equal b.
Output
If a valid schedule exists, print n integers: a permutation of 1 to n giving the projects
in schedule order, separated by spaces (or on separate lines).
If no valid schedule exists, print a single line containing -1.
If several valid schedules exist, any one of them is accepted.
Notes
In the first example, projects 1 and 2 belong to team 1, project 3 belongs to team 2, and
project 4 has no team. The rule forces project 1 before project 3. Printing 1 2 3 4 keeps
team 1's projects (1, 2) together, team 2's single project (3) together with itself, respects
the rule (1 before 3), and places the teamless project 4 wherever convenient.
In the second example, a rule forces team 1's project before team 2's project while another
rule forces team 2's project before team 1's project, so no order can keep both teams
contiguous and satisfy both rules; the answer is -1.