AllGraphCuts
SRM 687 · 2016-04-01 · by lg5293
Problem Statement
All graphs in this problem statement are simple undirected graphs with nonnegative integer edge weights. Here, "simple" means that there are no self-loops and each pair of vertices is connected by at most one direct edge.
Given a connected graph G, a cut between vertices i and j is a set E of edges such that each path from i to j contains at least one edge from the set E. In other words, if you cut all edges in E, the vertices i and j will end in different connected components.
If E is a set of edges, the weight of E is the sum of weights of edges it contains. For any distinct vertices i and j, the min cut weight between i and j is the minimum weight of a cut between i and j. Additionally, for any vertex i we define that the min cut weight between i and i is 0.
You are given a
- For each i and j, the min cut weight between vertices i and j must be exactly x[i*n+j].
- G must have at most 1000 edges.
- The weight of each edge must be between 0 and 10^5, inclusive.
If there are no solutions, return the
Constraints
- n will be between 2 and 50, inclusive.
- x will have exactly n*n elements.
- Each element of x will be between 0 and 10^5, inclusive.
{0,1,
1,0}
Returns: {6 }
In this case n = 2. The output can be decoded as a single edge between nodes 0 and 1 with weight 1 (i.e. 1*2*2 + 1*2 + 0 = 6).
{0,1,
1,1}
Returns: {-1 }
Note that since we have x[1*2+1] != 0, there is no valid solution.
{0,2,2,
2,0,2,
2,2,0}
Returns: {10, 11, 14 }
This graph can be decoded as a triangle graph, where all the edge weights are 1.
{0,1,2,3,4,
1,0,2,3,4,
1,2,0,3,4,
1,2,3,0,4,
1,2,3,4,0}
Returns: {-1 }
{0,0,0,0}
Returns: {2 }
Note that some edge weights can be zero. The empty int[] {} is not a correct return value, since the graph G you return must be connected.
Submissions are judged against all 130 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class AllGraphCuts with a public method vector<int> findGraph(vector<int> x) · 130 test cases · 2 s / 256 MB per case