#P4102. [HEOI2014] 林中路径

[HEOI2014] 林中路径

Description

Alice and Marisa both live in a huge forest consisting of NN vertices and MM directed edges. Marisa loves visiting Alice, but Alice does not like this reckless guest. Alice is very observant, so whenever she notices that Marisa has taken a path to her home that is exactly the same as a previous one, she pretends not to be at home, and Marisa has to go back disappointed. Marisa has discovered this secret, so she decides to take a different path to Alice’s home each time.

Marisa has limited stamina. She does not want to walk more than KK edges to reach Alice’s home, and each time she reaches Alice’s home after walking tt edges (where tKt \le K), she must spend t2t^2 stamina. Marisa wants to know how much total stamina she will spend before Alice refuses to see her no matter what (that is, when Marisa can no longer find a path of length at most KK that is different from all the previous ones).

Now Marisa has a map of the forest, but because she is very clumsy, she does not know which vertices are her home and Alice’s home. Therefore, she will ask you QQ times; in each query, you are asked to determine the answer if Marisa’s home is at SS and Alice’s home is at TT.

A path AA of length PP (where PP can be any nonnegative integer) is defined as a sequence of edges Am0,Am1,Am2,,AmP1A_{m_0}, A_{m_1}, A_{m_2}, \dots, A_{m_{P-1}}, such that for any 1i<P1 \le i < P, the ending vertex of Ami1A_{m_{i-1}} is the starting vertex of AmiA_{m_i}.

Two paths AA and BB are exactly the same if both have length PP and, for any 0i<P0 \le i < P, Ami=BmiA_{m_i} = B_{m_i}.

Input Format

The first line contains four integers NN, MM, KK, QQ, as described above.

The next MM lines each contain two integers XX, YY, indicating a directed edge from vertex XX to vertex YY. Vertices are numbered 1,2,3,,N1, 2, 3, \dots, N. The edge on the (i+1)(i+1)-th line has index ii. The following QQ lines each contain two integers SS and TT, as described above.

Output Format

For each query, output one line with the answer. Since Marisa cannot accept very large numbers, you only need to output the answer modulo 109+710^9+7.

2 0 1 1 
1 2
0
2 2 2 1 
1 2 
2 1 
1 1
4
2 2 100 1 
1 2 
2 1 
1 2
166650
2 3 100 2 
1 2 
1 2 
2 1 
2 2 
2 1
632506153
518794755

Hint

For 20%20\% of the testdata, N5N \le 5, M10M \le 10, K3K \le 3, Q10Q \le 10.

For 40%40\% of the testdata, N30N \le 30, M900M \le 900, K100K \le 100, Q100Q \le 100.

For 60%60\% of the testdata, N50N \le 50, M2,500M \le 2,500, K10,000,000K \le 10,000,000, Q1,000Q \le 1,000.

For 100%100\% of the testdata, 2N1002 \le N \le 100, 0M100,0000 \le M \le 100,000, 0K1,000,000,0000 \le K \le 1,000,000,000, 0Q10,0000 \le Q \le 10,000, 1X,Y,S,TN1 \le X, Y, S, T \le N.

g++ paths.cpp –o paths –Wl,--stack=268435456 –O2 

Explanation for Sample 1: There is no path from SS to TT.

Explanation for Sample 2: Marisa may pass through a vertex multiple times, even if that vertex is Alice’s home or Marisa’s home.

Translated by ChatGPT 5