K - Gas Station Editorial /

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 66

問題文

高橋王国は NN 個の街と MM 本の道路からなります。

街には 1,2,,N1,2,\ldots,N の番号がついており、i(1iM)i\,(1 \leq i \leq M) 本目の道路は街 uiu_i と街 viv_i を双方向に結んでいます。どの街からどの街へもいくつかの道路をたどることで到達できることが保証されます。

また、街 a1,a2,,aKa_1,a_2,\ldots,a_K にはガソリンスタンドがあります。

QQ 個のクエリに答えてください。i(1iQ)i\,(1 \leq i \leq Q) 番目のクエリは以下の内容です。

  • sis_i を出発し、ガソリンスタンドのある街を 11 つ以上通った後、街 tit_i に行くとき、道を通る回数の最小値を求めよ。

制約

  • 2N2×1052 \leq N \leq 2 \times 10^5
  • 1M,Q2×1051 \leq M,Q \leq 2 \times 10^5
  • 1Kmin(N1,20)1 \leq K \leq \min(N-1,20)
  • 1aiN1 \leq a_i \leq N
  • aiaj(ij)a_i \neq a_j\,(i \neq j)
  • 1ui<viN1 \leq u_i < v_i \leq N
  • (ui,vi)(uj,vj)(ij)(u_i,v_i) \neq (u_j,v_j) \,(i \neq j)
  • 1si,tiN1 \leq s_i , t_i \leq N
  • si,tis_i,t_i にはガソリンスタンドがない
  • どの街からどの街へもいくつかの道路をたどることで到達できる
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。

NN MM QQ KK
a1a_1 a2a_2 \ldots aKa_K
u1u_1 v1v_1
u2u_2 v2v_2
\vdots
uMu_M vMv_M
s1s_1 t1t_1
s2s_2 t2t_2
\vdots
sQs_Q tQt_Q

出力

QQ 行出力せよ。i(1iQ)i\,(1 \leq i \leq Q) 行目には、ii 番目のクエリに対する答えを出力せよ。


入力例 1Copy

Copy
3 2 1 1
1
1 2
2 3
3 2

出力例 1Copy

Copy
3

33 を出発し、街 22、街 11、街 22 の順に移動すると移動回数は 33 回です。これより少ない移動回数は実現できないので 33 を出力します。


入力例 2Copy

Copy
5 6 2 2
3 1
1 2
2 3
2 4
1 5
2 5
3 5
5 5
5 4

出力例 2Copy

Copy
2
3

Score : 66 points

Problem Statement

The Kingdom of Takahashi has NN towns and MM roads.

The towns are numbered 1,2,,N1,2,\ldots,N, and the ii-th road (1iM)(1 \leq i \leq M) connects Town uiu_i and Town viv_i bidirectionally. It is guaranteed that one can get from every town to every town by traversing some roads.

Additionally, there are gas stations in Towns a1,a2,,aKa_1,a_2,\ldots,a_K.

Answer QQ queries. The ii-th query (1iQ)(1 \leq i \leq Q) is as follows.

  • Find the minimum number of times one needs to traverse a road when getting from Town sis_i to Town tit_i via a town with a gas station.

Constraints

  • 2N2×1052 \leq N \leq 2 \times 10^5
  • 1M,Q2×1051 \leq M,Q \leq 2 \times 10^5
  • 1Kmin(N1,20)1 \leq K \leq \min(N-1,20)
  • 1aiN1 \leq a_i \leq N
  • aiaj(ij)a_i \neq a_j\,(i \neq j)
  • 1ui<viN1 \leq u_i < v_i \leq N
  • (ui,vi)(uj,vj)(ij)(u_i,v_i) \neq (u_j,v_j) \,(i \neq j)
  • 1si,tiN1 \leq s_i , t_i \leq N
  • Nither sis_i nor tit_i has a gas station.
  • It is possible to get from every town to every town by traversing some roads.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN MM QQ KK
a1a_1 a2a_2 \ldots aKa_K
u1u_1 v1v_1
u2u_2 v2v_2
\vdots
uMu_M vMv_M
s1s_1 t1t_1
s2s_2 t2t_2
\vdots
sQs_Q tQt_Q

Output

Print QQ lines. The ii-th line (1iQ)(1 \leq i \leq Q) should contain the answer to the ii-th query.


Sample Input 1Copy

Copy
3 2 1 1
1
1 2
2 3
3 2

Sample Output 1Copy

Copy
3

After starting in Town 33, we can go to Town 22, Town 11, Town 22 in this order to complete the trip with three moves. It is impossible to do it with fewer moves, so we print 33.


Sample Input 2Copy

Copy
5 6 2 2
3 1
1 2
2 3
2 4
1 5
2 5
3 5
5 5
5 4

Sample Output 2Copy

Copy
2
3


2025-04-07 (Mon)
20:34:10 +00:00