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 pb push_back
#define re resize
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define all1(x) (x).begin()+1, (x).end()
#define loop(i, n) for(int i = 0; i < n; i++)
#define loop1(i, n) for(int i = 1; i <= n; i++)
#define print(x) cout << #x << ": " << x << endl << flush
template<class T> bool ckmin(T&a, T b) { bool B = a > b; a = min(a, b); return B; }
template<class T> bool ckmax(T&a, T b) { bool B = a < b; a = max(a, b); return B; }
typedef long long ll;
typedef vector<ll> vl;
typedef vector<int> vi;
const int mod = 1e9+2022;
const int maxn = 1e3+5;
int n, m;
vi p, a;
vi adj[maxn];
void init(int N, int M, vi P, vi A) {
n = N; m = M; p = P; a = A;
loop(i, n+m) if(i)
adj[p[i]].pb(i);
}
int count_ways(int l, int r) {
for(int i = l-n; i <= r-n; i++) a[i] = !a[i];
ll res = 0;
vl ways1(n+m);
vl ways0(n+m);
auto dfs = [&] (int u, auto dfs) -> void {
if(n-1 < u) {
ways0[u] = !a[u-n];
ways1[u] = a[u-n];
return;
}
ll c = adj[u].size();
vl dp(c+1);
ll cnt = 0;
dp[0] = 1;
for(auto v : adj[u]) {
dfs(v, dfs);
cnt++;
for(int i = cnt; i > 0; i--)
dp[i] = (ways0[v] * dp[i]
+ ways1[v] * dp[i-1]) % mod;
dp[0] = (ways0[v] * dp[0]) % mod;
}
loop(i, c+1) {
ways1[u] += dp[i] * i;
ways1[u] %= mod;
ways0[u] += dp[i] * (c-i);
ways0[u] %= mod;
}
};
dfs(0, dfs);
res = ways1[0];
return res;
}
# | 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... |