#P4565. [CTSC2018] 暴力写挂

    ID: 3457 远端评测题 4000ms 512MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>2018线段树WC/CTSC/集训队O2优化分治最近公共祖先,LCA

[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 k=0k = 0 asks for the longest path on tree TT. 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 O(n2logn)O(n^2 \log n) partial-solution program for k=0k = 0 that enumerates pairs and uses LCA to compute distances. So temporaryDO chose 11 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 TT'. As a result, the program did not RE, but when computing the distance between xx and yy, 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 i,ji, j with iji \le j. temporaryDO’s program gloriously scored zero during evaluation. Unconvinced, he decided to spend several days reproducing his program’s output. Given TT and TT', please help the poor temporaryDO compute what his program outputs.

Input Format

The first line contains an integer nn, the number of nodes in the tree.

Lines 22 to nn each contain three integers x,y,vx, y, v, indicating that in TT there is an edge between xx and yy with length vv.

Lines n+1n + 1 to 2n12n - 1 each contain three integers x,y,vx, y, v, indicating that in TT' there is an edge between xx and yy with length vv.

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 (3,4)(3, 4), the distance is computed as 3+0(0+(2))=53 + 0 - (0 + (-2)) = 5.

Constraints
For all testdata, 1n3666661 \le n \le 366666, v2017011328|v| \le 2017011328. Detailed constraints are shown in the table below; in the table, “无” means no special restriction.

测试点编号 nn \le vv TT is a path TT' is a path
11 3636 =1=1 No
22 366366
33 13881388 >0>0
44 19991999
55 26662666
66 56665666 none
77 86668666
88 1111111111
99 1234512345
1010 366666366666 >0>0 Yes Yes
1111 none
121312\sim 13 >0>0 No
1414 none
151615\sim 16 >0>0 No Yes
1717 none
182018\sim 20 No

depth(p)\mathrm{depth}(p) and depth(p)\mathrm{depth'}(p) denote the distances from node 11 to node pp in trees TT and TT' respectively. Here, distance means the sum of edge weights along the path, and depth(1)=0\mathrm{depth}(1) = 0.

LCA(x,y)\mathrm{LCA}(x, y) and LCA(x,y)\mathrm{LCA'}(x, y) denote, in TT and TT' respectively, the lowest common ancestor of nodes xx and yy, i.e., the point on the shortest path from xx to yy that has the fewest edges from the root.

Translated by ChatGPT 5