Roads and Ramen
Contest: Technocup 2021 - Elimination Round 1
Difficulty rating: 2800
Tags: data structures, trees

In the Land of Fire there are $$$n$$$ villages and $$$n-1$$$ bidirectional road, and there is a path between any pair of villages by roads. There are only two types of roads: stone ones and sand ones. Since the Land of Fire is constantly renovating, every morning workers choose a single road and flip its type (so it becomes a stone road if it was a sand road and vice versa). Also everyone here loves ramen, that's why every morning a ramen pavilion is set in the middle of every stone road, and at the end of each day all the pavilions are removed.

For each of the following $$$m$$$ days, after another road is flipped, Naruto and Jiraiya choose a simple path — that is, a route which starts in a village and ends in a (possibly, the same) village, and doesn't contain any road twice. Since Naruto and Jiraiya also love ramen very much, they buy a single cup of ramen on each stone road and one of them eats it. Since they don't want to offend each other, they only choose routes where they can eat equal number of ramen cups. Since they both like traveling, they choose any longest possible path. After every renovation find the maximal possible length of a path (that is, the number of roads in it) they can follow.

Input format:
The first line contains the only positive integer $$$n$$$ ($$$2 \leq n \leq 500\,000$$$) standing for the number of villages in the Land of Fire.

Each of the following $$$(n-1)$$$ lines contains a description of another road, represented as three positive integers $$$u$$$, $$$v$$$ and $$$t$$$ ($$$1 \leq u, v \leq n$$$, $$$t \in \{0,1\}$$$). The first two numbers denote the villages connected by the road, and the third denotes the initial type of the road: $$$0$$$ for the sand one and $$$1$$$ for the stone one. Roads are numbered from $$$1$$$ to $$$(n-1)$$$ in the order from the input.

The following line contains a positive integer $$$m$$$ ($$$1 \leq m \leq 500\,000$$$) standing for the number of days Naruto and Jiraiya travel for.

Each of the following $$$m$$$ lines contains the single integer $$$id$$$ ($$$1 \leq id \leq n-1$$$) standing for the index of the road whose type is flipped on the morning of corresponding day.

It is guaranteed that there is a road path between any pair of villages.

Output format:
Output $$$m$$$ lines. In the $$$i$$$-th of them print the only integer denoting the maximal possible length of any valid path on the $$$i$$$-th day.

Constraints:
Time limit: 5.0 s
Memory limit: 512.0 MB

Note:
After the renovation of the $$$3$$$-rd road the longest path consists of the roads $$$1$$$, $$$2$$$ and $$$4$$$.

After the renovation of the $$$4$$$-th road one of the longest paths consists of the roads $$$1$$$ and $$$2$$$.

After the renovation of the $$$1$$$-st road one of the longest paths consists of the roads $$$1$$$, $$$2$$$ and $$$3$$$.

After the renovation of the $$$3$$$-rd road the longest path consists of the roads $$$1$$$, $$$2$$$ and $$$4$$$.

After the renovation of the $$$4$$$-rd road one of the longest paths consists of the roads $$$2$$$ and $$$4$$$.

Example input:
5
1 2 0
1 3 0
3 5 0
3 4 0
5
3
4
1
3
4

Example output:
3
2
3
3
2

Editorial:
Fix any diameter of the tree. One of the optimal paths always starts at one of the diameter's endpoints.
Proof:

Let $$$AB$$$ be a diameter of the tree, and the optimal answer be $$$EF$$$. Then the parity of the number of stone roads on $$$DE$$$ is the same as on $$$DF$$$, and also the same holds for $$$CE$$$ and $$$CF$$$. Since a diameter has the greatest length among all paths in the tree, the stone roads parity is different on $$$AC$$$ and on $$$BC$$$ (otherwise, the diameter would be an answer). Hence, the stone roads parity on $$$CE$$$ coincides with one of $$$AC$$$ and $$$BC$$$. Assume without loss of generality that the stone roads parities of $$$AC$$$ and $$$CE$$$ are the same. Then the path $$$AE$$$ contains an even number of stone roads. Note that since $$$AB$$$ is a diameter, $$$AC$$$ is no shorter than $$$CF$$$, hence $$$AD$$$ is no shorter than $$$DF$$$, which implies that $$$AE$$$ is not shorter than $$$EF$$$. This means that there is an optimal path starting at one of the diameter's endpoints.
Now remaining is to solve the problem if one of the endpoints is fixed. If we root the tree from it, and write down for each vertex the stone roads parity between it and the root, then each query is basically changing the parity of a subtree. In an euler-tour traversal every such subtree is represented by a contiguous subsegment. Now the original problem can be reformulated in a following way: we have a binary array, there are queries of type "flip a subsegment", and after each query we need to find a zero with the greatest depth parameter. This can be done via a segment tree, where in each node we store the deepest zero and the deepest one on the subsegment corresponding to that node. The final time complexity is $$$O(n\log{n})$$$.