Logo
(追記) (追記ここまで)

31692번 - Colourful Tree 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
6 초 1024 MB85571.429%

문제

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

At the beginning, there is only one vertex numbered 1ドル$ with colour $C$ on the tree. Then there are $q$ operations of two types coming in order:

  • 0ドル$ $x$ $c$ $d$: Add a new vertex indexed $(n + 1)$ with colour $c$ to the tree, where $n$ is the current number of existing vertices. An edge connecting vertex $x$ and $(n+ 1)$ with length $d$ will also be added to the tree.
  • 1ドル$ $x$ $c$: Change the colour of vertex $x$ to $c$.

After each operation, you should find a pair of vertices $u$ and $v$ (1ドル ≤ u, v ≤ n$) with different colours in the current tree so that the distance between $u$ and $v$ is as large as possible.

The distance between two vertices $u$ and $v$ is the length of the shortest path from $u$ to $v$ on the tree.

입력

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

The first line of the input contains two integers $q$ and $C$ (1ドル ≤ q ≤ 5 \times 10^5,ドル 1ドル ≤ C ≤ q$) indicating the number of operations and the initial colour of vertex 1ドル$.

For the following $q$ lines, each line describes an operation taking place in order with 3ドル$ or 4ドル$ integers.

  • If the $i$-th line contains 4ドル$ integers 0ドル,ドル $x_i,ドル $c_i$ and $d_i$ (1ドル ≤ x_i ≤ n,ドル 1ドル ≤ c_i ≤ q,ドル 1ドル ≤ d ≤ 10^9$), the $i$-th operation will add a new vertex $(n + 1)$ with colour $c_i$ to the tree and connect it to vertex $x_i$ with an edge of length $d_i$.
  • If the $i$-th line contains 3ドル$ integers 1ドル,ドル $x_i$ and $c_i$ (1ドル ≤ x_i ≤ n,ドル 1ドル ≤ c_i ≤ q$), the $i$-th operation will change the colour of vertex $x_i$ to $c_i$.

It’s guaranteed that the sum of $q$ of all test cases will not exceed 5ドル \times 10^5$.

출력

For each operation output the maximum distance between two vertices with different colours. If no valid pair exists output 0ドル$ instead.

제한

예제 입력 1

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

예제 출력 1

0
0
2
3
2
0

힌트

출처

Contest > Waterloo's local Programming Contests > Waterloo Winter 2023 Local E번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /