Hashigo Sama
Time limit8sMemory limit256 MB
Count black-white colorings of joined ladders where each monochrome block has size at most k, modulo 1,000,000,007.
- Level
Hard9 of 10
- Topics
- Dynamic programming, Graph
- Solved
- No attempts yet
Problem
Chelsea is a modern artist. She decided to build her next work out of ladders. She joins several ladders together and then paints a pattern on the result.
One ladder is described by a graph called a hashigo. There are hashigos, numbered 0 through . Hashigo has length and consists of the vertices . Its edges come in two kinds.
- for every with
- for every with
The even numbered vertices are linked in the order and form one rail, and the odd numbered vertices are linked in the order and form the other rail. The two rails are connected by rungs, and the four end vertices , , , carry no rung.
Hashigo and hashigo are combined at position () and position () by merging each of the following four pairs into a single vertex.
Chelsea performs this operation times to combine all hashigos. After the operations the graph is connected and no vertex has degree greater than 4.
She now paints every vertex black or white, subject to one condition.
- Every connected component formed by adjacent vertices of the same color has size at most .
She would like to inspect every pattern and pick the best one, but the number of patterns is very large. Count the colorings that satisfy the condition.
Input
The input contains several datasets. Each dataset has the following format.
n k
l0 l1 ... ln-1
f0 p0 t0 q0
...
fn-2 pn-2 tn-2 qn-2
The first line contains two integers () and ().
The next line contains integers (), the length of hashigo .
Each of the following lines contains four integers (), (), (), (). It means that hashigo and hashigo are combined at position and position . You may assume that the graph obtained by combining the hashigos is connected and that no vertex of the graph has degree greater than 4.
The last dataset is followed by a line containing two zeros.
Output
For each dataset, print the number of different colorings modulo 1,000,000,007 on one line.