Problem Statement
We want to find an undirected tree with the following properties:
- The number of nodes is exactly n.
- The number of simple paths of length 2 is exactly s.
Note that the direction of the path does not matter: A-B-C is the same path as C-B-A.
Return "Possible" (quotes for clarity) if such a tree exists and "Impossible" otherwise.
Definition
- Class:
- TreeAndPathLength2
- Method:
- possible
- Parameters:
- int, int
- Returns:
- String
- Method signature:
- String possible(int n, int s)
- (be sure your method is public)
Constraints
- n will be between 2 and 50, inclusive.
- s will be between 1 and 1,000, inclusive.
Examples
4
3
Returns: "Possible"
We want a tree on n=4 vertices that will have s=3 simple paths of length 2. Let the vertices be labeled A, B, C, and D. Consider the tree that contains the edges A-B, B-C, and B-D. This tree does indeed contain exactly three simple paths of length 2. These are the paths A-B-C, A-B-D, and C-B-D.
4
2
Returns: "Possible"
In this case, one valid tree contains the edges A-B, B-C, and C-D. The two simple paths of length 2 are A-B-C and B-C-D.
3
2
Returns: "Impossible"
There is only one possible tree on 3 nodes: a path of length 2. Obviously, this tree only contains a single path of length 2. Thus, we cannot have n=3 and s=2.
5
4
Returns: "Possible"
50
999
Returns: "Impossible"
50
1000
Returns: "Possible"
50
49
Returns: "Possible"
50
48
Returns: "Possible"
50
47
Returns: "Impossible"
50
123
Returns: "Possible"
50
124
Returns: "Possible"
50
125
Returns: "Possible"
50
126
Returns: "Possible"
50
53
Returns: "Possible"
48
456
Returns: "Possible"
2
1
Returns: "Impossible"
3
1
Returns: "Possible"
3
2
Returns: "Impossible"
2
1000
Returns: "Impossible"
10
25
Returns: "Impossible"
10
20
Returns: "Possible"
10
8
Returns: "Possible"
10
36
Returns: "Possible"
10
35
Returns: "Impossible"
45
75
Returns: "Possible"
45
666
Returns: "Possible"
49
999
Returns: "Impossible"
5
10
Returns: "Impossible"
6
3
Returns: "Impossible"
7
14
Returns: "Impossible"
5
5
Returns: "Impossible"
7
5
Returns: "Possible"