#abc329g. G - Delivery on Tree
G - Delivery on Tree
Score : points
问题描述
给定一棵包含 个顶点的二叉树。顶点编号从 到 ,其中顶点 是根节点。第 条边 双向连接顶点 和顶点 。
在这个树上有 个篮子和 个球。球的编号为 到 ,每个球 有一个指定的起点 和终点 。初始时,篮子为空并放置在顶点 处,球则放置在其各自的起点处。
你可以按任意顺序执行以下操作任意多次:
- 让 表示当前篮子所在的顶点,执行以下任一操作:
- 选择一条与顶点 相连的边,沿该边移动篮子至相邻顶点。篮子中的球随之一起移动。
- 选择一个起点为 并且仍在顶点 上的球,将其放入篮子中。此操作仅当篮子中球的数量少于 个(即篮子不能容纳 或更多的球)时才能执行。
- 从篮子中选择一个终点为 的球取出,并将其放在顶点 处。
如果最终篮子为空并且位于顶点 处,同时所有球都到达了它们各自的终点,则称这样的操作序列是一个有效操作序列。
由于频繁移动篮子很费力,篮子的移动路径应在返回顶点 之前恰好经过每条边两次。求满足存在遵循该路径的有效操作序列的路径数量模 的结果。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
You are given a binary tree with vertices. The vertices are numbered to , with vertex being the root. The -th edge connects vertex and vertex bidirectionally.
There are one basket and balls on this tree. The balls are numbered to , and each ball has a designated start vertex and goal vertex . Initially, the basket is empty and placed at vertex , and the balls are placed at their respective start vertices.
You can perform the following operation any number of times in any order.
- Let be the current vertex where the basket is placed. Do one of the following:
- Choose one edge connected to vertex , and move the basket along that edge to the adjacent vertex. The balls inside the basket move together with it.
- Choose a ball with the start vertex that is still placed at , and put it into the basket. This operation can only be performed if the basket contains fewer than balls (that is, the basket cannot contain or more balls).
- Choose a ball with the goal vertex from the basket and take it out, placing it at vertex .
A sequence of operations is called a good operation sequence if and only if the basket is eventually empty and placed at vertex , and the balls are eventually placed at their respective goal vertices.
Since it is tiring to move the basket many times, the basket's movement path should traverse each edge exactly twice before returning to vertex . Find the number, modulo , of those paths such that a good operation sequence exists following that path.
Constraints
- For every , there are at most two 's such that .
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
Print the number, modulo , of paths traversing every edge exactly twice before returning to vertex such that a good operation sequence exists following that path.
Sample Input 1
5 2 1
1 1 3 3
2 4
5 3
Sample Output 1
1
The given graph is shown in the figure below. The circles and squares written next to the vertices represent the start and goal vertices of the balls, respectively.
Among the paths where every edge is traversed exactly twice before returning to vertex , there is only one path such that a good operation sequence exists following that path, which is shown below:
Here, you can construct the following good operation sequence:
- Move the basket to vertex .
- Put ball into the basket.
- Move the basket to vertex .
- Move the basket to vertex .
- Move the basket to vertex .
- Take ball out of the basket and place it at vertex .
- Move the basket to vertex .
- Move the basket to vertex .
- Put ball into the basket.
- Move the basket to vertex .
- Take ball out of the basket and place it at vertex .
- Move the basket to vertex .
Sample Input 2
5 2 2
1 1 3 3
2 4
5 3
Sample Output 2
2
The value of has increased by from Sample Input 1. This allows you to construct a good operation sequence for the following path, in addition to the one illustrated above.
Sample Input 3
15 4 2
1 2 1 4 2 3 4 7 3 7 5 9 11 8
14 12
5 4
13 15
5 12
Sample Output 3
8
update @ 2024/3/10 01:55:10