luogu#P10238. [yLCPC2024] F. PANDORA PARADOXXX

    ID: 9803 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>洛谷原创最近公共祖先 LCA洛谷月赛离线处理

[yLCPC2024] F. PANDORA PARADOXXX

Background

The arcades in Fusu’s city jointly hosted KING of PerforPandora!

However, because heavy snow blocked the roads, some arcades cannot be reached. She wants to know, among the arcades that can reach each other, what the maximum possible distance is.

Problem Description

You are given a tree with nn nodes. A tree is defined as an undirected connected graph with nn vertices and n1n - 1 edges. The edges of this tree have weights. The distance dist(u,v)\mathrm{dist}(u,v) between two nodes u,vu, v is defined as the sum of edge weights along the simple path from uu to vv. It can be proven that the simple path between any two nodes in a tree is unique. In particular, we define dist(u,u)=0\mathrm{dist}(u, u) = 0.

Now there are qq operations. In each operation, one edge of the tree is deleted. Obviously, after performing at least one operation, the tree will be split into several connected components. After each operation, you need to find the maximum, over all connected components, of the distance between the farthest two nodes within that component.

Formally, after each operation, you need to compute

$$\max\limits_{c \in C}\{\max\limits_{u, v \in c} \mathrm{dist}(u,v)\}$$

where CC denotes the set of all current connected components.

Input Format

This problem contains multiple test cases within a single input file. The first line contains a positive integer TT, denoting the number of test cases. For each test case:

The first line contains two integers n,qn, q (1q<n1051 \leq q < n \leq 10^5), representing the number of nodes in the tree and the number of operations.
The next n1n - 1 lines each contain three integers u,v,wu, v, w (1u,vn1 \leq u, v \leq n, 1w1051 \leq w \leq 10^5), indicating an edge between uu and vv with weight ww.
The next qq lines each contain one integer eie_i (1ei<n1 \leq e_i < n), indicating that this operation deletes the eie_i-th edge in the input. It is guaranteed that each edge is deleted at most once.

It is guaranteed that, within a single test file, the sum of all nn does not exceed 3×1053 \times 10^5.

Output Format

For each test case, output qq lines. Each line contains one integer, in order, representing the required answer after each operation.

2
4 2
1 2 1
2 3 2
3 4 3
2
3
12 2
1 2 1
2 3 1
1 4 3
2 5 4
5 6 3
5 7 2
7 8 1
8 9 1
9 10 1
7 11 5
8 12 3
4
6
3
1
10
9

Hint

Hint

Please note that large-scale input and output can significantly affect program performance. Choose appropriate I/O methods, do not flush the output buffer frequently, and avoid TLE.

Translated by ChatGPT 5