#732. 谷仓染色
谷仓染色
题目描述
From USACO17DEC
FJ has a large farm with barns (), some of which are already painted and some not yet painted.FJ 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 FJ paint the remaining yet-uncolored barns?
题面翻译
FJ 有一个巨大的农场,在这个农场中有N个谷仓(),保证所有相连的谷仓不会形成环,这也就是说对于任意两个谷仓,只有一条路径可以达到。
在这N个谷仓中,有一些是已经被染色的谷仓,而有一些则还没有。FJ想要把这些没有染色的谷仓进行染色,这样农场中的N个谷仓就都被染色啦。
但是现在FJ只有三种可以使用的颜色。除此以外,如果两个相邻的谷仓是染的相同的染色,这会让农场中的奶牛Bessie非常的困惑,因此Farmer John必须保证两两相邻的谷仓颜色不同。
请问FJ有多少种染色的方案?
输入格式
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 .
第一行两个用空格分离的正整数N 和 K,代表N 个谷仓和K个已经被染色的谷仓。
接下来 n-1 行,每行两个用空格分离的正整数x 和y ,表示谷仓 x和谷仓 y相连。
接下来K行,每行两个用空格分离的正整数b 和 c,表示谷仓b被染了c这种颜色。
输出格式
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.
输出染色的方案模 ,的值。
样例 #1
样例输入 #1
4 1
1 2
1 3
1 4
4 3
样例输出 #1
8
数据范围
对于30%的数据:N≤10;
对于另10%的数据:N≤1000;
对于100%的数据:N≤10^5^
Statistics
Related
In following homework: