#P15584. [KTSC 2026] 网格树 / Grid Tree

    ID: 17423 远端评测题 3000ms 1024MiB 尝试: 0 已通过: 0 显示难度NOI/NOI+/CTS 上传者: 标签>交互题Special Judge2026KTSC(韩国)

[KTSC 2026] 网格树 / Grid Tree

Problem Description

You are given a rooted tree with NN nodes, numbered 0∼N−10 \sim N-1. Node 00 is the root. Each node has either 00 or 22 children. For a node with exactly two children, its left child and right child are distinguished. Each tree edge ee has a positive integer length cec_e.

Draw the tree on a 2D Cartesian coordinate system. Each node vv is drawn at a distinct grid point mv=(xv,yv)m_v=(x_v,y_v). Here, the root must be drawn at m0=(0,0)m_0=(0,0). A grid point is a point whose xx and yy coordinates are both integers.

A tree edge e=(p,v)e=(p,v) connecting vv and its parent pp is drawn as a path connecting mpm_p and mvm_v in the coordinate system. The path must satisfy all of the following conditions:

  • While moving along the path from mpm_p to mvm_v, the moving direction must always be the positive direction of the xx axis or the positive direction of the yy axis. This means that a path moving in a direction where both xx and yy increase is not allowed. Also, the direction can only change at grid points. This means that if a path has length kk, the direction can change at only (k−1)(k-1) grid points.
  • If vv is the left child of pp, the initial direction of the path from mpm_p must be along the positive xx axis.
  • If vv is the right child of pp, the initial direction of the path from mpm_p must be along the positive yy axis.
  • The path length is at least the edge length cec_e.
  • 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 vv as L(v)=xv+yvL(v)=x_v+y_v. 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)
  • NN: the number of nodes.
  • P,C,DP,C,D: integer arrays of size N−1N-1. For any 1≤i≤N−11\le i\le N-1, the parent of node ii is P[i−1]P[i-1]. Let ee be the edge connecting ii and its parent, then ce=C[i−1]c_e=C[i-1]. If D[i−1]=0D[i-1]=0, node ii is a left child; otherwise, if D[i−1]=1D[i-1]=1, node ii 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 11: NN.
  • For all 0≤i<N−10 \leq i < N - 1:
    • Line 2+i2 + i: P[i]P[i] C[i]C[i] D[i]D[i].

Output Format

The sample grader prints the answer in the following format:

  • Line 11: 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 00 as the root.
  • Each node has 00 or 22 children.
  • 3≤N≤200 0003\le N\le 200\, 000.
  • For any 0≤i≤N−20\le i\le N-2, 0≤P[i]≤N−10\le P[i]\le N-1.
  • For any 0≤i≤N−20\le i\le N-2, 1≤C[i]≤1091\le C[i]\le 10^9.
  • For any 0≤i≤N−20\le i\le N-2, 0≤D[i]≤10\le D[i]\le 1.

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 00 children.

ID Score Constraints
11 1010 N≤7N\le 7
22 8 8 For any node vv with two children, one of the children of vv is a leaf.
33 2121 N≤5 000N\le 5\,000, the distances from all leaves to node 00 are all KK (≤2500\le 2500).
44 2929 N≤5 000N\le 5\, 000, the distance from any node to node 00 is at most 25002500.
55 3232 No additional constraints.

Scoring

For subtask 33, if no drawing with grid depth exactly KK exists and compute_min_depth returns −1-1, then the test will be judged correct. More precisely:

  • For testdata where a drawing with grid depth exactly KK exists:
    • If it returns KK, you get full score.
    • Otherwise, you get 00 points.
  • For testdata where no drawing with grid depth exactly KK exists:
    • If it returns the minimum grid depth, you get full score.
    • If it returns −1-1, you get full score.
    • Otherwise, you get 00 points.

Note: In subtask 33, the distances from all leaves to node 00 are equal and are KK.

Samples

Sample 11

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 22, as shown in the figure below.

::::align{center} ::::

It can be proven that no drawing with grid depth less than 22 exists.

Therefore, this function should return 22.

Sample 22

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 44, as shown in the figure below. ::::align{center} ::::

Therefore, this function should return 44.

Translated by ChatGPT 5