#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 with color on the tree. Then there are operations of two types coming in order:
- : Add a new vertex indexed with color to the tree, where is the current number of existing vertices. An edge connecting vertex and with length will also be added to the tree.
- : Change the color of vertex to .
After each operation, you should find a pair of vertices and () with colors in the current tree so that the distance between and is as large as possible.
The distance between two vertices and is the length of the shortest path from to on the tree.
输入格式
There are multiple test cases. The first line of the input contains an integer indicating the number of test cases. For each test case:
The first line of the input contains two integers and (, ) indicating the number of operations and the initial color of vertex .
For the following lines, each line describes an operation taking place in order with or integers.
- If the -th line contains integers , , and (, , ), the -th operation will add a new vertex with color to the tree and connect it to vertex with an edge of length .
- If the -th line contains integers , and (, ), the -th operation will change the color of vertex to .
It's guaranteed that the sum of of all test cases will not exceed .
输出格式
For each operation output the maximum distance between two vertices with different colors. If no valid pair exists output instead.
题目大意
【题目描述】
你的任务是维护一棵有色树并处理查询。
一开始,树上只有一个编号为 的顶点,颜色为 。然后按顺序进行 个操作,有两种类型:
- :向树中添加一个颜色为 的新顶点,其编号为 ,其中 是当前存在的顶点数。同时,添加一条连接顶点 和 的长度为 的边。
- :将顶点 的颜色更改为 。
在每次操作之后,你应该找到当前树中颜色 的两个顶点 和 (),使得它们之间的距离尽可能大。
两个顶点 和 之间的距离是树上从 到 的最短路径的长度。
【输入格式】
输入的第一行包含两个整数 和 (,),表示操作的数量和顶点 的初始颜色。
接下来的 行中,每行描述一个按顺序进行的操作,包含 或 个整数。
- 如果第 行包含 个整数 、、 和 (,,),则第 个操作将向树中添加一个颜色为 的新顶点 ,并将其与顶点 连接,边的长度为 。
- 如果第 行包含 个整数 、 和 (,),则第 个操作将顶点 的颜色更改为 。
保证所有测试用例中 的总和不超过 。
【输出格式】
对于每个操作,输出两个不同颜色顶点之间的最大距离。如果不存在有效的顶点对,则输出 。
翻译来自于: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