TopologicalOrdering
2016 TCO Algo 3A · 2016-03-24 · by cgy4ever
2016 TCO Algo 3A · 2016-03-24 · by cgy4ever · Search
Problem Statement
Problem Statement
Given a directed graph, a topological ordering of its vertices is a permutation P of its vertices with the following property: whenever the graph contains an edge from vertex u to vertex v, the vertex u appears in P before the vertex v.
It is known that computing the number of topological orderings in a given directed graph is hard (in particular, #P-complete). In this task we will take a look at the inverse problem: given n, find any directed graph with exactly n different topological orderings.
Here is a precise definition of our task: You are given anint n.
Construct a graph that satisfies the following constraints:
You may assume that for the given constraints a solution always exists. If there are multiple solutions, you may choose any of them. Return aint[] that contains the number of vertices in your graph followed by a list of its edges.
Formally, the correct return value should be constructed as follows: Suppose that your graph has x vertices and y edges. Let a[] and b[] be such that for each valid i your graph contains an edge from vertex a[i] to vertex b[i]. Then, return the followingint[] : {x, a[0], b[0], a[1], b[1], ..., a[y-1], b[y-1] }.
It is known that computing the number of topological orderings in a given directed graph is hard (in particular, #P-complete). In this task we will take a look at the inverse problem: given n, find any directed graph with exactly n different topological orderings.
Here is a precise definition of our task: You are given an
- The graph must have exactly n different topological orderings.
- The number of vertices in the graph must be between 1 and 50, inclusive.
- The vertices must be numbered 0 through x-1, where x is the number of vertices.
- The number of edges in the graph must be between 0 and 100, inclusive.
- There must not be any self-loops.
- There must not be any duplicate edges. (I.e., for each ordered pair of vertices (u,v), there must be at most one edge from u to v.)
You may assume that for the given constraints a solution always exists. If there are multiple solutions, you may choose any of them. Return a
Formally, the correct return value should be constructed as follows: Suppose that your graph has x vertices and y edges. Let a[] and b[] be such that for each valid i your graph contains an edge from vertex a[i] to vertex b[i]. Then, return the following
Constraints
- n will be between 1 and 32767, inclusive.
Examples
0)
5
Returns: {5, 0, 1, 1, 2, 2, 3 }
We have a graph with 5 nodes and 3 edges: 0->1->2->3. This graph has 5 topological orderings: {0,1,2,3,4}, {0,1,2,4,3}, {0,1,4,2,3}, {0,4,1,2,3} and {4,0,1,2,3}.
1)
4
Returns: {5, 0, 2, 1, 2, 2, 3, 2, 4 }
This time we have a graph with 5 nodes and 4 edges: 0->2, 1->2, 2->3 and 2->4.
2)
720
Returns: {6 }
A graph with 6 nodes and no edges has 6! = 720 topological orderings.
3)
1
Returns: {1 }
4)
32767
Returns: {23, 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 0, 16, 0, 17, 0, 18, 1, 19, 1, 20, 1, 21, 1, 22 }
Submissions are judged against all 156 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Coding Area
Language: C++17 · define a public class TopologicalOrdering with a public method vector<int> construct(int n) · 156 test cases · 2 s / 256 MB per case