#P10238. Tree Edge Deletion and Maximum Diameter
Tree Edge Deletion and Maximum Diameter
Tree Edge Deletion and Maximum Diameter
You are given a tree with \(n\) nodes and \(n-1\) edges. The tree is an undirected connected graph. Each edge has a weight. The distance between two nodes \(u\) and \(v\) is defined as the sum of the weights along the unique simple path connecting them, i.e. \(\mathrm{dist}(u,v)\). In particular, we define \(\mathrm{dist}(u,u)=0\).
There are \(q\) operations. In each operation, one edge is removed from the tree. After at least one removal the tree is split into several connected components. For each operation, you are required to compute the maximum diameter among all current connected components. Formally, after each operation, you need to output
[ \max_{c \in C} \Big{ \max_{u,v \in c} ; \mathrm{dist}(u,v) \Big} ]
where (C) is the set of all connected components.
Note: The operations are performed sequentially. That is, after each edge removal you must compute the maximum diameter of the components in the current forest.
inputFormat
The input begins with two integers \(n\) and \(q\) \((1 \leq n \leq 10^5,\; 1 \leq q \leq n-1)\) denoting the number of nodes and the number of operations respectively.
The next \(n-1\) lines each contain three integers \(u\), \(v\) and \(w\) \((1 \leq u,v \leq n,\; 1 \leq w \leq 10^9)\) representing an edge between nodes \(u\) and \(v\) with weight \(w\). The edges are numbered from 1 to \(n-1\) in the order of input.
The following \(q\) lines each contain one integer \(e\) \((1 \leq e \leq n-1)\) indicating that the edge numbered \(e\) is removed in that operation. It is guaranteed that each edge is removed at most once.
outputFormat
Output \(q\) lines. The \(i\)th line should contain a single integer representing the maximum diameter among all connected components after the \(i\)th operation.
sample
4 3
1 2 3
2 3 2
2 4 4
2
3
1
7
3
0
</p>