eolymp
bolt
Try our new interface for solving problems
Problems

Hashigo Sama

Hashigo Sama

Chelsea is a modern artist. She decided to make her next work with ladders. She wants to combine some ladders and paint some beautiful pattern. A ladder can be considered as a graph called \textit{hashigo}. There are \textbf{n }\textit{hashigos} numbered from \textbf{0 }to \textbf{n - 1}. \textit{Hashigo} \textbf{i }of length \textbf{l_i} has \textbf{2l_i + 6 }vertices \textbf{v_\{i,0\}}, \textbf{v_\{i,1\}}, ..., \textbf{v_\{i,2li+5\}} and has edges between the pair of vertices (\textbf{v_\{i,j\}}, \textbf{v_\{i,j+2\}}) (\textbf{0 }≤ \textbf{j }≤ \textbf{2l_i + 3}) and (\textbf{v_\{i,2j\}}, \textbf{v_\{i,2j+1\}}) (\textbf{1 }≤ \textbf{j }≤ \textbf{l_i + 1}). The gure below is example of a \textit{hashigo} of length \textbf{2}. This corresponds to the graph given in the first dataset in the sample input. \includegraphics{https://static.e-olymp.com/content/5c/5cd2d4f737e7aa82ea7260774de4af0dc60a12d3.jpg} Two \textit{hashigos }\textbf{i }and \textbf{j }are combined at position \textbf{p }(\textbf{0 }≤ \textbf{p }≤ \textbf{l_i - 1}) and \textbf{q }(\textbf{0 }≤ \textbf{q }≤ \textbf{l_j - 1}) by marged each pair of vertices (\textbf{v_\{i,2p+2\}}, \textbf{v_\{j,2q+2\}}), (\textbf{v_\{i,2p+3\}}, \textbf{v_\{j,2q+4\}}), (\textbf{v_\{i,2p+4\}}, \textbf{v_\{j,2q+3\}}) and (\textbf{v_\{i,2p+5\}}, \textbf{v_\{j,2q+5\}}). Chelsea performs this operation \textbf{n - 1 }times to combine the \textbf{n }\textit{hashigos}. After this operation, the graph should be connected and the maximum degree of the graph should not exceed \textbf{4}. The gure below is a example of the graph obtained by combining three \textit{hashigos}. This corresponds to the graph given in the second dataset in the sample input. \includegraphics{https://static.e-olymp.com/content/4c/4cf0cccd462223cc3bf4d80be476fe679b1cefee.jpg} Now she decided to paint each vertex by black or white with satisfying the following condition: \begin{itemize} \item The maximum components formed by the connected vertices painted by the same color is less than or equals to \textbf{k}. \end{itemize} She would like to try all the patterns and choose the best. However, the number of painting way can be very huge. Since she is not good at math nor computing, she cannot calculate the number. So please help her with your superb programming skill! \InputFile The input contains several datasets, and each dataset is in the following format. \textbf{n k} \textbf{l_0 l_1 ... l_\{n-1\}} \textbf{f_0 p_0 t_0 q_0} \textbf{...} \textbf{f_\{n-2\} p_\{n-2\} t_\{n-2\} q_\{n-\}2} The first line contains two integers \textbf{n }(\textbf{1 }≤ \textbf{n }≤ \textbf{30}) and \textbf{k }(\textbf{1 }≤ \textbf{k }≤ \textbf{8}). The next line contains \textbf{n }integers \textbf{l_i} (\textbf{1 }≤ \textbf{l_\{i \}}≤ \textbf{30}), each denotes the length of \textit{hashigo }\textbf{i}. The following \textbf{n - 1 }lines each contains four integers \textbf{f_i} (\textbf{0 }≤ \textbf{f_\{i \}}≤ \textbf{n - 1}), \textbf{p_i} (\textbf{0 }≤ \textbf{p_\{i \}}≤ \textbf{l_\{fi-1\}}), \textbf{t_i }(\textbf{0 }≤ \textbf{t_i} ≤ \textbf{n - 1}), \textbf{q_i} (\textbf{0 }≤ \textbf{q_i} ≤ \textbf{l_\{ti-1\}}). It represents the \textit{hashigo }\textbf{f_i} and the \textit{hashigo }\textbf{t_i} are combined at the position \textbf{p_i} and the position \textbf{q_i}. You may assume that the graph obtained by combining \textbf{n }\textit{hashigos} is connected and the degree of each vertex of the graph does not exceed \textbf{4}. The last dataset is followed by a line containing two zeros. \OutputFile For each dataset, print the number of different colorings modulo \textbf{1000000007} in a line.
Time limit 20 seconds
Memory limit 256 MiB
Input example #1
1 5
2
3 7
2 3 1
0 1 1 0
1 2 2 0
2 8
5 6
0 2 1 2
2 8
1 1
0 0 1 0
2 2
2 2
0 1 1 0
2 3
3 3
0 2 1 1
2 4
3 1
1 0 0 1
0 0
Output example #1
708
1900484
438404500
3878
496
14246
9768
Source 2012 JAG Practice Contest for ACM-ICPC Asia Regional, AtCoder, November 4; 2013 Petrozavodsk Winter Training Camp, January 26, Problem D