Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cstring>
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- const int maxn = 1e5 + 10;
- const ll MOD = 1e9 + 7;
- int n, k;
- vector<int> graph[maxn];
- int already_colored[maxn];
- ll dp[maxn][5];
- void dfs(int node, int prev) {
- //cout << node << endl;
- for(int i = 0; i < graph[node].size(); i++) {
- int sosed = graph[node][i];
- if(sosed == prev) continue;
- dfs(sosed, node);
- }
- for(int color = 1; color <= 3; color++) {
- if(already_colored[node] != color and already_colored[node] != -1) continue;
- dp[node][color] = 1;
- for(int i = 0; i < graph[node].size(); i++) {
- int sosed = graph[node][i];
- if(sosed == prev) continue;
- ll sum = 0;
- for(int next_color = 1; next_color <= 3; next_color++) {
- if(color == next_color) continue;
- sum += dp[sosed][next_color];
- sum %= MOD;
- }
- // cout << sum << endl;
- dp[node][color] *= sum;
- dp[node][color] %= MOD;
- }
- }
- }
- int main() {
- ifstream cin("barnpainting.in");
- ofstream cout("barnpainting.out");
- cin >> n >> k;
- for(int i = 0; i < n - 1; i++) {
- int a, b;
- cin >> a >> b;
- graph[a].push_back(b);
- graph[b].push_back(a);
- }
- memset(already_colored, -1, sizeof already_colored);
- for(int i = 0; i < k; i++) {
- int b, c;
- cin >> b >> c;
- already_colored[b] = c;
- }
- dfs(1, -1);
- cout << (dp[1][1] + dp[1][2] + dp[1][3]) % MOD << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement