Lock Patterns and Spanning Trees
Time limit1sMemory limit128 MB
Count the spanning trees of an m by m king-move grid for m up to 6 by evaluating a cofactor of its Laplacian matrix.
- Level
Medium4 of 10
- Topics
- Matrix, Math, Combinatorics, Graph
- Solved
- No attempts yet
Problem
Android uses a pattern to lock a smartphone. The common lock pattern is a 3×3 grid of 9 nodes. Sanghyun wants stronger security, so he is building an app that offers the lock patterns described here.
In his app a lock pattern has a size from 2×2 up to , and only adjacent nodes can be connected. Two nodes are adjacent when one is directly next to the other horizontally, vertically, or diagonally. A node at one of the four corners is connected to 3 nodes, and every other node is connected to at most 8.
A lock pattern always has to form a spanning tree. A spanning tree is a set of edges of the graph that contains no closed loop. It also has to give a path between any two nodes, and it consists of vertices and edges. A single lock pattern admits many different spanning trees.
To count the spanning trees of a graph , first number every vertex . Then build the matrix this way.
- if , then is the number of edges incident to
- if , then is when there is an edge between and , and when there is none
The number of spanning trees is a cofactor of .
Here is the determinant of the matrix obtained by deleting row and column from . Every cofactor of has the same value, whichever and you pick.
For example, the matrix of the 2×2 pattern is as follows.
Taking and , the cofactor comes out like this.
So the 2×2 pattern has 16 spanning trees.
The matrix of the 3×3 pattern is as follows.
The cofactor of this matrix is the number of spanning trees of the 3×3 pattern.
Write a program that counts the spanning trees you can make on an pattern.
Input
The first line contains the number of test cases (). Each of the next lines holds one test case, the pattern size . ()
Output
For each test case, print on its own line the number of spanning trees you can make on the pattern.
Hint
The pattern is a graph of 4 vertices that are all connected to each other, and it has 16 spanning trees.