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;
#define vec vector
#define int long long
const int MOD = 1'000'002'022;
const int MXN = 100'005;
int black[MXN];
int total[MXN];
int white[MXN];
vec<int32_t> par;
vec<int32_t> a;
vec<int> tree[MXN];
int n, m;
void compute(int u) {
black[u] = 0;
if(u >= n) {
total[u] = 1;
black[u] = a[u-n];
white[u] = 1 - a[u-n];
return;
}
// cerr << "PROCESSING: " << u << '\n';
total[u] = tree[u].size();
for(int v : tree[u]) {
total[u] *= total[v];
total[u] %= MOD;
}
int k = tree[u].size();
vec<vec<int>> h(k+1, vec<int>(k+1, 0));
h[0][0] = 1;
for(int i = 0; i<k; i++) {
h[i+1][k] = h[i][k] * white[tree[u][i]];
h[i+1][k] %= MOD;
for(int j = 0; j<k; j++) {
h[i+1][j] += h[i][j] * white[tree[u][i]];
h[i+1][j] %= MOD;
h[i+1][j+1] += h[i][j] * black[tree[u][i]];
h[i+1][j+1] %= MOD;
}
}
for(int i = 0; i<=k; i++) {
black[u] += h[k][i]*i;
black[u] %= MOD;
}
white[u] = total[u] + MOD - black[u];
white[u] %= MOD;
}
void dfs1(int u = 0) {
for(int v : tree[u]) {
dfs1(v);
}
compute(u);
// cerr << "BLACK: " << black[u] << " WHITE: " << white[u] << '\n';
}
void init(int32_t N, int32_t M, std::vector<int32_t> P, std::vector<int32_t> A) {
n = N;
m = M;
a = A;
par = P;
for(int i = 1; i<N+M; i++) {
tree[P[i]].push_back(i);
}
}
int32_t count_ways(int32_t L, int32_t R) {
if(false && L == R) {
a[L] = 1-a[L];
int cur = L;
while(cur != -1) {
compute(cur);
cur = par[cur];
}
}
else {
for(int i = L; i<=R; i++) {
a[i-n] = 1-a[i-n];
}
dfs1();
}
return black[0];
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |