Advertisement
leoanjos

Gates of Uncertainty

Oct 14th, 2021
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.59 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define long long long int
  6.  
  7. const int MOD = 1e9 + 7;
  8.  
  9. vector<tuple<int, int, int>> tree;
  10. vector<vector<vector<long>>> memo;
  11.  
  12. long count(int should, int does, int node = 1) {
  13.     if (!node) return should == does;
  14.  
  15.     auto [left, right, F] = tree[node];
  16.     if (F != -1 && F != does) return 0LL;
  17.  
  18.     long &ans = memo[should][does][node];
  19.     if (~ans) return ans;
  20.  
  21.     if (F == -1) {
  22.         if (!should && !does) {
  23.             ans = count(1, 1, left) * count(1, 1, right);
  24.             ans %= MOD;
  25.         } else if (!should) {
  26.             ans = 0LL;
  27.             for (int i = 0; i <= 1; i++)
  28.                 for (int j = 0; j <= 1 - i; j++) {
  29.                     ans += (count(1, i, left) * count(1, j, right)) % MOD;
  30.                     ans %= MOD;
  31.                 }
  32.         } else if (!does) {
  33.             ans = 0LL;
  34.             for (int i = 0; i <= 1; i++)
  35.                 for (int j = 0; j <= 1 - i; j++) {
  36.                     ans += (count(i, 1, left) * count(j, 1, right)) % MOD;
  37.                     ans %= MOD;
  38.                 }
  39.         } else {
  40.             ans = 0LL;
  41.             for (int i = 0; i <= 1; i++)
  42.                 for (int j = 0; j <= 1 - i; j++)
  43.                     for (int k = 0; k <= 1; k++)
  44.                         for (int l = 0; l <= 1 - k; l++) {
  45.                             ans += (count(i, k, left) * count(j, l, right)) % MOD;
  46.                             ans %= MOD;
  47.                         }
  48.         }
  49.     } else {
  50.         if (!should) {
  51.             ans = 0LL;
  52.             for (int i = 0; i <= 1; i++)
  53.                 for (int j = 0; j <= 1; j++) {
  54.                     ans += (count(1, i, left) * count(1, j, right)) % MOD;
  55.                     ans %= MOD;
  56.                 }
  57.         } else {
  58.             ans = 0LL;
  59.             for (int i = 0; i <= 1; i++)
  60.                 for (int j = 0; j <= 1 - i; j++)
  61.                     for (int k = 0; k <= 1; k++)
  62.                         for (int l = 0; l <= 1; l++) {
  63.                             ans += (count(i, k, left) * count(j, l, right)) % MOD;
  64.                             ans %= MOD;
  65.                         }
  66.         }
  67.     }
  68.  
  69.     return ans;
  70. }
  71.  
  72. int main() {
  73.     ios_base::sync_with_stdio(false);
  74.     cin.tie(NULL);
  75.  
  76.     int N; cin >> N;
  77.  
  78.     tree.resize(N + 1);
  79.     for (int i = 1; i <= N; i++) {
  80.         int X, Y, F;
  81.         cin >> X >> Y >> F;
  82.         tree[i] = make_tuple(X, Y, F);
  83.     }
  84.  
  85.     memo.assign(2, vector<vector<long>>(2, vector<long>(N + 5, -1LL)));
  86.  
  87.     long ans = (count(0, 1) + count(1, 0)) % MOD;
  88.     cout << ans << "\n";
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement