#include "circuit.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int const N=3e5+5;
ll const mod=1000002022;
vector<int> child[N];
ll on[N],off[N],tot[N];
vector<ll> dp[N];
int n,m;
vector<int> par,st;
int dep[N];
void dfs(int node){
for(int i:child[node]){
dep[i]=dep[node]+1;
dfs(i);
}
}
bool vis[N];
void compute(int node){
int c=child[node].size();
tot[node]=c;
for(int i:child[node])
tot[node]=(tot[node]*tot[i])%mod;
for (int i = 0; i <=c+2; ++i)
{
dp[i].clear();
for (int j = 0; j < c+5; ++j)
dp[i].push_back(0);
}
dp[1][1]=on[child[node][0]];
dp[1][0]=off[child[node][0]];
for(int i=2;i<=c;i++){
for(int j=0;j<=i;j++)
{
dp[i][j]=(dp[i-1][j]*off[child[node][i-1]]%mod)%mod;
if(j==0)
continue;
dp[i][j]+=((dp[i-1][j-1]*on[child[node][i-1]])%mod);
dp[i][j]%=mod;
}
// cout<<endl;
}
on[node]=0;
for(ll i=1;i<=c;i++)
on[node]=(on[node]+((dp[c][i]*i)%mod))%mod;
off[node]=(((tot[node]-on[node])%mod)+mod)%mod;
}
void init(int nn, int mm, vector<int> P, vector<int> A){
// cerr<<"GOT"<<endl;
n=nn;
m=mm;
par=P;
st=A;
for(int i=1;i<n+m;i++)
child[par[i]].push_back(i);
dfs(0);
priority_queue<pair<int,int>> q;
for(int i=n;i<n+m;i++){
tot[i]=1;
if(st[i-n]==1){
on[i]=1;
off[i]=0;
}
else{
on[i]=0;
off[i]=1;
}
// cout<<on[i]<<' '<<off[i]<<' '<<tot[i]<<endl;
}
for(int i=0;i<n+m;i++)
q.push({dep[par[i]],par[i]});
while(q.size()){
int node=(q.top()).second;
q.pop();
if(node==-1 || vis[node])
continue;
vis[node]=1;
compute(node);
q.push({dep[par[node]],par[node]});
}
// for (int i = 0; i < n; ++i)
// cout<<on[i]<<' '<<off[i]<<' '<<tot[i]<<endl;
}
int count_ways(int L, int R){
// cout<<"L R := "<<L<<' '<<R<<endl;
for (int i = 0; i < n+m; ++i)
vis[i]=0;
priority_queue<pair<int,int>> q;
for(int i=L;i<=R;i++){
st[i-n]^=1;
tot[i]=1;
if(st[i-n]==1){
on[i]=1;
off[i]=0;
}
else{
on[i]=0;
off[i]=1;
}
q.push({dep[par[i]],par[i]});
}
while(q.size()){
int node=(q.top()).second;
q.pop();
if(node==-1 || vis[node])
continue;
vis[node]=1;
// cout<<node<<endl;
compute(node);
q.push({dep[par[node]],par[node]});
}
// for (int i = 0; i < n+m; ++i)
// cout<<on[i]<<' '<<off[i]<<' '<<tot[i]<<endl;
return on[0]%mod;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14424 KB |
Output is correct |
2 |
Correct |
6 ms |
14428 KB |
Output is correct |
3 |
Correct |
33 ms |
22360 KB |
Output is correct |
4 |
Correct |
34 ms |
22616 KB |
Output is correct |
5 |
Correct |
35 ms |
22616 KB |
Output is correct |
6 |
Correct |
43 ms |
22616 KB |
Output is correct |
7 |
Correct |
38 ms |
22628 KB |
Output is correct |
8 |
Correct |
34 ms |
22616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14424 KB |
Output is correct |
2 |
Correct |
6 ms |
14424 KB |
Output is correct |
3 |
Correct |
8 ms |
14424 KB |
Output is correct |
4 |
Correct |
7 ms |
14424 KB |
Output is correct |
5 |
Correct |
9 ms |
14424 KB |
Output is correct |
6 |
Correct |
7 ms |
14680 KB |
Output is correct |
7 |
Correct |
7 ms |
14680 KB |
Output is correct |
8 |
Correct |
7 ms |
14680 KB |
Output is correct |
9 |
Correct |
8 ms |
14708 KB |
Output is correct |
10 |
Correct |
7 ms |
14680 KB |
Output is correct |
11 |
Correct |
8 ms |
14680 KB |
Output is correct |
12 |
Correct |
8 ms |
14680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14424 KB |
Output is correct |
2 |
Correct |
6 ms |
14428 KB |
Output is correct |
3 |
Correct |
33 ms |
22360 KB |
Output is correct |
4 |
Correct |
34 ms |
22616 KB |
Output is correct |
5 |
Correct |
35 ms |
22616 KB |
Output is correct |
6 |
Correct |
43 ms |
22616 KB |
Output is correct |
7 |
Correct |
38 ms |
22628 KB |
Output is correct |
8 |
Correct |
34 ms |
22616 KB |
Output is correct |
9 |
Correct |
6 ms |
14424 KB |
Output is correct |
10 |
Correct |
6 ms |
14424 KB |
Output is correct |
11 |
Correct |
8 ms |
14424 KB |
Output is correct |
12 |
Correct |
7 ms |
14424 KB |
Output is correct |
13 |
Correct |
9 ms |
14424 KB |
Output is correct |
14 |
Correct |
7 ms |
14680 KB |
Output is correct |
15 |
Correct |
7 ms |
14680 KB |
Output is correct |
16 |
Correct |
7 ms |
14680 KB |
Output is correct |
17 |
Correct |
8 ms |
14708 KB |
Output is correct |
18 |
Correct |
7 ms |
14680 KB |
Output is correct |
19 |
Correct |
8 ms |
14680 KB |
Output is correct |
20 |
Correct |
8 ms |
14680 KB |
Output is correct |
21 |
Correct |
9 ms |
14680 KB |
Output is correct |
22 |
Correct |
7 ms |
14500 KB |
Output is correct |
23 |
Correct |
7 ms |
14424 KB |
Output is correct |
24 |
Correct |
8 ms |
14696 KB |
Output is correct |
25 |
Correct |
8 ms |
14680 KB |
Output is correct |
26 |
Correct |
8 ms |
14680 KB |
Output is correct |
27 |
Correct |
7 ms |
14680 KB |
Output is correct |
28 |
Correct |
9 ms |
14680 KB |
Output is correct |
29 |
Correct |
34 ms |
22600 KB |
Output is correct |
30 |
Correct |
34 ms |
22616 KB |
Output is correct |
31 |
Correct |
7 ms |
14424 KB |
Output is correct |
32 |
Correct |
9 ms |
14680 KB |
Output is correct |
33 |
Correct |
7 ms |
14628 KB |
Output is correct |
34 |
Correct |
7 ms |
14424 KB |
Output is correct |
35 |
Correct |
12 ms |
14936 KB |
Output is correct |
36 |
Correct |
9 ms |
14696 KB |
Output is correct |
37 |
Correct |
35 ms |
22616 KB |
Output is correct |
38 |
Correct |
35 ms |
22616 KB |
Output is correct |
39 |
Correct |
8 ms |
14424 KB |
Output is correct |
40 |
Correct |
7 ms |
14424 KB |
Output is correct |
41 |
Correct |
7 ms |
14608 KB |
Output is correct |
42 |
Correct |
7 ms |
14424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
735 ms |
19156 KB |
Output is correct |
2 |
Correct |
1175 ms |
23796 KB |
Output is correct |
3 |
Correct |
1165 ms |
23500 KB |
Output is correct |
4 |
Correct |
1163 ms |
23500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
735 ms |
19156 KB |
Output is correct |
2 |
Correct |
1175 ms |
23796 KB |
Output is correct |
3 |
Correct |
1165 ms |
23500 KB |
Output is correct |
4 |
Correct |
1163 ms |
23500 KB |
Output is correct |
5 |
Execution timed out |
3043 ms |
19160 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14424 KB |
Output is correct |
2 |
Correct |
6 ms |
14424 KB |
Output is correct |
3 |
Correct |
8 ms |
14424 KB |
Output is correct |
4 |
Correct |
7 ms |
14424 KB |
Output is correct |
5 |
Correct |
9 ms |
14424 KB |
Output is correct |
6 |
Correct |
7 ms |
14680 KB |
Output is correct |
7 |
Correct |
7 ms |
14680 KB |
Output is correct |
8 |
Correct |
7 ms |
14680 KB |
Output is correct |
9 |
Correct |
8 ms |
14708 KB |
Output is correct |
10 |
Correct |
7 ms |
14680 KB |
Output is correct |
11 |
Correct |
8 ms |
14680 KB |
Output is correct |
12 |
Correct |
8 ms |
14680 KB |
Output is correct |
13 |
Correct |
735 ms |
19156 KB |
Output is correct |
14 |
Correct |
1175 ms |
23796 KB |
Output is correct |
15 |
Correct |
1165 ms |
23500 KB |
Output is correct |
16 |
Correct |
1163 ms |
23500 KB |
Output is correct |
17 |
Execution timed out |
3043 ms |
19160 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14424 KB |
Output is correct |
2 |
Correct |
6 ms |
14428 KB |
Output is correct |
3 |
Correct |
33 ms |
22360 KB |
Output is correct |
4 |
Correct |
34 ms |
22616 KB |
Output is correct |
5 |
Correct |
35 ms |
22616 KB |
Output is correct |
6 |
Correct |
43 ms |
22616 KB |
Output is correct |
7 |
Correct |
38 ms |
22628 KB |
Output is correct |
8 |
Correct |
34 ms |
22616 KB |
Output is correct |
9 |
Correct |
6 ms |
14424 KB |
Output is correct |
10 |
Correct |
6 ms |
14424 KB |
Output is correct |
11 |
Correct |
8 ms |
14424 KB |
Output is correct |
12 |
Correct |
7 ms |
14424 KB |
Output is correct |
13 |
Correct |
9 ms |
14424 KB |
Output is correct |
14 |
Correct |
7 ms |
14680 KB |
Output is correct |
15 |
Correct |
7 ms |
14680 KB |
Output is correct |
16 |
Correct |
7 ms |
14680 KB |
Output is correct |
17 |
Correct |
8 ms |
14708 KB |
Output is correct |
18 |
Correct |
7 ms |
14680 KB |
Output is correct |
19 |
Correct |
8 ms |
14680 KB |
Output is correct |
20 |
Correct |
8 ms |
14680 KB |
Output is correct |
21 |
Correct |
9 ms |
14680 KB |
Output is correct |
22 |
Correct |
7 ms |
14500 KB |
Output is correct |
23 |
Correct |
7 ms |
14424 KB |
Output is correct |
24 |
Correct |
8 ms |
14696 KB |
Output is correct |
25 |
Correct |
8 ms |
14680 KB |
Output is correct |
26 |
Correct |
8 ms |
14680 KB |
Output is correct |
27 |
Correct |
7 ms |
14680 KB |
Output is correct |
28 |
Correct |
9 ms |
14680 KB |
Output is correct |
29 |
Correct |
34 ms |
22600 KB |
Output is correct |
30 |
Correct |
34 ms |
22616 KB |
Output is correct |
31 |
Correct |
7 ms |
14424 KB |
Output is correct |
32 |
Correct |
9 ms |
14680 KB |
Output is correct |
33 |
Correct |
7 ms |
14628 KB |
Output is correct |
34 |
Correct |
7 ms |
14424 KB |
Output is correct |
35 |
Correct |
12 ms |
14936 KB |
Output is correct |
36 |
Correct |
9 ms |
14696 KB |
Output is correct |
37 |
Correct |
35 ms |
22616 KB |
Output is correct |
38 |
Correct |
35 ms |
22616 KB |
Output is correct |
39 |
Correct |
8 ms |
14424 KB |
Output is correct |
40 |
Correct |
7 ms |
14424 KB |
Output is correct |
41 |
Correct |
7 ms |
14608 KB |
Output is correct |
42 |
Correct |
7 ms |
14424 KB |
Output is correct |
43 |
Execution timed out |
3066 ms |
14936 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14424 KB |
Output is correct |
2 |
Correct |
6 ms |
14428 KB |
Output is correct |
3 |
Correct |
33 ms |
22360 KB |
Output is correct |
4 |
Correct |
34 ms |
22616 KB |
Output is correct |
5 |
Correct |
35 ms |
22616 KB |
Output is correct |
6 |
Correct |
43 ms |
22616 KB |
Output is correct |
7 |
Correct |
38 ms |
22628 KB |
Output is correct |
8 |
Correct |
34 ms |
22616 KB |
Output is correct |
9 |
Correct |
6 ms |
14424 KB |
Output is correct |
10 |
Correct |
6 ms |
14424 KB |
Output is correct |
11 |
Correct |
8 ms |
14424 KB |
Output is correct |
12 |
Correct |
7 ms |
14424 KB |
Output is correct |
13 |
Correct |
9 ms |
14424 KB |
Output is correct |
14 |
Correct |
7 ms |
14680 KB |
Output is correct |
15 |
Correct |
7 ms |
14680 KB |
Output is correct |
16 |
Correct |
7 ms |
14680 KB |
Output is correct |
17 |
Correct |
8 ms |
14708 KB |
Output is correct |
18 |
Correct |
7 ms |
14680 KB |
Output is correct |
19 |
Correct |
8 ms |
14680 KB |
Output is correct |
20 |
Correct |
8 ms |
14680 KB |
Output is correct |
21 |
Correct |
9 ms |
14680 KB |
Output is correct |
22 |
Correct |
7 ms |
14500 KB |
Output is correct |
23 |
Correct |
7 ms |
14424 KB |
Output is correct |
24 |
Correct |
8 ms |
14696 KB |
Output is correct |
25 |
Correct |
8 ms |
14680 KB |
Output is correct |
26 |
Correct |
8 ms |
14680 KB |
Output is correct |
27 |
Correct |
7 ms |
14680 KB |
Output is correct |
28 |
Correct |
9 ms |
14680 KB |
Output is correct |
29 |
Correct |
34 ms |
22600 KB |
Output is correct |
30 |
Correct |
34 ms |
22616 KB |
Output is correct |
31 |
Correct |
7 ms |
14424 KB |
Output is correct |
32 |
Correct |
9 ms |
14680 KB |
Output is correct |
33 |
Correct |
7 ms |
14628 KB |
Output is correct |
34 |
Correct |
7 ms |
14424 KB |
Output is correct |
35 |
Correct |
12 ms |
14936 KB |
Output is correct |
36 |
Correct |
9 ms |
14696 KB |
Output is correct |
37 |
Correct |
35 ms |
22616 KB |
Output is correct |
38 |
Correct |
35 ms |
22616 KB |
Output is correct |
39 |
Correct |
8 ms |
14424 KB |
Output is correct |
40 |
Correct |
7 ms |
14424 KB |
Output is correct |
41 |
Correct |
7 ms |
14608 KB |
Output is correct |
42 |
Correct |
7 ms |
14424 KB |
Output is correct |
43 |
Correct |
735 ms |
19156 KB |
Output is correct |
44 |
Correct |
1175 ms |
23796 KB |
Output is correct |
45 |
Correct |
1165 ms |
23500 KB |
Output is correct |
46 |
Correct |
1163 ms |
23500 KB |
Output is correct |
47 |
Execution timed out |
3043 ms |
19160 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |