#P4321. 随机漫游

    ID: 3186 远端评测题 2000~4000ms 500MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>动态规划,dp图论O2优化期望高斯消元

随机漫游

Description

Country H has NN cities.

In the next MM days, Xiao c will go to look for Xiao w, but Xiao c does not know the exact location of Xiao w, so Xiao c decides to randomly choose one road to walk each time until meeting Xiao w.

Xiao c knows that Xiao w can only be in one of the nn cities c1,c2..cnc_1, c_2.. c_n. Xiao c wants to know, in the worst case, the expected number of roads Xiao c needs to traverse before meeting Xiao w.

All edges in country H are undirected. Between any two cities, there is at most one road directly connecting them, and no road connects the same city to itself.

At any time, in country H there do not exist cities uu and vv such that vv cannot be reached from uu.

Input Format

The first line contains two positive integers N,EN, E, representing the number of cities and the number of edges in country H.

Lines 2 to E+1E+1 each contain two positive integers u,vu, v, indicating there is a road between city uu and city vv.

Line E+2E+2 contains one positive integer MM.

Lines E+3E+3 to E+M+2E+M+2 each contain n+2n+2 positive integers: the first integer is nn, followed by nn distinct integers cic_i, and the last integer ss indicates the city where Xiao c is located.

Output Format

Output MM lines, each containing a single integer rr representing the answer.

If the expected value you compute is qp\frac{q}{p}, where p,qp, q are coprime, then you should output an rr such that r×pq(mod 998244353)r\times p \equiv q(\mathrm{mod}\ 998244353) and 0r<9982443530\leq r < 998244353. It can be proved that such an rr is unique.

3 2
1 2
2 3
3
2 1 2 1		
3 1 2 3 1
1 3 1
1
4
4

Hint

The roads of country H form a chain, so in the worst case Xiao w is at the deepest node (taking Xiao c’s city as the root).

For the first day, Xiao c is in city 1, the deepest node is 2, city 1 can only reach city 2, and the expected number of roads to reach it is 1.

For the second day, Xiao c is in city 1, the deepest node is 3, and the computed expectation is 4 roads to reach it.

The third day is the same as the second day.

The worst case means going through all nn possible cities at least once.

Subtask 1: 10 points, N=4,M=12N = 4, M = 12.

Subtask 2: 15 points, N=10,M=100000N =10, M = 100000.

Subtask 3: 15 points, N=18,M=1N = 18, M = 1.

Subtask 4: 10 points, N=18,M=99995N = 18, M = 99995, the graph is a chain.

Subtask 5: 10 points, N=18,M=99996N = 18, M = 99996, all ss are the same.

Subtask 6: 15 points, N=18,M=99997N = 18, M = 99997, E=N1E = N-1.

Subtask 7: 15 points, N=18,M=99998N = 18, M = 99998, all ss are the same.

Subtask 8: 10 points, N=18,M=99999N = 18, M = 99999.

Constraints for all testdata: $1\leq N\leq 18, 1\leq M\leq 100000, 1\leq E\leq \frac{N(N-1)}{2}$.

Translated by ChatGPT 5