Two Paths
Time limit5sMemory limit1024 MB
Given a DAG, find edge-disjoint simple paths from A to B and from C to D with the minimum total length, or report NO.
- Level
Medium7 of 10
- Topics
- Graph, Shortest path, Topological sort
- Solved
- No attempts yet
Statement
You are given a directed acyclic graph with two fixed pairs of vertices: and . Consider all pairs of edge-disjoint simple paths, one from to and another from to . Find the pair with minimal total length.
Input
The first line contains two integers and : the number of vertices and edges in the graph (, ). Each of the next lines contains two integers and , meaning there is an edge from to (). The graph has no loops, no multiple edges, and no cycles. The last line contains the fixed vertices in the order , , , ().
Output
If no pair of paths exists, print a single line with the word NO. Otherwise, print YES on the first line, then the path from to , then the path from to . Each path takes two lines: the number of vertices in the path, then the vertices in the order they are visited. Each path must be simple, so no vertex appears more than once in the same path. If several solutions exist, print any of them.