One-Way Roads
InterviewTime limit2sMemory limit64 MB
Decide whether the undirected streets of a graph can all be oriented so that each required ordered pair stays reachable from the first to the second.
- Level
Hard8 of 10
- Topics
- Graph, DFS, Union-find, Greedy
- Solved
- No attempts yet
Problem
The capital of Byteland suffers from severe traffic congestion, so the city authorities have decided to make every street one-way. If the directions are chosen carelessly, some junctions may become unreachable from others.
The transport department has prepared a list of pairs of junctions that must stay connected after the change. For each pair , once every street has been made one-way it must still be possible to travel from junction to junction .
Write a program that decides whether it is possible to assign a direction to every street so that all of these requirements are satisfied.
Input
The first line contains three integers , , and (, ): the number of junctions, the number of streets, and the number of requirements. Junctions are numbered from to .
Each of the next lines contains two integers and (, ), describing a two-way street between junctions and . There is at most one street between any pair of junctions.
Each of the next lines contains two integers and (, ), meaning that after every street is made one-way it must be possible to travel from to .
Output
Print YES if every street can be made one-way so that all requirements are satisfied, and NO otherwise.