#P3914. 染色计数

    ID: 2851 远端评测题 3000ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>动态规划,dp数学枚举,暴力深度优先搜索,DFS

染色计数

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 N1N - 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, 1N10;1M41 \le N \le 10; 1 \le M \le 4.

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

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

Translated by ChatGPT 5