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:
- The relative order of Alice's tasks
- The relative order of Bob's tasks
- 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_iis always from Alice's list andb_iis always from Bob's list.