#arc171c. C - Swap on Tree
C - Swap on Tree
Score: points
问题陈述
有一棵包含 个顶点的树,顶点编号为 到 。第 条边连接顶点 和 。 此外,还有 个编号为 到 的物品。最初,物品 放置在顶点 上。 你可以执行以下操作任意次数,可能为零次:
- 选择一条边。设顶点 和 是边的端点,并交换顶点 和 上的物品。然后,删除所选的边。
设顶点 上的物品为 。当你完成操作时,存在多少种不同的可能序列 ?请计算出结果对 取模后的值。
以上为大语言模型 kimi 翻译,仅供参考。
Problem Statement
There is a tree with vertices numbered to . The -th edge connects vertices and .
Additionally, there are pieces numbered to . Initially, piece is placed on vertex .
You can perform the following operation any number of times, possibly zero:
- Choose one edge. Let vertices and be the endpoints of the edge, and swap the pieces on vertices and . Then, delete the chosen edge.
Let be the piece on vertex . How many different possible sequences exist when you finish performing the operation? Find the count modulo .
Constraints
- The graph given in the input is a tree.
Input
The input is given from Standard Input in the following format:
Output
Print the number, modulo , of possible sequences .
Sample Input 1
3
1 2
2 3
Sample Output 1
5
For example, the sequence can be obtained by the following steps:
- Choose the first edge, swap the pieces on vertices and , and delete the edge. This results in .
- Finish operating.
Also, the sequence can be obtained by the following steps:
- Choose the second edge, swap the pieces on vertices and , and delete the edge. This results in .
- Choose the first edge, swap the pieces on vertices and , and delete the edge. This results in .
- Finish operating.
The operation can yield the following five sequences:
Sample Input 2
5
2 5
3 4
1 3
1 5
Sample Output 2
34
Sample Input 3
8
4 5
2 5
3 6
1 3
1 8
2 7
2 8
Sample Output 3
799