#P4115. Tree Coloring and Longest White Node Distance
Tree Coloring and Longest White Node Distance
Tree Coloring and Longest White Node Distance
You are given a tree with n nodes and weighted edges. Initially, all nodes are white. You need to process q operations on the tree. There are two types of operations:
- C x: Toggle the color of node x (if white then becomes black, and if black then becomes white).
- A: Query the maximum distance between any two white nodes in the tree. Note that the two queried white nodes may be the same (in which case the distance is 0).
The distance between two nodes is defined as the sum of the weights along the unique simple path between them in the tree.
Note: Even if some intermediate nodes on the path are black, the distance is computed using the tree path. If there is only one white node or no white node at all, the answer is 0.
The tree is undirected and connected.
Precomputed distances: Since the tree is static (only node colors change), you may precompute all pairs shortest path distances using standard tree traversal techniques.
For any formula used, please present it in \( \LaTeX \)
format.
inputFormat
The first line contains two integers n and q (1 ≤ n ≤ N, 1 ≤ q ≤ Q) representing the number of nodes and the number of operations respectively.
The next n - 1 lines each contain three integers u, v, and w denoting an undirected edge between nodes u and v with weight w (\(w > 0\)).
The following q lines describe the operations. Each operation is given in one of the following formats:
- C x: Toggle the color of node x (1-indexed).
- A: Query the maximum distance between any two white nodes.
outputFormat
For each query operation (denoted by 'A'), output one line containing a single integer, which is the maximum distance between any pair of white nodes in the tree.
sample
3 5
1 2 5
2 3 6
A
C 2
A
C 2
A
11
11
11
</p>