#K5476. Maximum Trade Profit
Maximum Trade Profit
Maximum Trade Profit
You are given V planets and E trade routes connecting them. Each trade route has a profit value associated with it. Your task is to select a subset of the trade routes such that no cycle is formed, and the total profit is maximized. In other words, you need to construct a Maximum Spanning Forest (or a Maximum Spanning Tree if the graph is connected).
Note: The graph may be disconnected, in which case the maximum profit is the sum of the profits of the maximum spanning trees for each connected component.
inputFormat
The first line of input contains two integers V and E, which represent the number of planets and the number of trade routes, respectively.
The next E lines each contain three integers u, v, and w, describing a trade route between planets u and v with a profit value w.
Planets are numbered from 0 to V-1.
outputFormat
Output a single integer: the maximum profit that can be obtained by selecting a subset of trade routes without forming any cycle.
## sample4 5
0 1 10
0 2 6
0 3 5
1 3 15
2 3 4
31
</p>