#P4565. [CTSC2018] 暴力写挂
[CTSC2018] 暴力写挂
Description
temporaryDO is a very weak OIer. In April, during the provincial team qualifier, he saw the problem “Link-Cut Tree.” The partial score for asks for the longest path on tree . Poor temporaryDO didn’t know how to solve it; even after scratching his head, he couldn’t come up with any idea.
At that moment, the kind Banban appeared in the air, radiating a brilliant yet gentle light that rippled across the exam hall. “The problem isn’t hard,” Banban said. That magnetic voice filled temporaryDO with strength.
He decided to write an partial-solution program for that enumerates pairs and uses LCA to compute distances. So temporaryDO chose as the root, built a heavy-light decomposition structure for LCA, and then wrote a double for loop to enumerate pairs.
However, clumsy temporaryDO accidentally allocated an array too small, causing an out-of-bounds access into a mysterious memory region. Coincidentally, that region happened to store another tree . As a result, the program did not RE, but when computing the distance between and , it calculated
$$\mathrm{depth}(x) + \mathrm{depth}(y) - ({\mathrm{depth}(\mathrm{LCA}(x,y))}+{\mathrm{depth'}(\mathrm{LCA'}(x,y))})$$Finally, the program outputs the maximum of the above-defined “distance” over all pairs with . temporaryDO’s program gloriously scored zero during evaluation. Unconvinced, he decided to spend several days reproducing his program’s output. Given and , please help the poor temporaryDO compute what his program outputs.
Input Format
The first line contains an integer , the number of nodes in the tree.
Lines to each contain three integers , indicating that in there is an edge between and with length .
Lines to each contain three integers , indicating that in there is an edge between and with length .
Output Format
Output a single integer on one line: the output of temporaryDO’s program.
6
1 2 2
1 3 0
2 4 1
2 5 -7
3 6 0
1 2 -1
2 3 -1
2 5 3
2 6 -2
3 4 8
5
Hint
Sample explanation 1
For the pair , the distance is computed as .
Constraints
For all testdata, , . Detailed constraints are shown in the table below; in the table, “无” means no special restriction.
| 测试点编号 | is a path | is a path | ||
|---|---|---|---|---|
| No | ||||
| none | ||||
| Yes | Yes | |||
| none | ||||
| No | ||||
| none | ||||
| No | Yes | |||
| none | ||||
| No | ||||
and denote the distances from node to node in trees and respectively. Here, distance means the sum of edge weights along the path, and .
and denote, in and respectively, the lowest common ancestor of nodes and , i.e., the point on the shortest path from to that has the fewest edges from the root.
Translated by ChatGPT 5
京公网安备 11011102002149号