#P4084. [USACO17DEC] Barn Painting G
[USACO17DEC] Barn Painting G
题目描述
Farmer John has a large farm with barns (), some of which are already painted and some not yet painted. Farmer John wants to paint these remaining barns so that all the barns are painted, but he only has three paint colors available. Moreover, his prize cow Bessie becomes confused if two barns that are directly reachable from one another are the same color, so he wants to make sure this situation does not happen.
It is guaranteed that the connections between the barns do not form any 'cycles'. That is, between any two barns, there is at most one sequence of connections that will lead from one to the other.
How many ways can Farmer John paint the remaining yet-uncolored barns?
输入格式
The first line contains two integers and (), respectively the number of barns on the farm and the number of barns that have already been painted.
The next lines each contain two integers and () describing a path directly connecting barns and .
The next lines each contain two integers and (, ) indicating that barn is painted with color .
输出格式
Compute the number of valid ways to paint the remaining barns, modulo , such that no two barns which are directly connected are the same color.
题目大意
题意:给定一颗 个节点组成的树,你要给每个点涂上三种颜色之一,其中有 个节点已染色,要求任意两相邻节点颜色不同,求合法染色方案数。答案对 取模。
4 1
1 2
1 3
1 4
4 3
8