Advertisement
Josif_tepe

Untitled

May 16th, 2023
685
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstring>
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6. typedef long long ll;
  7. const int maxn = 1e5 + 10;
  8. const ll MOD = 1e9 + 7;
  9. int n, k;
  10. vector<int> graph[maxn];
  11. int already_colored[maxn];
  12. ll dp[maxn][5];
  13. void dfs(int node, int prev) {
  14.     //cout << node << endl;
  15.     for(int i = 0; i < graph[node].size(); i++) {
  16.         int sosed = graph[node][i];
  17.         if(sosed == prev) continue;
  18.         dfs(sosed, node);
  19.     }
  20.     for(int color = 1; color <= 3; color++) {
  21.         if(already_colored[node] != color and already_colored[node] != -1) continue;
  22.             dp[node][color] = 1;
  23.  
  24.         for(int i = 0; i < graph[node].size(); i++) {
  25.             int sosed = graph[node][i];
  26.             if(sosed == prev) continue;
  27.            
  28.             ll sum = 0;
  29.             for(int next_color = 1; next_color <= 3; next_color++) {
  30.                 if(color == next_color) continue;
  31.                 sum += dp[sosed][next_color];
  32.                 sum %= MOD;
  33.             }
  34.             // cout << sum << endl;
  35.             dp[node][color] *= sum;
  36.             dp[node][color] %= MOD;
  37.            
  38.         }
  39.     }
  40. }
  41. int main() {
  42.     ifstream cin("barnpainting.in");
  43.     ofstream cout("barnpainting.out");
  44.     cin >> n >> k;
  45.     for(int i = 0; i < n - 1; i++) {
  46.         int a, b;
  47.         cin >> a >> b;
  48.         graph[a].push_back(b);
  49.         graph[b].push_back(a);
  50.     }
  51.     memset(already_colored, -1, sizeof already_colored);
  52.     for(int i = 0; i < k; i++) {
  53.         int b, c;
  54.         cin >> b >> c;
  55.         already_colored[b] = c;
  56.     }
  57.     dfs(1, -1);
  58.  
  59.   cout << (dp[1][1] + dp[1][2] + dp[1][3]) % MOD << endl;
  60.     return 0;
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement