Advertisement
Vince14

Barn Painting

Feb 26th, 2023
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.48 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <vector>
  7. #include <set>
  8. #include <map>
  9. #include <stack>
  10. #include <queue>
  11. #include <deque>
  12. #include <unordered_map>
  13. #include <numeric>
  14. #include <iomanip>
  15. #include <regex>
  16. using namespace std;
  17. #define pii pair<int, int>
  18. #define FAST ios_base::sync_with_stdio(false); cin.tie(NULL)
  19. const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
  20. const int dl[2] = {1, -1};
  21. const int MOD = 1e9 + 7;
  22. const int MAX = 100005;
  23.  
  24. int n, k;
  25. vector<int> v[MAX];
  26. long long dp[MAX][3];
  27. vector<int> child[MAX];
  28. bool zero[MAX][3];
  29.  
  30. void make_tree(int cur, int par){
  31.     for(auto nxt : v[cur]){
  32.         if(nxt != par){
  33.             child[cur].push_back(nxt);
  34.             make_tree(nxt, cur);
  35.         }
  36.     }
  37.     if(child[cur].size() == 0){
  38.         for(int i = 0; i < 3; i++){
  39.             if(dp[cur][i] != -1){
  40.                 dp[cur][i] = 1;
  41.             }
  42.         }
  43.     }
  44. }
  45.  
  46. long long DP(int cur, int color){
  47.     if(zero[cur][color]){
  48.         return dp[cur][color] = 0;
  49.     }
  50.     if(dp[cur][color] != -1){
  51.         return dp[cur][color];
  52.     }
  53.     dp[cur][color] = 1;
  54.     for(auto nxt : child[cur]){
  55.         long long tmp = 0;
  56.         for(int i = 0; i < 3; i++) {
  57.             if(i == color){
  58.                 continue;
  59.             }
  60.             tmp += DP(nxt,i);
  61.             tmp %= MOD;
  62.         }
  63.         dp[cur][color] *= tmp;
  64.         dp[cur][color] %= MOD;
  65.     }
  66.     return dp[cur][color];
  67. }
  68.  
  69. int main() {
  70.     FAST;
  71.     freopen("barnpainting.in", "r", stdin);
  72.     freopen("barnpainting.out", "w", stdout);
  73.     cin >> n >> k;
  74.     for(int i = 1; i <= n; i++){
  75.         for(int j = 0; j < 3; j++){
  76.             dp[i][j] = -1;
  77.         }
  78.     }
  79.     for(int x, y, i = 0; i < n - 1; i++){
  80.         cin >> x >> y;
  81.         v[x].push_back(y);
  82.         v[y].push_back(x);
  83.     }
  84.     for(int x, color, i = 0; i < k; i++){
  85.         cin >> x >> color;
  86.         color--;
  87.         for(int j = 0; j < 3; j++){
  88.             if(j != color){
  89.                 zero[x][j] = true;
  90.             }
  91.         }
  92.     }
  93.     make_tree(1, -1);
  94.     /*
  95.     for(int i = 1; i <= n; i++){
  96.         cout << i << ": ";
  97.         for(auto x : child[i]){
  98.             cout << x << " ";
  99.         }
  100.         cout << "\n";
  101.     }
  102.     */
  103.     long long ans = 0;
  104.     for(int i = 0; i < 3; i++){
  105.         ans += DP(1, i);
  106.         ans %= MOD;
  107.     }
  108.     cout << ans << "\n";
  109.  
  110. }
  111.  
  112. /*
  113. 4 1
  114. 1 2
  115. 1 3
  116. 1 4
  117. 4 3
  118.  
  119.  8
  120.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement