luogu#P9665. [ICPC2021 Macao R] Colorful Tree

[ICPC2021 Macao R] Colorful Tree

题目描述

Your task is to maintain a colorful tree and process queries.

At the beginning, there is only one vertex numbered 11 with color CC on the tree. Then there are qq operations of two types coming in order:

  • 00 xx cc dd: Add a new vertex indexed (n+1)(n+1) with color cc to the tree, where nn is the current number of existing vertices. An edge connecting vertex xx and (n+1)(n+1) with length dd will also be added to the tree.
  • 11 xx cc: Change the color of vertex xx to cc.

After each operation, you should find a pair of vertices uu and vv (1u,vn1 \le u, v \le n) with different\textbf{different} colors in the current tree so that the distance between uu and vv is as large as possible.

The distance between two vertices uu and vv is the length of the shortest path from uu to vv on the tree.

输入格式

There are multiple test cases. The first line of the input contains an integer TT indicating the number of test cases. For each test case:

The first line of the input contains two integers qq and CC (1q5×1051 \le q \le 5 \times 10^5, 1Cq1 \le C \le q) indicating the number of operations and the initial color of vertex 11.

For the following qq lines, each line describes an operation taking place in order with 33 or 44 integers.

  • If the ii-th line contains 44 integers 00, xix_i, cic_i and did_i (1xin1 \le x_i \le n, 1ciq1 \le c_i \le q, 1d1091 \le d \le 10^9), the ii-th operation will add a new vertex (n+1)(n + 1) with color cic_i to the tree and connect it to vertex xix_i with an edge of length did_i.
  • If the ii-th line contains 33 integers 11, xix_i and cic_i (1xin1 \le x_i \le n, 1ciq1 \le c_i \le q), the ii-th operation will change the color of vertex xix_i to cic_i.

It's guaranteed that the sum of qq of all test cases will not exceed 5×1055 \times 10^5.

输出格式

For each operation output the maximum distance between two vertices with different colors. If no valid pair exists output 00 instead.

题目大意

【题目描述】

你的任务是维护一棵有色树并处理查询。

一开始,树上只有一个编号为 11 的顶点,颜色为 CC。然后按顺序进行 qq 个操作,有两种类型:

  • 00 xx cc dd:向树中添加一个颜色为 cc 的新顶点,其编号为 (n+1)(n+1),其中 nn 是当前存在的顶点数。同时,添加一条连接顶点 xx(n+1)(n+1) 的长度为 dd 的边。
  • 11 xx cc:将顶点 xx 的颜色更改为 cc

在每次操作之后,你应该找到当前树中颜色 不同\textbf{不同} 的两个顶点 uuvv1u,vn1 \le u, v \le n),使得它们之间的距离尽可能大。

两个顶点 uuvv 之间的距离是树上从 uuvv 的最短路径的长度。

【输入格式】

输入的第一行包含两个整数 qqCC1q5×1051 \le q \le 5 \times 10^51Cq1 \le C \le q),表示操作的数量和顶点 11 的初始颜色。

接下来的 qq 行中,每行描述一个按顺序进行的操作,包含 3344 个整数。

  • 如果第 ii 行包含 44 个整数 00xix_icic_idid_i1xin1 \le x_i \le n1ciq1 \le c_i \le q1d1091 \le d \le 10^9),则第 ii 个操作将向树中添加一个颜色为 cic_i 的新顶点 (n+1)(n + 1),并将其与顶点 xix_i 连接,边的长度为 did_i
  • 如果第 ii 行包含 33 个整数 11xix_icic_i1xin1 \le x_i \le n1ciq1 \le c_i \le q),则第 ii 个操作将顶点 xix_i 的颜色更改为 cic_i

保证所有测试用例中 qq 的总和不超过 5×1055 \times 10^5

【输出格式】

对于每个操作,输出两个不同颜色顶点之间的最大距离。如果不存在有效的顶点对,则输出 00

翻译来自于:ChatGPT

2
1 1
0 1 1 1
5 1
0 1 1 1
0 1 2 1
0 3 3 1
1 4 1
1 3 1
0
0
2
3
2
0