Problem Statement
Let G be a simple undirected graph.
An unordered pair {a, b} of nodes in G is called a good pair if a and b are in the same connected component of G and the distance between a and b is exactly d.
(The distance between two nodes is the smallest number of edges on a path from one node to the other.)
You are given the int d and an int k. Construct an undirected graph G with the following properties:
You may assume that each valid test case has a solution. If there are multiple solutions, you may return any of them.
You are given the int d and an int k. Construct an undirected graph G with the following properties:
- The graph must be simple (i.e., no self-loops and no multiple edges).
- The number of nodes n must be between 3 and 1,000, inclusive.
- The nodes must be numbered 0 through n-1.
- The number of edges m must be between 2 and 1,000, inclusive.
- The graph must contain exactly k good pairs of nodes.
You may assume that each valid test case has a solution. If there are multiple solutions, you may return any of them.
Definition
- Class:
- GraphAndPairs
- Method:
- construct
- Parameters:
- int, int
- Returns:
- int[]
- Method signature:
- int[] construct(int d, int k)
- (be sure your method is public)
Constraints
- d will be between 2 and 50, inclusive.
- k will be between 1 and 50,000, inclusive.
Examples
2
1
Returns: {4, 0, 1, 1, 3, 3, 2, 2, 0, 1, 2 }The graph looks as follows: Â Â Â Â 0 - 1 | / | 2 - 3 The only good pair of vertices is {0, 3}.2
2
Returns: {8, 0, 1, 1, 3, 3, 2, 2, 0, 1, 2, 4, 5, 5, 7, 7, 6, 6, 4, 5, 6 }This time the graph looks as follows: Â Â Â Â 0 - 1 4 - 5 | / | | / | 2 - 3 6 - 7 The good pairs are {0, 3} and {4, 7}.4
4
Returns: {8, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 0 }5
2
Returns: {10, 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 6, 7, 7, 8, 8, 9, 0, 5, 1, 6, 2, 7, 3, 8, 4, 9 }