problems / 173

Alice and Bob's Sequence

easy topological-sort graphs constructive 2000 ms · 256 MB

Alice and Bob's Sequence

Alice and Bob are organizing a joint project with ordered task lists. Alice has n tasks that must be completed in a specific order, and Bob has m tasks that must also be completed in his specific order. However, their project manager has imposed k additional constraints: certain tasks of Alice must be completed before certain tasks of Bob.

Your job is to determine whether it's possible to merge their task lists into a single sequence that respects:

  1. The relative order of Alice's tasks
  2. The relative order of Bob's tasks
  3. All the cross-task dependencies

If such a sequence exists, output YES and provide one valid merged sequence. Otherwise, output NO.

Input

The first line contains three integers n, m, and k (1 ≤ n, m ≤ 5·10^4, 0 ≤ k ≤ 5·10^4).

The second line contains n distinct task identifiers for Alice's tasks (space-separated integers from 1 to 10^9).

The third line contains m distinct task identifiers for Bob's tasks (space-separated integers from 1 to 10^9).

The following k lines each contain two integers a_i and b_i, meaning Alice's task a_i must be completed before Bob's task b_i. Both a_i and b_i are valid task identifiers from the respective lists.

Output

If no valid merged sequence exists, print NO.

If a valid merged sequence exists, print YES on the first line, followed by a second line with n + m space-separated task identifiers representing one valid merged sequence.

Notes

  • Task identifiers from Alice's and Bob's lists are distinct (no task belongs to both).
  • The constraints are consistent: a_i is always from Alice's list and b_i is always from Bob's list.

Any correct answer is accepted.

Example 1

Input

3 2 0
1 2 3
4 5

Output

YES
1 2 3 4 5

Example 2

Input

2 2 1
10 20
30 40
20 30

Output

YES
10 20 30 40

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

Hint 1

A valid merge must keep Alice's order and Bob's order, and also satisfy every cross constraint. Treat each task as a node. Which edges does each kind of requirement give you?

Hint 2

Consecutive tasks in Alice's list and in Bob's list each give a precedence edge, and each cross constraint gives one more. The question is then whether these precedences can all be satisfied at once.

Hint 3

Find a topological ordering of this graph, for example with Kahn's algorithm. If every node gets processed the order is a valid merge, otherwise a cycle makes it impossible and you print NO.

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

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