# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
720372 | 2023-04-08T05:40:34 Z | ismayil | Digital Circuit (IOI22_circuit) | C++17 | 0 ms | 0 KB |
#include "circuit.h" #include <bits/stdc++.h> #define int long long #define endl '\n' using namespace std; const int MAX = 3e3; int dp[2][MAX]; vector<int> adj[MAX]; int n, m; void init(int N, int M, vector<int> P, vector<int> A) { n = N, m = M; for(int i = 0; i < N + M; i++){ if(P[i] == -1) continue; adj[P[i]].push_back(i); } for(int i = 0; i < M; i++){ dp[A[i]][i + n] = 1; } } void dfs(int u){ if(u >= n) return; int l = -1, r = -1; for(auto v : adj[u]){ dfs(v); if(l == -1) l = v; else r = v; } dp[0][u] = dp[0][l] * dp[1][r] + dp[1][l] * dp[0][r] + 2 * dp[0][l] * dp[0][r]; dp[1][u] = dp[0][l] * dp[1][r] + dp[1][l] * dp[0][r] + 2 * dp[1][l] * dp[1][r]; } int count_ways(int L, int R) { for(int i = L; i <= R; i++){ dp[0][i] = 1 - dp[0][i]; dp[1][i] = 1 - dp[1][i]; } dfs(0); return dp[1][0]; }