Rick's Robe
InterviewTime limit3sMemory limit1024 MB
Given a graph with wet vertices and existing holes, find the vertex for a new hole that minimizes the time for every wet point to reach its nearest hole.
- Level
Medium5 of 10
- Topics
- BFS, Graph, Brute force
- Solved
- No attempts yet
Statement
Rick decided to wash his robe. He has little time before his next crazy adventure, so instead of a proper wash he wet points of the robe with a special solution and is now waiting for the robe to dry.
The solution Rick made has a kind of mind of its own. Instead of evaporating, it spreads across the robe. The robe can be represented as an undirected graph with vertices and edges. The wet points are distinct vertices of the graph. The robe is also old and has holes (another distinct vertices), and the solution can drain through them.
Rick would never make a solution that works inefficiently. The solution from each initial point flows to the nearest hole and drains out through it. Passing through one edge of the fabric takes one unit of time. Any amount of solution can flow along one edge at the same time.
Rick computed how long it takes for all the solution to drain out through the holes and decided that this is too long. So he wants to make one more hole at any vertex to speed up the process. Help him find the vertex to make a hole in, so that the time until every drop reaches its nearest hole is minimized.
The solution acts instantly, so after the solution is applied, you may make a hole at a spot where the solution already is, if that minimizes the answer. If several choices are possible, Rick asks you to find the vertex with the smallest number at which adding a hole minimizes the required distance.
Input
The first line contains four integers , , , and separated by spaces: the number of vertices and edges of the robe, the number of initially wet points, and the number of holes. (; ; ).
The next line contains distinct integers , the numbers of the vertices that Rick wet with the solution ().
The next line contains distinct integers , the numbers of the vertices where the robe has holes ().
Each of the next lines contains two integers and , meaning there is an edge between vertices and ().
Output
Print on one line, separated by a space, the number of the vertex to make a hole in and the minimum drying time of the robe.
You may print the number of a vertex that already has a hole if no other choice improves the answer. If several answers are possible, print the one with the smallest vertex number, whether or not that vertex already has a hole.