-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB_Appleman_and_Tree.cpp
More file actions
47 lines (34 loc) · 835 Bytes
/
B_Appleman_and_Tree.cpp
File metadata and controls
47 lines (34 loc) · 835 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5, mod = 1000000007;
int n, color[N+1];
vector<int> adj[N+1];
int dp[N+1][2];
void dfs(int node = 0){
dp[node][0] = 1;
dp[node][1] = 0;
for(auto &j : adj[node]){
dfs(j);
dp[node][1] = (1LL * dp[node][1] * (dp[j][0] + dp[j][1])) % mod;
dp[node][1] = (dp[node][1] + 1LL * dp[node][0] * dp[j][1]) % mod;
dp[node][0] = (1LL * dp[node][0] * (dp[j][0] + dp[j][1])) % mod;
}
if(color[node]){
dp[node][1] = dp[node][0];
dp[node][0] = 0;
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1;i<n;i++){
int p;
cin>>p;
adj[p].push_back(i);
}
for(int i=0;i<n;i++) cin>>color[i];
dfs();
cout<<dp[0][1];
}