#P7498. 磁控法则
磁控法则
Description
The cave Norton is in can be viewed as a tree formed by chambers and passages. Norton is in chamber , and the treasure is in chamber . In the treasure chamber there is a special magnet with a powerful magnetic field, and Norton also carries a small magnet made of the same material. The magnet in the treasure chamber has only one pole (N or S), and its pole never changes. Norton’s magnet also has only one pole (N or S), but at the start of each moment it switches poles with probability (N becomes S, S becomes N).
At each moment, Norton’s movement is determined by the pole of the treasure-chamber magnet and the pole of Norton’s magnet:
-
The two magnets have different poles. In this case, the magnets trigger an “attraction” effect. Norton will move, due to the attractive force, to the chamber adjacent to whose distance to is (that is, the parent of when rooting the tree at ).
-
The two magnets have the same pole. In this case, the magnets trigger a “bounce” effect. Norton will, due to the repulsive force, move uniformly at random to one of the chambers adjacent to whose distance to is (that is, any child of when rooting the tree at ). If there is no such chamber, a “stun” effect is triggered: in the next moment, Norton will not make any move, and will not perform any pole switching either.
After some investigation, Norton already knows the entire cave structure and the pole-switching probability . To search for the treasure more effectively, each time he asks you a query: you need to answer, if initially Norton is in chamber with his magnet pole being , and the treasure is in chamber with the chamber magnet pole being , what is the expected number of moments until Norton can find the treasure.
Input Format
The first line contains three integers , representing the number of chambers, the number of queries, and the pole-switching probability. The pole-switching probability is given as a probability modulo .
The next lines each contain two positive integers , indicating that there is a passage between chambers and . It is guaranteed that the final structure is a tree.
The next lines each contain, in order, a positive integer , a character , a positive integer , and a character , indicating one query, with meanings as above. It is guaranteed that are N or S.
Output Format
Output lines. Each line contains one non-negative integer, representing the query answer modulo .
6 4 499122177
1 2
1 3
5 3
4 6
4 3
2 N 1 S
3 S 1 S
5 N 6 N
1 S 4 N
3
6
17
11
10 6 199648871
3 7
4 9
2 3
5 6
7 10
5 7
5 9
8 2
1 3
10 S 5 S
1 N 7 S
1 N 4 N
1 S 4 N
4 N 3 S
7 N 4 N
332748127
8
665496262
665496261
665496253
831870314
Hint
Explanation of Sample 1
The pole-switching probability at each moment is .
The cave structure is as follows:

For query 1, at moment Norton has probability to move to , and probability to trigger “stun” and enter moment . At moment , similarly, he has probability to move to , and probability to trigger “stun” and enter moment ... The expected value is $1\times\dfrac{1}{2}+3\times\left(\dfrac{1}{2}\right)^2+5\times\left(\dfrac{1}{2}\right)^3+\ldots=3$.
Constraints
This problem uses bundled testdata.
- Subtask 1 ( ): .
- Subtask 2 ( ): .
- Subtask 3 ( ): For all queries, it is guaranteed that .
- Subtask 4 ( ): No special restrictions.
For all testdata, $2\leq n\leq5\times 10^5,1\leq q\leq5\times10^5,1\leq u,v,x,y\leq n,x\neq y,2\leq p\leq998244352$.
The input size of this problem is large. Please use a fast input method.
Translated by ChatGPT 5
京公网安备 11011102002149号