You are given a weighted undirected tree on vertices and a list of
updates. Each update changes the weight of one edge. The task is to output the diameter of the tree after each update.
The first line contains three space-separated integers,
and
(
,
) – the number of vertices in the tree, the number of updates and the limit on the weights of edges. The vertices are numbered
through
.
Next,lines describing the initial tree follow. The
-th of these lines contains three space-separated integers
,
,
(
,
) meaning that initially, there is an edge between vertices
and
with weight
. It is guaranteed that these
lines describe a tree.
Finally,lines describing queries follow. The
-th of these lines contains two space-separated integers
,
(
). These two integers are then transformed according to the following scheme:
··whereis the result of the last query (initially
). Tuple
represents a query which takes the
-th edge from the input and sets its weight to
.
Outputlines. For each
, line
should contain the diameter of the tree after the
-th update.
10 10 10000 1 9 1241 5 6 1630 10 5 1630 2 6 853 10 1 511 5 3 760 8 3 1076 4 10 1483 7 10 40 8 2051 5 6294 5 4168 7 1861 0 5244 6 5156 3 3001 8 5267 5 3102 8 3623
The first sample is depicted in the figure below. The left-most picture shows the initial state of the graph. Each following picture depicts the situation after an update. The weight of the updated edge is painted green, and the diameter is red.The first query changes the weight of therd edge, i.e.
, to
. The largest distance between any pair of vertices is
– the distance between
and
.
As the answer is, the second query is
![]()
Hence the weight of the edge
is changed to
. This causes the pair
to be the pair with the greatest distance, namely
.
The third query is decoded as![]()
As the weight of the edge
decreases to
, the most distant pair is suddenly
with
.