33OJ
登录 注册

#P17890. [ICPC 2026 Nanchang I] Tourist

    ID: 20626 远端评测题 1000ms 1024MiB 尝试: 0 已通过: 0 显示难度提高 上传者: 标签>数学矩阵运算江西ICPC2026省赛/邀请赛

[ICPC 2026 Nanchang I] Tourist

Problem Description

The Construction Kingdom is a famous tourist attraction, attracting thousands of tourists every year. The kingdom consists of nn towns and mm directed roads. Each road has a length of 11. It is guaranteed that among the initial mm roads, there are no duplicate roads and no road from a town to itself.

Later, due to the continuous prosperity of tourism, the Construction Kingdom decided to expand the roads. Starting from day 11, over the next kk days, a new directed road from xx to yy will be built each day. Due to improper planning, it may happen that an already existing road is built, or a road from a town to itself is built.

Fengmi is a free-spirited tourist. She has qq travel wishes and a desired distance ww. Each wish has a start ss and an end tt (sis_i and tit_i may be the same). She wants to know, for each wish, the earliest day on which she can start from town ss, and under the condition that she is allowed to pass through towns and roads multiple times, exactly travel a distance of ww to reach town tt, or report that it is impossible. Please help her solve this problem.

Input Format

The first line contains two integers nn and mm (2≤n≤2002 \le n \le 200, 1≤m≤n(n−1)1 \le m \le n(n-1)) --- the number of towns and the number of initial roads.

Each of the next mm lines contains two integers uiu_i and viv_i (1≤ui,vi≤n1 \le u_i, v_i \le n, ui≠viu_i \ne v_i), indicating a directed road from uiu_i to viv_i.

The next line contains an integer kk (1≤k≤n21 \le k \le n^2) --- the number of roads to be built in the following days.

Each of the next kk lines contains two integers xix_i and yiy_i (1≤xi,yi≤n1 \le x_i, y_i \le n), meaning that on day ii, a new directed road from xix_i to yiy_i is built.

The next line contains two integers qq and ww (1≤q≤n21 \le q \le n^2, 1≤w≤1091 \le w \le 10^9) --- the number of wishes and the desired distance.

Each of the next qq lines contains two integers sis_i and tit_i (1≤si,ti≤n1 \le s_i, t_i \le n) --- the start and end of the ii-th wish.

Output Format

For each wish, output a single integer --- the earliest day that the wish can be fulfilled (i.e., after the road built on that day, there exists a walk of length exactly ww from ss to tt). If it is never possible, output −1-1.

6 3
1 3
2 3
3 1
4
4 5
5 5
2 5
5 6
5 8
5 1
5 5
1 1
4 5
4 6
-1
2
0
2
4

Hint

In the sample, the desired distance is w=8w = 8.

  • For the first wish, it can be proved that it is impossible.
  • For the second wish, after the roads 4→54 \to 5 and 5→55 \to 5 are built, one can walk along the town sequence [5,5,5,5,5,5,5,5,5][5, 5, 5, 5, 5, 5, 5, 5, 5] to reach the destination.
  • For the third wish, initially one can walk along the town sequence [1,3,1,3,1,3,1,3,1][1, 3, 1, 3, 1, 3, 1, 3, 1] to the destination.
  • For the fourth wish, after the roads 4→54 \to 5, 5→55 \to 5 are built, one can walk along the town sequence [4,5,5,5,5,5,5,5,5][4, 5, 5, 5, 5, 5, 5, 5, 5] to the destination.
  • For the fifth wish, after the roads 4→54 \to 5, 5→55 \to 5, 2→52 \to 5, 5→65 \to 6 are built, one can walk along the town sequence [4,5,5,5,5,5,5,5,6][4, 5, 5, 5, 5, 5, 5, 5, 6] to the destination.