#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.
题目大意
题目描述
Farmer John 有一个大农场,农场上有 个谷仓(),其中一些已经涂色,另一些尚未涂色。Farmer John 想要为这些剩余的谷仓涂色,使得所有谷仓都被涂色,但他只有三种可用的油漆颜色。此外,他的获奖奶牛 Bessie 如果发现两个直接相连的谷仓颜色相同,会感到困惑,因此他希望确保这种情况不会发生。
保证 个谷仓之间的连接不会形成任何“环”。也就是说,任意两个谷仓之间最多只有一条连接路径。
Farmer John 有多少种方式可以为剩余的未涂色谷仓涂色?
输入格式
第一行包含两个整数 和 (),分别表示农场上的谷仓数量和已经涂色的谷仓数量。
接下来的 行每行包含两个整数 和 (),描述直接连接谷仓 和 的路径。
接下来的 行每行包含两个整数 和 (, ),表示谷仓 已经被涂成颜色 。
输出格式
计算为剩余谷仓涂色的有效方式数量,模 ,要求任何两个直接相连的谷仓颜色不同。