#P8124. [BalticOI 2021] From Hacks to Snitches (Day1)

[BalticOI 2021] From Hacks to Snitches (Day1)

Description

You are given an undirected graph with NN vertices and MM edges. There are KK guards. Guard ii passes through i\ell_i vertices, which are v1,v2,,viv_1, v_2, \cdots, v_{\ell_i}. The movement rule is as follows: the guard starts at vertex v1v_1 at minute 00. At minute 11, it moves from v1v_1 to v2v_2; at minute 22, it moves from v2v_2 to v3v_3; \cdots; at minute i1\ell_i - 1, it moves from vi1v_{\ell_i - 1} to viv_{\ell_i}; at minute i\ell_i, it moves from viv_{\ell_i} back to v1v_1. Then it repeats this cycle forever.

You are a thief. You want to go from vertex 11 to vertex NN. That is, at minute 00 you are at vertex 11. You may move directly from one vertex to another, but you must traverse a path between these two vertices. You must ensure that there is no guard on any vertex along this path, and also that no guard traverses any edge that belongs to this path. Traversing each edge takes you exactly one minute. It is guaranteed that the routes of any two guards are vertex-disjoint, and that both the start and the end vertices are not on any guard route.

You need to find the minimum number of minutes to reach vertex NN without being detected by the guards, or report that it is impossible.

Input Format

The first line contains two integers N,MN, M, the number of vertices and edges.

The next MM lines each contain two integers u,vu, v, describing an edge.

Line M+2M + 2 contains an integer KK, the number of guards.

The next KK lines each start with an integer i\ell_i, the length of the guard's route, followed by i\ell_i integers v1,v2,,viv_1, v_2, \cdots, v_{\ell_i} describing the route.

Output Format

Output one line containing one integer, the minimum number of minutes required, or output impossible if there is no solution.

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

Hint

Sample 1 Explanation

The route of guard 11 is as follows:

One feasible way is:

  • At minute 00, start at vertex 11.
  • At minute 11, wait at vertex 11.
  • At minute 22, move to vertex 22.
  • At minute 33, move to vertex 55.
  • At minute 44, move to vertex 66.

Sample 2 Explanation

The graph and routes are the same as in sample 1, but the start and end vertices are different.

One feasible way is: do not wait, and go directly along 1234561 \to 2 \to 3 \to 4 \to 5 \to 6.

Constraints

This problem uses bundled testdata.

  • Subtask 1 (5 pts): N,M105N, M \le 10^5, K=1K = 1, 1125\ell_1 \le 125.
  • Subtask 2 (10 pts): N,M105N, M \le 10^5, i125\sum \ell_i \le 125, and satisfies Property A.
  • Subtask 3 (10 pts): i200\ell_i \le 200, i350\sum \ell_i \le 350, and satisfies Property A.
  • Subtask 4 (10 pts): satisfies Property A.
  • Subtask 5 (25 pts): i125\sum \ell_i \le 125.
  • Subtask 6 (20 pts): i200\ell_i \le 200, i350\sum \ell_i \le 350.
  • Subtask 7 (20 pts): no special restrictions.
  • Subtask Ex (0 pts): Extra Subtask.

For 100%100\% of the testdata, 1N2.5×1051 \le N \le 2.5 \times 10^5, 1M3×1061 \le M \le 3 \times 10^6, 3i15003 \le \ell_i \le 1500, i2750\sum \ell_i \le 2750.

Property A means that there is no edge connecting any two guard routes.

Notes

Translated from BalticOI 2021 Day1 C From Hacks to Snitches

Translated by ChatGPT 5