#P5632. 【模板】Stoer-Wagner

【模板】Stoer-Wagner

Description

Define the minimum cut of an undirected graph GG as follows: it is a set of edges such that removing these edges can make GG split into two connected components, and the sum of edge weights in this set is minimal.

Given an undirected graph GG, find its minimum cut.

Input Format

The first line contains two numbers n,mn, m, representing the number of vertices and the number of edges.

The next mm lines each contain three positive integers ai,bi,wia_i, b_i, w_i, indicating that there is an edge between aia_i and bib_i with weight wiw_i.

Output Format

Output one line with one integer representing the minimum cut of GG. If the graph is disconnected, output 0.

4 6
1 2 5
1 3 1
2 4 1
3 4 2
2 3 1
1 4 2

4

Hint

For the first 20%20\% of the testdata, n10n \leq 10.
For the first 40%40\% of the testdata, n100n \leq 100.
For another 10%10\% of the testdata, the input is guaranteed to be a tree.
For another 10%10\% of the testdata, the input is guaranteed to be a chain.
For 100%100\% of the testdata, n600,mn×(n1)2n \leq 600, m \leq \frac{n\times (n-1)}{2}, and it is guaranteed that i=1mwi109\sum_{i=1}^{m} w_i \leq 10^9.

PS: If you want to submit “maximum flow / minimum cut tree”, just forget it.

Translated by ChatGPT 5