# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
785796 | Andrey | Digital Circuit (IOI22_circuit) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "circuit.h"
#include<bits/stdc++.h>
using namespace std;
vector<int> haha[300001];
vector<long long> wut[300001];
vector<long long> bruh(300001);
vector<long long> dp(300001);
vector<long long> prod(300001);
const long long MOD = 1e9+2022;
long long n,m;
void dfs(long long a, long long t) {
long long br = 0,pr = 1,sb = 0,yeah = 0;
for(long long i = 0; i < wut[a].size(); i++) {
if(bruh[wut[a][i]]) {
br++;
}
yeah++;
}
for(v: haha[a]) {
if(v != t) {
dfs(v,a);
sb = (sb*prod[v]+dp[v]*pr)%MOD;
pr*=prod[v];
pr%=MOD;
yeah++;
}
}
prod[a] = (pr*yeah)%MOD;
dp[a] = (sb+br*pr)%MOD;
}
void init(int N, int M, vector<int> p, vector<int> a) {
n = N;
m = M;
for(long long i = n; i < n+m; i++) {
bruh[i] = a[i-n];
}
for(long long i = 1; i < n; i++) {
haha[i].push_back(p[i]);
haha[p[i]].push_back(i);
}
for(long long i = n; i < n+m; i++) {
wut[p[i]].push_back(i);
}
}
int count_ways(int l, int r) {
for(long long i = l; i <= r; i++) {
if(bruh[i]) {
bruh[i] = 0;
}
else {
bruh[i] = 1;
}
}
dfs(0,-1);
return dp[0];
}