The Most Beautiful Christmas Tree
Time limit2sMemory limit1024 MB
Count the ways to place n distinct ornaments so each child is more beautiful than its parent, for each queried root, modulo 998244353.
- Level
Medium6 of 10
- Topics
- Tree, Dynamic programming, Combinatorics
- Solved
- No attempts yet
Problem
Hyeona is preparing a Christmas tree and wants the tree she made to be the most beautiful in the world.
Hyeona's tree has vertices and edges, and it has a root . Hyeona also has ornaments, one for each vertex. Every ornament has a unique beauty value. There is a 1st most beautiful ornament, a 2nd most beautiful ornament, up to an -th most beautiful ornament. No two ornaments have the same beauty value.
The tree is called beautiful if the ornaments placed on the vertices satisfy the following condition.
- For every parent-child pair formed with as the root, the ornament on the child vertex is more beautiful (has a larger beauty value) than the ornament on the parent vertex.
Here, vertex is the parent of vertex if the two vertices are adjacent and the distance from to is smaller than the distance from to . The distance between two vertices and is the minimum number of edges on a path from to .
Find the number of ways to place ornaments on the vertices so that is beautiful, modulo . is a prime. Every vertex must receive an ornament. Changing the root of the tree, find this count times.
Input
The first line contains the number of vertices .
The next lines each contain two integers and , separated by a space, meaning that an edge connects vertices and .
The next line contains the number of queries .
Each of the next lines contains one integer . This is a query asking for the answer when the root of the tree is .
Output
For each query, print the answer. Separate consecutive answers with a newline.
Constraints
- ,
- The given graph is always a tree.