QOJ.ac

QOJ

時間限制: 1 s 記憶體限制: 512 MB 總分: 100

#7397. Random Walk

统计

Given a tree with $n$ nodes, you start at node $x$. In each step, you choose one of the edges adjacent to your current node with equal probability and move to the adjacent node.

There are $Q$ queries. Each query provides a set $S$. Calculate the expected number of steps to visit all nodes in the set $S$ starting from $x$.

Specifically, node $x$ (the starting point) is considered to have been visited once at the beginning.

The answer should be taken modulo $998244353$.

Input

The first line contains three positive integers $n, Q, x$.

The next $n-1$ lines each contain two positive integers $(u, v)$ describing a tree edge.

The next $Q$ lines each start with an integer $k$, representing the size of the set, followed by $k$ distinct integers representing the set $S$.

Output

Output $Q$ lines, each containing a non-negative integer representing the answer.

Examples

Input 1

3 5 1
1 2
2 3
1 1
1 3
2 2 3
3 1 2 3
2 1 2

Output 1

0
4
4
4
1

Note

The tree in the example is a chain of length $3$, and the starting point is one end of the chain, so the answer only depends on the farthest node.

When the farthest node is $2$, it clearly takes $1$ step.

When the farthest node is $3$, it can be calculated that the expected number of steps to reach it is $4$.

Subtasks

For $20\%$ of the data, $1 \leq n, Q \leq 5$.

Another $10\%$ of the data satisfies the condition that the given tree is a chain.

Another $10\%$ of the data satisfies the condition that $k=1$ for all queries.

Another $30\%$ of the data satisfies $1 \leq n \leq 10$ and $Q=1$.

For $100\%$ of the data, $1 \leq n \leq 18$, $1 \leq Q \leq 5000$, and $1 \leq k \leq n$.

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
#832EditorialOpen题解alpha10222026-01-28 02:11:34View

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.