Islands
Time limit2sMemory limit128 MB
Each island has one undirected weighted edge; find the maximum-weight walk choosing one edge per component/tree path structure respecting ferry reachability rules.
- Level
Hard8 of 10
- Topics
- Graph, Greedy, Union-find, Implementation
- Solved
- No attempts yet
Problem
You are visiting a park with islands, numbered through . From each island , exactly one bridge was built, connecting island to some other island; that bridge has length . There are therefore bridges in total. Although each bridge was built starting from one island, every bridge can be crossed in both directions. In addition, for every pair of islands there is a ferry that shuttles back and forth between them.
Because you enjoy walking more than riding ferries, you want to maximize the total length of the bridges you cross, subject to the following rules:
- You may start on any island of your choice.
- You may never visit the same island twice.
- From your current island you may move to an island that you have not visited yet, in one of two ways:
- Walk: allowed only if a bridge directly connects and . The bridge's length is added to your total walking distance.
- Ferry: allowed only if is not reachable from using any combination of bridges and ferries you have already used. (When checking reachability, consider every path, including paths that pass through islands you have already visited.)
You do not have to visit every island, and it may be impossible to cross every bridge.
Given the bridges and their lengths, compute the maximum total distance you can walk while obeying the rules above.
Input
- The first line contains the integer , the number of islands (). Islands are numbered from to .
- Each of the next lines describes one bridge. Line (for ) contains two space-separated integers: the island at the other endpoint of the bridge built from island , followed by that bridge's length (). The two endpoints of every bridge are always different islands.
Output
Print a single line containing one integer: the maximum possible total walking distance.
Note: for some inputs the answer does not fit in a 32-bit integer, so use a 64-bit integer type (for example long long in C/C++ or a normal integer in Python).
Note

In the sample, the bridges are , , , , , and . Note that there are two different bridges connecting islands and .
One way to achieve the maximum walking distance is:
- Start on island .
- Walk the bridge of length to reach island .
- Walk the bridge of length to reach island .
- Walk the bridge of length to reach island .
- Take the ferry from island to island .
- Walk the bridge of length to reach island .
You finish on island with a total walking distance of . The only island left unvisited is island , and you can no longer reach it: not by walking (there is no bridge between island and island ), and not by ferry (island is reachable from island via the bridge , then the ferry you already used from island to island , then the bridges and ).