#P4102. [HEOI2014] 林中路径
[HEOI2014] 林中路径
Description
Alice and Marisa both live in a huge forest consisting of vertices and 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 edges to reach Alice’s home, and each time she reaches Alice’s home after walking edges (where ), she must spend 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 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 times; in each query, you are asked to determine the answer if Marisa’s home is at and Alice’s home is at .
A path of length (where can be any nonnegative integer) is defined as a sequence of edges , such that for any , the ending vertex of is the starting vertex of .
Two paths and are exactly the same if both have length and, for any , .
Input Format
The first line contains four integers , , , , as described above.
The next lines each contain two integers , , indicating a directed edge from vertex to vertex . Vertices are numbered . The edge on the -th line has index . The following lines each contain two integers and , 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 .
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 of the testdata, , , , .
For of the testdata, , , , .
For of the testdata, , , , .
For of the testdata, , , , , .
g++ paths.cpp –o paths –Wl,--stack=268435456 –O2
Explanation for Sample 1: There is no path from to .
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
京公网安备 11011102002149号