Smallest Graph with K Spanning Trees
Time limit5sMemory limit512 MB
Find the smallest node count of a graph built by the move/attach process whose spanning tree count equals K, for K up to 10000.
- Level
Hard8 of 10
- Topics
- Graph, Dynamic programming, Number theory, Math
- Solved
- No attempts yet
Problem
A spanning tree of an undirected graph is a tree that uses only edges of the graph and contains every node, so it has one edge fewer than the number of nodes. Two spanning trees are different when their edge sets are different.
You want a graph whose number of spanning trees is exactly , and you want to use as few nodes as possible. Only a graph built by the process below counts.
Start with a graph that has a single node. That node is both the center and the tip . Then apply the following two operations any number of times, in any order.
- Move: make the current tip the new center. That is, set , and add no node and no edge.
- Attach: choose positive integers and . If and are the same node, then is required. Create new nodes and add the edges . Then set .
Attach glues on a new path that leaves the tip and returns to the center, and the -th new node of that path becomes the next tip. A graph built by this process never has an edge from a node to itself, and never has two edges between the same pair of nodes.
Find the smallest number of nodes of a graph that this process can build and that has exactly spanning trees.
Input
The first line has the number of test cases . Each of the next lines has one integer .
Output
For each test case print one line of the form Case #x: y, where is the test case number starting from 1 and is the smallest number of nodes.
Every in the given range has an answer, and the answer is never larger than 22.
Explanation
In the first Attach, and are the same node, so the operation creates a cycle of length . A cycle has as many spanning trees as it has edges, so that graph has spanning trees. For a single triangle is enough and the answer is 3.
For , first build a triangle with and , then apply Attach once more with and . The result has 4 nodes and 5 edges, and it has 8 spanning trees.
After a Move, everything attached later shares exactly one node with the part built before. The number of spanning trees of the whole graph is then the product of the numbers of the two parts. Two triangles that share one node have 5 nodes and 9 spanning trees.