HalfGraph
TCO19 SRM 751 · 2019-01-09 · by majk
Problem Statement
Let G=(V,E) be an undirected graph.
A spanning subgraph of G is a graph that can be created by removing some (possibly empty) subset of edges from G. Note that a spanning subgraph must still contain all vertices of the original graph G.
A spanning subgraph H is called a half of G if the degree of every vertex v in H is exactly half of its degree in G.
You are given
Find and return the adjacency matrix of any graph that is a half of G. If there are multiple answers, you may return any one of them. If there are no answers, output an empty
Constraints
- n will be between 1 and 50, inclusive.
- a has exactly n elements.
- Each element of a is a string of length n, consisting of characters '0' and '1' only.
- The matrix is symmetric, i.e., for all i,j we have a[i][j] = a[j][i].
- The main diagonal contains only characters '0'. In other words, the graph doesn't contain any loops.
{"000",
"000",
"000"}
Returns: {"000", "000", "000" }
An empty graph is its own half, because the half of zero is zero.
{"01111",
"10111",
"11011",
"11101",
"11110"}
Returns: {"00011", "00110", "01001", "11000", "10100" }
The input is a complete graph G on five vertices. In the graph G the degree of each vertex is 4. Thus, the output can be any subgraph of G that has 5 vertices and in which the degree of each vertex is 2.
{"010",
"101",
"010"}
Returns: { }
This graph contains the edges 0-1 and 1-2. It does not have any half, because you cannot have a vertex of degree 1/2.
{"01111",
"10100",
"11000",
"10001",
"10010"}
Returns: {"00011", "00100", "01000", "10000", "10000" }
Remember that the returned graph must be a subgraph of the original one. You are not allowed to add new edges, you may only erase the existing ones.
{"01111","10111","11011","11101","11110"}
Returns: {"00011", "00110", "01001", "11000", "10100" }
Submissions are judged against all 54 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class HalfGraph with a public method vector<string> compute(vector<string> a) · 54 test cases · 2 s / 256 MB per case