#P4381. [IOI 2008] Island

    ID: 3295 远端评测题 1000ms 250MiB 尝试: 3 已通过: 1 难度: 7 上传者: 标签>动态规划,dp2008IOIO2优化树的直径队列环套树,基环树

[IOI 2008] Island

Description

You plan to tour a park that consists of NN islands. From each island ii, the local authority has built a bridge to another island, with length LiL_i, and bridges can be walked in both directions. In addition, for every pair of islands there is a dedicated ferry that connects the two. Compared with taking a ferry, you prefer walking. You want the total length of bridges you walk across to be as large as possible, subject to the following rules:

  • You may choose any island to start your tour.
  • No island may be visited more than once.
  • At any time, you may move from your current island SS to another unvisited island DD. You may do so in one of the following ways:
    • Walk: only if there is a bridge directly between the two islands. In this case, the bridge length is added to your total walking distance.
    • Ferry: you may choose this only if there is no combination of bridges and ferries you have already used that allows you to get from SS to DD. When checking reachability, you should consider all paths, including those that pass through islands you have already visited.

Note that you do not have to visit all the islands, and you might be unable to walk across all the bridges.

Write a program that, given NN bridges and their lengths, computes the maximum possible sum of bridge lengths you can walk under the rules above.

Input Format

The first line contains an integer NN, the number of islands in the park.

Each of the next NN lines describes one island. The ii-th line contains two integers, separated by a single space, describing the bridge built from island ii: the first integer is the other endpoint island, and the second integer is the bridge length LiL_i. You may assume that the endpoints of every bridge are always different islands.

Output Format

Output a single integer, the maximum possible walking distance.

7
3 8
7 2
4 2
1 4
1 9
3 4
2 3
24

Hint

Sample explanation:

Sample illustration

In the sample, N=7N=7 bridges are (13)(1-3), (27)(2-7), (34)(3-4), (41)(4-1), (51)(5-1), (63)(6-3), and (72)(7-2). Note that there are two different bridges between islands 22 and 77.

One way to achieve the maximum walking distance is as follows:

  • Start at island 55.
  • Walk across the bridge of length 99 to island 11.
  • Walk across the bridge of length 88 to island 33.
  • Walk across the bridge of length 44 to island 66.
  • Take the ferry from island 66 to island 77.
  • Walk across the bridge of length 33 to island 22.

You end at island 22, and your total walking distance is 9+8+4+3=249+8+4+3=24.

Only island 44 is not visited. Note that, at the end of the tour above, you cannot visit that island. More precisely:

  • You cannot walk there because there is no bridge between island 22 (your current island) and island 44.
  • You cannot take a ferry there because island 44 is reachable from your current island 22. One possible way is: take bridge (27)(2-7), then the previously used ferry from island 77 to island 66, then take bridge (63)(6-3), and finally bridge (34)(3-4).

Constraints: For 100%100\% of the testdata, 2N1062 \leqslant N \leqslant 10^6, 1Li1081 \leqslant L_i \leqslant 10^8.

Translated by ChatGPT 5