#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.

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