problems / 110

Projects Grouped by Team

medium topological-sort graphs sorting 2000 ms · 256 MB

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 a before b, then a appears earlier than b in 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.

Any correct answer is accepted.

Example 1

Input

4 2 1
1 1 2 0
1 3

Output

1 2 3 4

Example 2

Input

4 2 2
1 1 2 2
1 3
4 2

Output

-1

Hints · reveal one at a time if you're stuck

Hint 1

Each team's projects must form one block, so a rule between two different teams only decides the order of whole blocks, while a rule inside one team only orders projects within that block. Treat each project with no team as a team of its own.

Hint 2

Order the teams topologically using only the cross-team rules. If that ordering is impossible, the answer is -1.

Hint 3

Then order all projects topologically using only the within-team rules, and print each team's projects in that relative order, teams in the order you found. A project that cannot be placed, including one with a rule to itself, means -1.

report an issue Prefer your own editor? sooks prep 110 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.