#P5643. Expected Steps to Cover a Set on a Tree

    ID: 18871 Type: Default 1000ms 256MiB

Expected Steps to Cover a Set on a Tree

Expected Steps to Cover a Set on a Tree

You are given a tree with n vertices and a starting vertex x. At each step, you move from your current vertex to one of its adjacent vertices, each chosen with equal probability. There are Q queries; in each query a set S of vertices is given. Your task is to compute the expected number of steps needed for a random walk (starting at x) to visit every vertex in S at least once. Note that if x is in S it is considered visited at time 0.

More formally, let the tree have n vertices and n-1 edges. At each step, if you are at vertex u having degree d_u, you choose one of its neighbors uniformly at random. For a query with set S, denote by f(x, S) the expected number of steps until all vertices in S have been visited at least once. It can be shown that
\[ f(x,S)\equiv E \pmod{998244353} \] where E is a rational number. Your answer should be given modulo 998244353.

Note: All formulas are represented in LaTeX format. For instance, the probability of moving from vertex u to any neighboring vertex is \(\frac{1}{d_u}\).

inputFormat

The first line contains three integers n, x and Q (1 ≤ x ≤ n), where n is the number of vertices and x is the starting vertex. The next n-1 lines each contain two integers u and v describing an edge of the tree.

Then Q queries follow. For each query, the first integer k denotes the size of the set S, followed by k distinct integers indicating the vertices in S.

outputFormat

For each query, output a single integer representing the expected number of steps to cover all vertices in S, taken modulo 998244353.

sample

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

4

</p>