#P3914. 染色计数

    ID: 4631 远端评测题 3000ms 512MiB 尝试: 0 已通过: 0 显示难度普及+/提高− 上传者: 标签>动态规划 DP数学枚举深度优先搜索 DFS

染色计数

Problem Description

There is a tree with NN nodes, numbered 1,2,⋯ ,N1, 2, \cdots, N. You need to color it so that adjacent nodes have different colors. There are MM colors, numbered 1,2,⋯ ,M1, 2, \cdots, M. Each node may be colored with a subset of the MM colors. Compute the number of different valid colorings modulo 109+710^9 + 7.

Input Format

The first line contains two integers N,MN, M.

Then follow NN lines. The ii-th line describes the colors allowed for node ii: the first integer kik_i is the number of allowed colors, followed by kik_i integers giving the allowed color indices.

Finally, the last N−1N - 1 lines each contain two integers Ai,BiA_i, B_i, indicating an edge (Ai,Bi)(A_i, B_i).

Output Format

Output one integer, the number of valid colorings modulo 109+710^9 + 7.

2 2
1 1
2 1 2
1 2
1

Hint

• For 30% of the testdata, 1≤N≤10;1≤M≤41 \le N \le 10; 1 \le M \le 4.

• For 60% of the testdata, 1≤N≤200;1≤M≤2001 \le N \le 200; 1 \le M \le 200.

• For 100% of the testdata, 1≤N≤5000;1≤M≤50001 \le N \le 5000; 1 \le M \le 5000.

Translated by ChatGPT 5