#P15584. [KTSC 2026] 网格树 / Grid Tree
[KTSC 2026] 网格树 / Grid Tree
Problem Description
You are given a rooted tree with nodes, numbered . Node is the root. Each node has either or children. For a node with exactly two children, its left child and right child are distinguished. Each tree edge has a positive integer length .
Draw the tree on a 2D Cartesian coordinate system. Each node is drawn at a distinct grid point . Here, the root must be drawn at . A grid point is a point whose and coordinates are both integers.
A tree edge connecting and its parent is drawn as a path connecting and in the coordinate system. The path must satisfy all of the following conditions:
- While moving along the path from to , the moving direction must always be the positive direction of the axis or the positive direction of the axis. This means that a path moving in a direction where both and increase is not allowed. Also, the direction can only change at grid points. This means that if a path has length , the direction can change at only grid points.
- If is the left child of , the initial direction of the path from must be along the positive axis.
- If is the right child of , the initial direction of the path from must be along the positive axis.
- The path length is at least the edge length .
- Paths must not intersect. In other words, an interior point of one path (that is, all points except its endpoints) cannot lie on another path.
Define the coordinate depth of a node as . In the drawn tree, all leaves (nodes with no children) must have the same coordinate depth. Define this depth of leaves as the grid depth.
Among all valid drawings, find the minimum possible value of the grid depth.
Implementation Details
This is a functional interactive problem. You do not need to, and must not, implement the main function.
You should implement the following function:
long long compute_min_depth(int N, vector<int> P, vector <int> C, vector<int> D)
- : the number of nodes.
- : integer arrays of size . For any , the parent of node is . Let be the edge connecting and its parent, then . If , node is a left child; otherwise, if , node is a right child.
- It can be proven that there exists a valid drawing. This function should return the minimum grid depth among valid drawings.
- This function is called exactly once.
Your source code must not call any input/output functions.
Input Format
The input format of the sample grader is as follows:
- Line : .
- For all :
- Line : .
Output Format
The sample grader prints the answer in the following format:
- Line : the return value of
compute_min_depth.
5
4 1 0
0 2 1
4 1 1
0 1 0
2
9
0 2 0
0 1 1
1 1 0
1 1 1
2 1 0
2 1 1
5 1 0
5 1 1
4
Hint
Constraints
- The given structure is a rooted tree with node as the root.
- Each node has or children.
- .
- For any , .
- For any , .
- For any , .
Subtasks
Define the distance between two nodes as the sum of edge weights on the unique simple path connecting them.
Define a leaf as a node with children.
| ID | Score | Constraints |
|---|---|---|
| For any node with two children, one of the children of is a leaf. | ||
| , the distances from all leaves to node are all (). | ||
| , the distance from any node to node is at most . | ||
| No additional constraints. |
Scoring
For subtask , if no drawing with grid depth exactly exists and compute_min_depth returns , then the test will be judged correct. More precisely:
- For testdata where a drawing with grid depth exactly exists:
- If it returns , you get full score.
- Otherwise, you get points.
- For testdata where no drawing with grid depth exactly exists:
- If it returns the minimum grid depth, you get full score.
- If it returns , you get full score.
- Otherwise, you get points.
Note: In subtask , the distances from all leaves to node are equal and are .
Samples
Sample
Consider the following call:
compute_min_depth(5, [4, 0, 4, 0], [1, 2, 1, 1], [0, 1, 1, 0])
- We can draw a tree with grid depth , as shown in the figure below.
::::align{center}
::::
It can be proven that no drawing with grid depth less than exists.
Therefore, this function should return .
Sample
Consider the following call:
compute_min_depth(9, [0, 0, 1, 1, 2, 2, 5, 5], [2, 1, 1, 1, 1, 1, 1, 1], [0, 1, 0, 1, 0, 1, 0, 1])
- We can draw a tree with grid depth , as shown in the figure below.
::::align{center}
::::
Therefore, this function should return .
Translated by ChatGPT 5