F.A.Qs Home Discuss ProblemSet Status Ranklist Contest 入门OJ LoginRegister 捐赠本站
Notice:1:注册本OJ方式请见https://www.lydsy.com/JudgeOnline/wttl/thread.php?tid=5671
Problem 5141. -- [Usaco2017 Dec]Barn Painting

5141: [Usaco2017 Dec]Barn Painting

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 65  Solved: 43
[Submit][Status][Discuss]

Description

Farmer John has a large farm with N barns (1≤N≤10^5), some of which are already painted and some n
ot 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 thi
s situation does not happen.It is guaranteed that the connections between the N barns do not form an
y 'cycles'. That is, between any two barns, there is at most one sequence of connections that will l
ead from one to the other.How many ways can Farmer John paint the remaining yet-uncolored barns?
给你一颗大小为n<=1e5的树,三种颜色,每个点涂一种颜色,相邻点不能同色。下面告诉你一些点已经被涂色,并
且告诉你是哪一种颜色,问你涂完整棵树有多少种方法?mod 1e9+7。

Input

The first line contains two integers N and K (0≤K≤N), respectively the number of barns on the farm 
and the number of barns that have already been painted.
The next N-1lines each contain two integers xx and yy (1≤x,y≤N,x≠y) describing a path directly connecting barns x and y.
The next K lines each contain two integers bb and cc (1≤b≤N, 1≤c≤3) indicating that barn bb is painted with color c.

Output

Compute the number of valid ways to paint the remaining barns, modulo 10^9+7, 
such that no two barns which are directly connected are the same color.

Sample Input

4 1
1 2
1 3
1 4
4 3

Sample Output

8

HINT

Source

Gold

[Submit][Status][Discuss]

HOME Back