#732. 谷仓染色

谷仓染色

题目描述

From USACO17DEC

FJ has a large farm with NN barns (1N1051 \le N \le 10^5), 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 NN 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个谷仓(N105N≤1​0^5​),保证所有相连的谷仓不会形成环,这也就是说对于任意两个谷仓,只有一条路径可以达到。

在这N个谷仓中,有一些是已经被染色的谷仓,而有一些则还没有。FJ想要把这些没有染色的谷仓进行染色,这样农场中的N个谷仓就都被染色啦。

但是现在FJ只有三种可以使用的颜色。除此以外,如果两个相邻的谷仓是染的相同的染色,这会让农场中的奶牛Bessie非常的困惑,因此Farmer John必须保证两两相邻的谷仓颜色不同。

请问FJ有多少种染色的方案?

输入格式

The first line contains two integers NN and KK (0KN0 \le K \le N), respectively the number of barns on the farm and the number of barns that have already been painted.

The next N1N-1 lines each contain two integers xx and yy (1x,yN,xy1 \le x, y \le N, x \neq y) describing a path directly connecting barns xx and yy.

The next KK lines each contain two integers bb and cc (1bN1 \le b \le N, 1c31 \le c \le 3) indicating that barn bb is painted with color cc.

第一行两个用空格分离的正整数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 109+710^9 + 7, such that no two barns which are directly connected are the same color.

输出染色的方案模 109+710^9 + 7,的值。

样例 #1

样例输入 #1

4 1
1 2
1 3
1 4
4 3

样例输出 #1

8

数据范围

对于30%的数据:N≤10;

对于另10%的数据:N≤1000;

对于100%的数据:N≤10^5^