-
Notifications
You must be signed in to change notification settings - Fork 0
/
barnpainting.cpp
55 lines (44 loc) · 1010 Bytes
/
barnpainting.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <algorithm>
#include <vector> // move exlipose file
#define MOD 1000000007
using namespace std;
vector<int> adj[100000];
int colored[100000];
int N, K;
long dp[100000][3];
long solve(int n, int c, int p) {
if(colored[n] != -1 && colored[n] != c) dp[n][c] = 0;
if (dp[n][c] != -1) return dp[n][c];
dp[n][c] = 1;
for(int child: adj[n]) {
if(child == p) continue;
long ways = 0;
for(int color = 0; color < 3; color++) {
if(color != c) ways += solve(child, color, n);
ways %= MOD;
}
dp[n][c] *= ways;
dp[n][c] %= MOD;
return dp[n][c];
}
int main () {
cin >> N >> K;
for(int i = 0; i < N-1; i++) {
int x, y;
cin >> x >> y;
x--; y--;
adj[x].push_back(y);
adj[y].push_back(x);
}
for(int i = 0; i < N; i++) {
colored[i] = -1;
for(int j = 0; j < 3; j++) dp[i][j] = -1;
}
for(int i = 0; i < K; i++) {
int b, c;
cin >> b >> c;
colored[b-1] = c-1;
}
cout << (solve(0, 0, -1) + solve(0, 1, -1) + solve(0, 2, -1))%MOD << endl;
}