#P3443. [POI 2006] LIS-The Postman

[POI 2006] LIS-The Postman

Description

Every morning, a busy postman needs to traverse all the city’s streets to deliver mail. All roads in the city are one-way and connected by intersections. For any two intersections u,vu, v, there are at most two streets directly between them: one uvu \to v and one vuv \to u (i.e., there are no two uvu \to v streets). All intersections are numbered from 11 to nn.

At intersection 11, the postman may start his trip or end his trip. For a long time, the postman could freely choose his route, but a new regulation has disrupted his plan: each postman has been given several sequences of intersections, and now his route must satisfy the following requirements:

  • The route must start at intersection 11 and end at intersection 11.
  • The route must traverse each street exactly once.
  • Each prescribed sequence of intersections must appear contiguously in the route. For example: the sequence 1 2 1 appears in 1 2 1 3, but does not appear in 1 2 3 1 (not contiguous).

The postman asks you to determine whether there exists a route that satisfies the above conditions; if so, also provide one such route.

Input Format

The first line contains two integers n,mn, m, the number of intersections and the number of streets.

The next mm lines each contain two integers a,ba, b, indicating there is a street aba \to b. It is guaranteed that identical streets are not repeated and there are no self-loops.

The next line contains an integer tt, the number of prescribed intersection sequences.

The next tt lines each describe one prescribed sequence: the first integer is kk, followed by kk integers giving the sequence.

Output Format

If there exists a route that satisfies the conditions, output TAK; otherwise, output NIE.

If the answer is TAK, then output the route in the following lines, one integer per line.

Suppose you output m+1m+1 integers, where the ii-th printed integer is viv_i. Your output must satisfy the following conditions:

  • v1=vm+1=1v_1 = v_{m+1} = 1.
  • For all 1im1 \leq i \leq m, there exists a street from viv_i to vi+1v_{i+1}.
  • Each street in the city is used exactly once.
  • Each prescribed intersection sequence appears contiguously in the route.
6 10
1 5
1 3
4 1
6 4
3 6
3 4
4 3
5 6
6 2
2 1
4
3 1 5 6
3 3 4 3
4 4 3 6 4
3 5 6 2
TAK
1
3
4
3
6
4
1
5
6
2
1

Hint

Constraints: 2n5×1042 \leq n \leq 5 \times 10^4, 1m2×1051 \leq m \leq 2 \times 10^5, 1a,bn1 \leq a, b \leq n, aba \neq b, 0t1040 \leq t \leq 10^4, 2k1052 \leq k \leq 10^5, k105\sum k \leq 10^5.

Translated by ChatGPT 5