loj#P3560. 「BalticOI 2021 Day1」From Hacks to Snitches
「BalticOI 2021 Day1」From Hacks to Snitches
Description
As one can’t make a living from the prizes awarded at computer science competitions, you have decided to get into the art business—or, more precisely, into a certain museum you have chosen for your debut as an art thief. Unfortunately, this museum is guarded quite well: there are watchmen patrolling the building, each touring on his own simple closed path through the museum.
To coordinate your operation, you use a map of the area which describes the museum as a set of corridors connecting corners, numbered from to . You start at corner , whereas your target —a valuable exhibit—is located at corner . Of course, one can reach any corner of the museum from any other corner, but you are not sure whether this is possible without getting noticed, that is, without ever being at the same corner as a watchman and without passing a watchman in a corridor.
Fortunately, you got your hands on the guard roster. So you know for each watchman where he is located at the beginning and which route he is taking. Each minute, a watchman moves from his current position to the next corner on his route, and you can either stay at your current position or move to an adjacent corner. You observed that no two of the watchmen’s routes intersect and that neither your starting position nor your target is contained in any of them.
Write a program that uses this information to either calculate the minimum amount of time in minutes you need to safely reach your target without being noticed or to decide that this is impossible.
Input
The first line of input contains the integers and described above. The following lines each contain two integers and (), meaning that there is a corridor directly connecting corners and . It is guaranteed that at most one corridor directly connects any two given corners.
The next line contains the single integer described above. Then lines follow;the th of these lines contains a sequence of integers, describing the route of the th watchman as follows: The first integer gives the number of distinct corners on the route. Then pairwise distinct integers specify the sequence of corners the watchman passes on his route. More precisely, the watchman starts at corner , in one minute he will be at , and so on; after minutes he will be at again.
Output
Your program should output a single line. This should consist of an integer, the minimum amount of time in minutes you need to safely reach your target, or the string impossible
if there is no way to achieve this.
6 6
1 2
2 3
3 4
4 5
5 2
5 6
1
4 3 2 5 4
4
6 6
1 2
2 3
3 4
4 5
5 2
5 6
1
4 4 5 2 3
5
11 13
1 2
2 3
3 4
4 2
3 5
5 6
6 7
7 5
6 8
8 9
9 10
10 8
9 11
3
3 4 2 3
3 7 6 5
3 10 8 9
impossible
Limits And Hints
We always have , , , .
- Subtask 1(5 points):, , .
- Subtask 2(10 points):, and no corridor connects the routes of two distinct watchmen.
- Subtask 3(10 points):, and no corridor connects the routes of two distinct watchmen.
- Subtask 4(10 points):No corridor connects the routes of two distinct watchmen.
- Subtask 5(25 points):.
- Subtask 6(20 points):,.
- Subtask 7(20 points):No further constraints.