#include <bits/stdc++.h>
#include "circuit.h"
using namespace std;
const int N = 2e5 + 5, mod = 1000002022;
vector<int> adj[N];
int64_t dp[N][2], color[N];
vector<int> par;
void dfs(int u) {
if(adj[u].size() == 0) {
if(color[u]) dp[u][0] = 0, dp[u][1] = 1;
else dp[u][0] = 1, dp[u][1] = 0;
return;
}
for(int v: adj[u]) dfs(v);
vector<vector<int64_t>> D((int)adj[u].size() + 1, vector<int64_t>((int)adj[u].size() + 1));
D[0][0] = 1;
for(int i = 0; i < (int)adj[u].size(); ++i) {
for(int j = 0; j <= i; ++j) {
D[i + 1][j] += D[i][j] * dp[adj[u][i]][0] % mod;
D[i + 1][j] %= mod;
D[i + 1][j + 1] += D[i][j] * dp[adj[u][i]][1] % mod;
D[i + 1][j + 1] %= mod;
}
}
dp[u][0] = dp[u][1] = 0;
for(int i = 0; i <= (int)adj[u].size(); ++i) {
dp[u][1] += D[(int)adj[u].size()][i] * i % mod;
dp[u][1] %= mod;
dp[u][0] += D[(int)adj[u].size()][i] * ((int)adj[u].size() - i) % mod;
dp[u][0] %= mod;
}
}
void calc(int u) {
if(adj[u].size() == 0) {
if(color[u]) dp[u][0] = 0, dp[u][1] = 1;
else dp[u][0] = 1, dp[u][1] = 0;
return;
}
vector<vector<int64_t>> D((int)adj[u].size() + 1, vector<int64_t>((int)adj[u].size() + 1));
D[0][0] = 1;
for(int i = 0; i < (int)adj[u].size(); ++i) {
for(int j = 0; j <= i; ++j) {
D[i + 1][j] += D[i][j] * dp[adj[u][i]][0] % mod;
D[i + 1][j] %= mod;
D[i + 1][j + 1] += D[i][j] * dp[adj[u][i]][1] % mod;
D[i + 1][j + 1] %= mod;
}
}
dp[u][0] = dp[u][1] = 0;
for(int i = 0; i <= (int)adj[u].size(); ++i) {
dp[u][1] += D[(int)adj[u].size()][i] * i % mod;
dp[u][1] %= mod;
dp[u][0] += D[(int)adj[u].size()][i] * ((int)adj[u].size() - i) % mod;
dp[u][0] %= mod;
}
}
void init(int N, int M, std::vector<int> P, std::vector<int> A) {
par = P;
for(int i = 1; i < N + M; ++i) {
adj[P[i]].push_back(i);
}
for(int i = 0; i < M; ++i) color[i + N] = A[i];
dfs(0);
}
int count_ways(int L, int R) {
for(int i = L; i <= R; ++i) {
color[i] ^= 1;
}
if(L == R) {
int u = L;
while(u != 0) {
calc(u);
u = par[u];
}
calc(0);
} else {
dfs(0);
}
return dp[0][1];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
35 ms |
12508 KB |
Output is correct |
4 |
Correct |
32 ms |
12964 KB |
Output is correct |
5 |
Correct |
29 ms |
12972 KB |
Output is correct |
6 |
Correct |
30 ms |
12992 KB |
Output is correct |
7 |
Correct |
34 ms |
12996 KB |
Output is correct |
8 |
Correct |
29 ms |
12964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
4 ms |
4944 KB |
Output is correct |
4 |
Correct |
3 ms |
4944 KB |
Output is correct |
5 |
Correct |
3 ms |
4996 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
4 ms |
5072 KB |
Output is correct |
8 |
Correct |
4 ms |
5072 KB |
Output is correct |
9 |
Correct |
3 ms |
5072 KB |
Output is correct |
10 |
Correct |
3 ms |
5200 KB |
Output is correct |
11 |
Correct |
4 ms |
5200 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
35 ms |
12508 KB |
Output is correct |
4 |
Correct |
32 ms |
12964 KB |
Output is correct |
5 |
Correct |
29 ms |
12972 KB |
Output is correct |
6 |
Correct |
30 ms |
12992 KB |
Output is correct |
7 |
Correct |
34 ms |
12996 KB |
Output is correct |
8 |
Correct |
29 ms |
12964 KB |
Output is correct |
9 |
Correct |
2 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
4944 KB |
Output is correct |
11 |
Correct |
4 ms |
4944 KB |
Output is correct |
12 |
Correct |
3 ms |
4944 KB |
Output is correct |
13 |
Correct |
3 ms |
4996 KB |
Output is correct |
14 |
Correct |
3 ms |
5072 KB |
Output is correct |
15 |
Correct |
4 ms |
5072 KB |
Output is correct |
16 |
Correct |
4 ms |
5072 KB |
Output is correct |
17 |
Correct |
3 ms |
5072 KB |
Output is correct |
18 |
Correct |
3 ms |
5200 KB |
Output is correct |
19 |
Correct |
4 ms |
5200 KB |
Output is correct |
20 |
Correct |
3 ms |
5072 KB |
Output is correct |
21 |
Correct |
4 ms |
5072 KB |
Output is correct |
22 |
Correct |
3 ms |
5072 KB |
Output is correct |
23 |
Correct |
3 ms |
4944 KB |
Output is correct |
24 |
Correct |
3 ms |
5072 KB |
Output is correct |
25 |
Correct |
4 ms |
5072 KB |
Output is correct |
26 |
Correct |
4 ms |
5048 KB |
Output is correct |
27 |
Correct |
5 ms |
5072 KB |
Output is correct |
28 |
Correct |
3 ms |
5072 KB |
Output is correct |
29 |
Correct |
39 ms |
12972 KB |
Output is correct |
30 |
Correct |
28 ms |
12960 KB |
Output is correct |
31 |
Correct |
3 ms |
5200 KB |
Output is correct |
32 |
Correct |
4 ms |
5064 KB |
Output is correct |
33 |
Correct |
3 ms |
4976 KB |
Output is correct |
34 |
Correct |
3 ms |
4944 KB |
Output is correct |
35 |
Correct |
7 ms |
5440 KB |
Output is correct |
36 |
Correct |
3 ms |
5200 KB |
Output is correct |
37 |
Correct |
40 ms |
13332 KB |
Output is correct |
38 |
Correct |
29 ms |
13180 KB |
Output is correct |
39 |
Correct |
3 ms |
5072 KB |
Output is correct |
40 |
Correct |
4 ms |
5072 KB |
Output is correct |
41 |
Correct |
4 ms |
4992 KB |
Output is correct |
42 |
Correct |
3 ms |
5012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
716 ms |
8344 KB |
Output is correct |
2 |
Correct |
853 ms |
11632 KB |
Output is correct |
3 |
Correct |
1118 ms |
11640 KB |
Output is correct |
4 |
Correct |
995 ms |
11612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
716 ms |
8344 KB |
Output is correct |
2 |
Correct |
853 ms |
11632 KB |
Output is correct |
3 |
Correct |
1118 ms |
11640 KB |
Output is correct |
4 |
Correct |
995 ms |
11612 KB |
Output is correct |
5 |
Execution timed out |
3087 ms |
8312 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
4 ms |
4944 KB |
Output is correct |
4 |
Correct |
3 ms |
4944 KB |
Output is correct |
5 |
Correct |
3 ms |
4996 KB |
Output is correct |
6 |
Correct |
3 ms |
5072 KB |
Output is correct |
7 |
Correct |
4 ms |
5072 KB |
Output is correct |
8 |
Correct |
4 ms |
5072 KB |
Output is correct |
9 |
Correct |
3 ms |
5072 KB |
Output is correct |
10 |
Correct |
3 ms |
5200 KB |
Output is correct |
11 |
Correct |
4 ms |
5200 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
13 |
Correct |
716 ms |
8344 KB |
Output is correct |
14 |
Correct |
853 ms |
11632 KB |
Output is correct |
15 |
Correct |
1118 ms |
11640 KB |
Output is correct |
16 |
Correct |
995 ms |
11612 KB |
Output is correct |
17 |
Execution timed out |
3087 ms |
8312 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
35 ms |
12508 KB |
Output is correct |
4 |
Correct |
32 ms |
12964 KB |
Output is correct |
5 |
Correct |
29 ms |
12972 KB |
Output is correct |
6 |
Correct |
30 ms |
12992 KB |
Output is correct |
7 |
Correct |
34 ms |
12996 KB |
Output is correct |
8 |
Correct |
29 ms |
12964 KB |
Output is correct |
9 |
Correct |
2 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
4944 KB |
Output is correct |
11 |
Correct |
4 ms |
4944 KB |
Output is correct |
12 |
Correct |
3 ms |
4944 KB |
Output is correct |
13 |
Correct |
3 ms |
4996 KB |
Output is correct |
14 |
Correct |
3 ms |
5072 KB |
Output is correct |
15 |
Correct |
4 ms |
5072 KB |
Output is correct |
16 |
Correct |
4 ms |
5072 KB |
Output is correct |
17 |
Correct |
3 ms |
5072 KB |
Output is correct |
18 |
Correct |
3 ms |
5200 KB |
Output is correct |
19 |
Correct |
4 ms |
5200 KB |
Output is correct |
20 |
Correct |
3 ms |
5072 KB |
Output is correct |
21 |
Correct |
4 ms |
5072 KB |
Output is correct |
22 |
Correct |
3 ms |
5072 KB |
Output is correct |
23 |
Correct |
3 ms |
4944 KB |
Output is correct |
24 |
Correct |
3 ms |
5072 KB |
Output is correct |
25 |
Correct |
4 ms |
5072 KB |
Output is correct |
26 |
Correct |
4 ms |
5048 KB |
Output is correct |
27 |
Correct |
5 ms |
5072 KB |
Output is correct |
28 |
Correct |
3 ms |
5072 KB |
Output is correct |
29 |
Correct |
39 ms |
12972 KB |
Output is correct |
30 |
Correct |
28 ms |
12960 KB |
Output is correct |
31 |
Correct |
3 ms |
5200 KB |
Output is correct |
32 |
Correct |
4 ms |
5064 KB |
Output is correct |
33 |
Correct |
3 ms |
4976 KB |
Output is correct |
34 |
Correct |
3 ms |
4944 KB |
Output is correct |
35 |
Correct |
7 ms |
5440 KB |
Output is correct |
36 |
Correct |
3 ms |
5200 KB |
Output is correct |
37 |
Correct |
40 ms |
13332 KB |
Output is correct |
38 |
Correct |
29 ms |
13180 KB |
Output is correct |
39 |
Correct |
3 ms |
5072 KB |
Output is correct |
40 |
Correct |
4 ms |
5072 KB |
Output is correct |
41 |
Correct |
4 ms |
4992 KB |
Output is correct |
42 |
Correct |
3 ms |
5012 KB |
Output is correct |
43 |
Execution timed out |
3020 ms |
5308 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4944 KB |
Output is correct |
3 |
Correct |
35 ms |
12508 KB |
Output is correct |
4 |
Correct |
32 ms |
12964 KB |
Output is correct |
5 |
Correct |
29 ms |
12972 KB |
Output is correct |
6 |
Correct |
30 ms |
12992 KB |
Output is correct |
7 |
Correct |
34 ms |
12996 KB |
Output is correct |
8 |
Correct |
29 ms |
12964 KB |
Output is correct |
9 |
Correct |
2 ms |
4944 KB |
Output is correct |
10 |
Correct |
3 ms |
4944 KB |
Output is correct |
11 |
Correct |
4 ms |
4944 KB |
Output is correct |
12 |
Correct |
3 ms |
4944 KB |
Output is correct |
13 |
Correct |
3 ms |
4996 KB |
Output is correct |
14 |
Correct |
3 ms |
5072 KB |
Output is correct |
15 |
Correct |
4 ms |
5072 KB |
Output is correct |
16 |
Correct |
4 ms |
5072 KB |
Output is correct |
17 |
Correct |
3 ms |
5072 KB |
Output is correct |
18 |
Correct |
3 ms |
5200 KB |
Output is correct |
19 |
Correct |
4 ms |
5200 KB |
Output is correct |
20 |
Correct |
3 ms |
5072 KB |
Output is correct |
21 |
Correct |
4 ms |
5072 KB |
Output is correct |
22 |
Correct |
3 ms |
5072 KB |
Output is correct |
23 |
Correct |
3 ms |
4944 KB |
Output is correct |
24 |
Correct |
3 ms |
5072 KB |
Output is correct |
25 |
Correct |
4 ms |
5072 KB |
Output is correct |
26 |
Correct |
4 ms |
5048 KB |
Output is correct |
27 |
Correct |
5 ms |
5072 KB |
Output is correct |
28 |
Correct |
3 ms |
5072 KB |
Output is correct |
29 |
Correct |
39 ms |
12972 KB |
Output is correct |
30 |
Correct |
28 ms |
12960 KB |
Output is correct |
31 |
Correct |
3 ms |
5200 KB |
Output is correct |
32 |
Correct |
4 ms |
5064 KB |
Output is correct |
33 |
Correct |
3 ms |
4976 KB |
Output is correct |
34 |
Correct |
3 ms |
4944 KB |
Output is correct |
35 |
Correct |
7 ms |
5440 KB |
Output is correct |
36 |
Correct |
3 ms |
5200 KB |
Output is correct |
37 |
Correct |
40 ms |
13332 KB |
Output is correct |
38 |
Correct |
29 ms |
13180 KB |
Output is correct |
39 |
Correct |
3 ms |
5072 KB |
Output is correct |
40 |
Correct |
4 ms |
5072 KB |
Output is correct |
41 |
Correct |
4 ms |
4992 KB |
Output is correct |
42 |
Correct |
3 ms |
5012 KB |
Output is correct |
43 |
Correct |
716 ms |
8344 KB |
Output is correct |
44 |
Correct |
853 ms |
11632 KB |
Output is correct |
45 |
Correct |
1118 ms |
11640 KB |
Output is correct |
46 |
Correct |
995 ms |
11612 KB |
Output is correct |
47 |
Execution timed out |
3087 ms |
8312 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |