#P8280. 「MCOI-08」Photoelectric Effect

    ID: 6506 远端评测题 1000ms 256MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>洛谷原创O2优化状态压缩,状压树形 dp洛谷月赛

「MCOI-08」Photoelectric Effect

Description

There is a tree with nn (1n1051 \le n \le 10^5) nodes and kk (2k52 \le k \le 5) colors, with node 11 as the root. Meanwhile, a color merge function aba \otimes b is given, satisfying that for 1a,bk1 \le a, b \le k, we have 1abk1 \le a \otimes b \le k.

How many ways are there to color all nodes such that for any pair of nodes u,vu, v that have no ancestor relationship, the following holds:

  • The color of LCA(u,v)\mathrm{LCA}(u, v) equals the merge of the color of node uu and the color of node vv.

Output the answer modulo 109+710^9 + 7.

Input Format

This problem contains multiple test cases. The first line contains a positive integer tt (1t1031 \le t \le 10^3), the number of test cases. Then follow tt test cases, and for each test case:

The first line contains two positive integers n,kn, k.

The next kk lines each contain kk positive integers. The jj-th number in the ii-th line is the value of iji \otimes j.

The next line contains n1n - 1 positive integers f2,f3,,fnf_2, f_3, \dots, f_n, where fif_i is the parent of node ii.

Output Format

For each test case:

Output one integer, representing the answer.

2
5 2
1 2
2 1
1 2 1 4
5 2
1 2
1 1
1 2 1 4
4
2

Hint

Explanation for Sample 1

The tree looks as follows:

Let wiw_i be the weight of node ii. Then there are the following 44 assignment methods:

  • wi={1,1,1,1,1}w_i = \{1, 1, 1, 1, 1\};
  • wi={2,2,2,1,1}w_i = \{2, 2, 2, 1, 1\};
  • wi={2,1,1,2,2}w_i = \{2, 1, 1, 2, 2\};
  • wi={1,2,2,2,2}w_i = \{1, 2, 2, 2, 2\}.

Constraints and Notes

This problem uses bundled testdata.

For 100%100\% of the testdata, 1n,n1051 \le n, \sum n \le 10^5, 2k52 \le k \le 5, and 1fi<i1 \le f_i < i.

For 100%100\% of the testdata, 1t10001 \le t \le 1000.

  • Subtask 1 (5 pts): n5n \le 5;
  • Subtask 2 (11 pts): each node has at most 22 children in the tree;
  • Subtask 3 (23 pts): each node has at most 33 children in the tree;
  • Subtask 4 (13 pts): k=2k = 2;
  • Subtask 5 (17 pts): k3k \le 3;
  • Subtask 6 (31 pts): no special constraints.

Translated by ChatGPT 5