Escape Plan
InterviewTime limit2sMemory limit1024 MB
For each room, find the nearest exit left open after the K nearest exits close, breaking distance ties by the smaller room number.
Statement
The blueprints for an innovative power plant were stolen from the office of your energy corporation. If the plant were built, it would solve most problems of modern energy and make you incredibly rich. Do not despair yet. The security service tracked the thief and found that the blueprints are now kept in an underground bunker owned by a competitor. Your best agent has already set out to steal the blueprints back, but the mission is very hard!
The competitor's system still has the Heartbleed vulnerability, so your IT department got limited access to the bunker's security system and found its floor plan. The bunker has rooms and corridors connecting them. Every corridor can be walked in both directions. No corridor connects a room to itself, and no two rooms are connected by more than one corridor (who would build corridors any other way?).
Some rooms contain exits from the bunker. As soon as an agent takes the blueprints, the alarm goes off in the room where the agent is. After that, all exits close and the agent is trapped.
Our IT team, toughened by CTF competitions, changed the security system so that when the alarm goes off in room , not all exits close, only the closest to that room. The distance between two rooms is the minimum number of corridors that must be traversed to get from one to the other. Exits at the same distance from room close in increasing order of their room numbers.
Input
The first line contains two space-separated integers and , the number of rooms and corridors (, ). Each of the next lines contains two integers and , the rooms connected by the -th corridor (). The next line contains , the number of rooms with exits (). The next line contains distinct space-separated integers , the rooms with exits (). The last line contains , the number of exits closed by the alarm ().
Output
Print integers. The -th integer is the number of the nearest room with an open exit after the alarm goes off in room . If several suitable rooms are equally close, choose the one with the smallest number. If room offers no escape, print -1.
Hints
In the first example , so each room just needs its nearest exit. The answer is unique for every room except room 2. Rooms 1 and 3 each have an exit one corridor away, so the answer is the smaller number, room 1.
In the second example, rooms 6 through 9 cannot escape. Only two exits are reachable from them, and the alarm closes both.