DFS
Time limit8sMemory limit1024 MB
Given a rooted tree with vertex values, sum the expected minimum value on the random DFS stack over all legal pairs (x, y), modulo 998244353.
- Level
Hard9 of 10
- Topics
- Tree, Union-find, Probability, DFS
- Solved
- No attempts yet
Problem
You are given a rooted tree of vertices, and is the root of the tree. Each vertex has value .
Let us define the DFS procedure starting from to find :
- Push on the stack.
- Check , the top element of the stack. If , the procedure ends. Otherwise, if there is at least one son of which is not visited, choose one such son with equal probability and push it on the stack.
- Repeat step 2 until there is no unvisited son.
- Pop the top element from the stack.
- Repeat step 2 until the stack is empty.
The procedure is legal if and only if is in the subtree of .
Define as the expectation of the minimum value of all vertices that were pushed on the stack during the DFS procedure starting from to find .
Now we want to calculate for all legal pairs . The answer can be written as an irreducible fraction , where and are integers and . Output the integer equal to . In other words, output an integer such that and .
Input
The first line contains an integer (), the number of test cases.
For each test case, the first line contains two integers and (, ), the number of vertices and the root.
The next line contains integers, where the -th integer is (), the value of vertex .
Each of the next lines contains two integers and (), an edge of the tree.
It is guaranteed that and that the given graph is a tree.
Output
For each test case, print the answer on its own line.