output
stringlengths 52
181k
| instruction
stringlengths 296
182k
|
---|---|
#include <bits/stdc++.h>
const int N = 100005, mo = 1000000007;
int f[N][2], a[N], q[N], fi[N], v[N], tot = 0;
struct Edge {
int to, ne;
} e[N << 1];
void add(int x, int y) {
e[++tot].to = y;
e[tot].ne = fi[x];
fi[x] = tot;
}
void dfs(int x, int fa) {
f[x][0] = 1;
for (int i = fi[x]; i; i = e[i].ne)
if (e[i].to != fa) {
int y = e[i].to;
dfs(y, x);
f[x][0] = 1ll * f[x][0] * f[y][0] % mo;
}
if (v[x]) {
f[x][1] = f[x][0];
return;
}
int t = 0, z = 1;
a[0] = 1;
for (int i = fi[x]; i; i = e[i].ne)
if (e[i].to != fa) {
q[++t] = e[i].to;
a[t] = 1ll * a[t - 1] * f[q[t]][0] % mo;
}
for (int i = t; i >= 1; i--) {
f[x][1] = (f[x][1] + 1ll * z * a[i - 1] % mo * f[q[i]][1]) % mo;
z = 1ll * z * f[q[i]][0] % mo;
}
(f[x][0] += f[x][1]) %= mo;
}
int main() {
int n;
scanf("%d\n", &n);
for (int i = 1; i < n; i++) {
int x;
scanf("%d", &x);
add(x, i);
add(i, x);
}
for (int i = 0; i < n; i++) scanf("%d", &v[i]);
dfs(0, -1);
printf("%d\n", f[0][1]);
}
|
### Prompt
Construct a cpp code solution to the problem outlined:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
const int N = 100005, mo = 1000000007;
int f[N][2], a[N], q[N], fi[N], v[N], tot = 0;
struct Edge {
int to, ne;
} e[N << 1];
void add(int x, int y) {
e[++tot].to = y;
e[tot].ne = fi[x];
fi[x] = tot;
}
void dfs(int x, int fa) {
f[x][0] = 1;
for (int i = fi[x]; i; i = e[i].ne)
if (e[i].to != fa) {
int y = e[i].to;
dfs(y, x);
f[x][0] = 1ll * f[x][0] * f[y][0] % mo;
}
if (v[x]) {
f[x][1] = f[x][0];
return;
}
int t = 0, z = 1;
a[0] = 1;
for (int i = fi[x]; i; i = e[i].ne)
if (e[i].to != fa) {
q[++t] = e[i].to;
a[t] = 1ll * a[t - 1] * f[q[t]][0] % mo;
}
for (int i = t; i >= 1; i--) {
f[x][1] = (f[x][1] + 1ll * z * a[i - 1] % mo * f[q[i]][1]) % mo;
z = 1ll * z * f[q[i]][0] % mo;
}
(f[x][0] += f[x][1]) %= mo;
}
int main() {
int n;
scanf("%d\n", &n);
for (int i = 1; i < n; i++) {
int x;
scanf("%d", &x);
add(x, i);
add(i, x);
}
for (int i = 0; i < n; i++) scanf("%d", &v[i]);
dfs(0, -1);
printf("%d\n", f[0][1]);
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 100001, MOD = 1e9 + 7;
int n, clr[N];
long long dp[N][2];
vector<vector<int> > g;
void dfs(int u, int p) {
if (clr[u] == 0) {
dp[u][0] = 1;
dp[u][1] = 0;
for (int i = 0; i < g[u].size(); i++)
if (g[u][i] != p) {
dfs(g[u][i], u);
dp[u][1] = (dp[u][1] * dp[g[u][i]][0]) % MOD;
dp[u][1] = (dp[u][1] + (dp[g[u][i]][1] * dp[u][0]) % MOD) % MOD;
dp[u][0] = (dp[u][0] * dp[g[u][i]][0]) % MOD;
}
} else {
dp[u][1] = 1;
for (int i = 0; i < g[u].size(); i++)
if (g[u][i] != p) {
dfs(g[u][i], u);
dp[u][1] = (dp[u][1] * dp[g[u][i]][0]) % MOD;
}
}
dp[u][0] = (dp[u][0] + dp[u][1]) % MOD;
}
int main() {
std::ios::sync_with_stdio(false);
scanf("%d", &n);
g.resize(n);
for (int i = 1; i < n; i++) {
int p;
scanf("%d", &p);
g[i].push_back(p);
g[p].push_back(i);
}
for (int i = 0; i < n; i++) scanf("%d", clr + i);
dfs(0, -1);
printf("%d\n", dp[0][1]);
return 0;
}
|
### Prompt
Create a solution in cpp for the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 100001, MOD = 1e9 + 7;
int n, clr[N];
long long dp[N][2];
vector<vector<int> > g;
void dfs(int u, int p) {
if (clr[u] == 0) {
dp[u][0] = 1;
dp[u][1] = 0;
for (int i = 0; i < g[u].size(); i++)
if (g[u][i] != p) {
dfs(g[u][i], u);
dp[u][1] = (dp[u][1] * dp[g[u][i]][0]) % MOD;
dp[u][1] = (dp[u][1] + (dp[g[u][i]][1] * dp[u][0]) % MOD) % MOD;
dp[u][0] = (dp[u][0] * dp[g[u][i]][0]) % MOD;
}
} else {
dp[u][1] = 1;
for (int i = 0; i < g[u].size(); i++)
if (g[u][i] != p) {
dfs(g[u][i], u);
dp[u][1] = (dp[u][1] * dp[g[u][i]][0]) % MOD;
}
}
dp[u][0] = (dp[u][0] + dp[u][1]) % MOD;
}
int main() {
std::ios::sync_with_stdio(false);
scanf("%d", &n);
g.resize(n);
for (int i = 1; i < n; i++) {
int p;
scanf("%d", &p);
g[i].push_back(p);
g[p].push_back(i);
}
for (int i = 0; i < n; i++) scanf("%d", clr + i);
dfs(0, -1);
printf("%d\n", dp[0][1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> a[100010];
int color[100010];
long long dp[100010][2];
bool past[100010];
void DFS(int u) {
if (color[u])
dp[u][1] = 1;
else
dp[u][0] = 1;
past[u] = 1;
for (int i = 0; i < (int)a[u].size(); i++) {
int v = a[u][i];
if (!past[v]) {
DFS(v);
dp[u][1] = dp[u][1] * (dp[v][0] + dp[v][1]) % 1000000007;
dp[u][1] = (dp[u][1] + dp[u][0] * dp[v][1]) % 1000000007;
dp[u][0] = dp[u][0] * (dp[v][0] + dp[v][1]) % 1000000007;
}
}
}
int main() {
scanf("%d", &n);
int x;
for (int i = 1; i <= n - 1; i++) {
scanf("%d", &x);
a[i].push_back(x);
a[x].push_back(i);
}
for (int i = 0; i <= n - 1; i++) scanf("%d", &color[i]);
DFS(0);
printf("%I64d", dp[0][1]);
return 0;
}
|
### Prompt
Create a solution in cpp for the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> a[100010];
int color[100010];
long long dp[100010][2];
bool past[100010];
void DFS(int u) {
if (color[u])
dp[u][1] = 1;
else
dp[u][0] = 1;
past[u] = 1;
for (int i = 0; i < (int)a[u].size(); i++) {
int v = a[u][i];
if (!past[v]) {
DFS(v);
dp[u][1] = dp[u][1] * (dp[v][0] + dp[v][1]) % 1000000007;
dp[u][1] = (dp[u][1] + dp[u][0] * dp[v][1]) % 1000000007;
dp[u][0] = dp[u][0] * (dp[v][0] + dp[v][1]) % 1000000007;
}
}
}
int main() {
scanf("%d", &n);
int x;
for (int i = 1; i <= n - 1; i++) {
scanf("%d", &x);
a[i].push_back(x);
a[x].push_back(i);
}
for (int i = 0; i <= n - 1; i++) scanf("%d", &color[i]);
DFS(0);
printf("%I64d", dp[0][1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 3;
long long n, m, i, j, k, l, s, t, d, u, ii, jj, x, y, z, h[N], hh[N][2];
vector<long long> V[N];
void f(int x) {
int i, u = V[x].size();
for (i = 0; i < u; i++) {
f(V[x][i]);
hh[x][1] = (hh[x][1] * (hh[V[x][i]][1] + hh[V[x][i]][0]) +
hh[x][0] * hh[V[x][i]][1]) %
1000000007;
hh[x][0] = hh[x][0] * (hh[V[x][i]][0] + hh[V[x][i]][1]) % 1000000007;
}
}
int main() {
cin >> n;
for (i = 2; i <= n; i++) {
scanf("%lld", &x);
V[x + 1].push_back(i);
}
for (i = 1; i <= n; i++) {
scanf("%lld", &x);
hh[i][x] = 1;
}
f(1);
cout << hh[1][1] << '\n';
}
|
### Prompt
Please provide a Cpp coded solution to the problem described below:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 3;
long long n, m, i, j, k, l, s, t, d, u, ii, jj, x, y, z, h[N], hh[N][2];
vector<long long> V[N];
void f(int x) {
int i, u = V[x].size();
for (i = 0; i < u; i++) {
f(V[x][i]);
hh[x][1] = (hh[x][1] * (hh[V[x][i]][1] + hh[V[x][i]][0]) +
hh[x][0] * hh[V[x][i]][1]) %
1000000007;
hh[x][0] = hh[x][0] * (hh[V[x][i]][0] + hh[V[x][i]][1]) % 1000000007;
}
}
int main() {
cin >> n;
for (i = 2; i <= n; i++) {
scanf("%lld", &x);
V[x + 1].push_back(i);
}
for (i = 1; i <= n; i++) {
scanf("%lld", &x);
hh[i][x] = 1;
}
f(1);
cout << hh[1][1] << '\n';
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
const long long int mod2 = 1e9 + 9;
const long long int maxn = 1e6 + 10;
const long long inf = 1e18 + 18;
double pie = 3.1415926535;
long long int col[maxn], dp[maxn][2];
vector<long long int> a[maxn];
void dfs(long long int s, long long int par) {
dp[s][0] = 1 - col[s];
dp[s][1] = col[s];
long long int i, old0, old1, node;
for (i = 0; i < int(a[s].size()); i++) {
node = a[s][i];
if (node == par) continue;
old0 = dp[s][0];
old1 = dp[s][1];
dp[s][0] = dp[s][1] = 0;
dfs(node, s);
dp[s][1] += ((dp[node][1] * old0) % mod + (dp[node][0] * old1) % mod) % mod;
dp[s][0] += old0 * dp[node][0];
dp[s][0] %= mod;
dp[s][1] += dp[node][1] * old1;
dp[s][1] %= mod;
dp[s][0] += dp[node][1] * old0;
dp[s][0] %= mod;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int n, x, i;
cin >> n;
for (i = 2; i <= n; i++) {
cin >> x;
a[x + 1].push_back(i);
a[i].push_back(x + 1);
}
for (i = 1; i <= n; i++) {
cin >> col[i];
}
dfs(1, -1);
cout << dp[1][1] << endl;
}
|
### Prompt
Generate a CPP solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
const long long int mod2 = 1e9 + 9;
const long long int maxn = 1e6 + 10;
const long long inf = 1e18 + 18;
double pie = 3.1415926535;
long long int col[maxn], dp[maxn][2];
vector<long long int> a[maxn];
void dfs(long long int s, long long int par) {
dp[s][0] = 1 - col[s];
dp[s][1] = col[s];
long long int i, old0, old1, node;
for (i = 0; i < int(a[s].size()); i++) {
node = a[s][i];
if (node == par) continue;
old0 = dp[s][0];
old1 = dp[s][1];
dp[s][0] = dp[s][1] = 0;
dfs(node, s);
dp[s][1] += ((dp[node][1] * old0) % mod + (dp[node][0] * old1) % mod) % mod;
dp[s][0] += old0 * dp[node][0];
dp[s][0] %= mod;
dp[s][1] += dp[node][1] * old1;
dp[s][1] %= mod;
dp[s][0] += dp[node][1] * old0;
dp[s][0] %= mod;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int n, x, i;
cin >> n;
for (i = 2; i <= n; i++) {
cin >> x;
a[x + 1].push_back(i);
a[i].push_back(x + 1);
}
for (i = 1; i <= n; i++) {
cin >> col[i];
}
dfs(1, -1);
cout << dp[1][1] << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1000000007, MaxN = 100001;
long long int f[MaxN], g[MaxN], C[MaxN], A, B;
vector<int> ady[MaxN];
long long int mcd(long long int a, long long int b) {
if (!b) {
A = 1, B = 0;
return a;
}
long long int gcd = mcd(b, a % b), x, y;
x = A, y = B;
A = y, B = x - y * (a / b);
return gcd;
}
long long int inverse(long long int a) {
mcd(a, MOD);
return (A + MOD * 5) % MOD;
}
void DFS(int x, int past) {
long long int s = 1, i, j, p, q, T = 1;
for (i = 0; i < ady[x].size(); i++) {
if (ady[x][i] == past) continue;
p = ady[x][i], DFS(p, x);
T = (T * (g[p] + f[p]) % MOD) % MOD;
}
if (C[x]) {
f[x] = T;
g[x] = 0;
} else {
for (i = 0; i < ady[x].size(); i++) {
if (ady[x][i] == past) continue;
p = ady[x][i];
f[x] = (f[x] + ((T * f[p]) % MOD) * inverse((f[p] + g[p]) % MOD)) % MOD;
}
g[x] = T;
}
}
int main() {
long long int i, j, p, q, N;
cin >> N;
for (i = 1; i < N; i++) {
cin >> p;
ady[p].push_back(i);
ady[i].push_back(p);
}
for (i = 0; i < N; i++) cin >> C[i];
DFS(0, 0);
cout << f[0] << "\n";
}
|
### Prompt
Please formulate a Cpp solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1000000007, MaxN = 100001;
long long int f[MaxN], g[MaxN], C[MaxN], A, B;
vector<int> ady[MaxN];
long long int mcd(long long int a, long long int b) {
if (!b) {
A = 1, B = 0;
return a;
}
long long int gcd = mcd(b, a % b), x, y;
x = A, y = B;
A = y, B = x - y * (a / b);
return gcd;
}
long long int inverse(long long int a) {
mcd(a, MOD);
return (A + MOD * 5) % MOD;
}
void DFS(int x, int past) {
long long int s = 1, i, j, p, q, T = 1;
for (i = 0; i < ady[x].size(); i++) {
if (ady[x][i] == past) continue;
p = ady[x][i], DFS(p, x);
T = (T * (g[p] + f[p]) % MOD) % MOD;
}
if (C[x]) {
f[x] = T;
g[x] = 0;
} else {
for (i = 0; i < ady[x].size(); i++) {
if (ady[x][i] == past) continue;
p = ady[x][i];
f[x] = (f[x] + ((T * f[p]) % MOD) * inverse((f[p] + g[p]) % MOD)) % MOD;
}
g[x] = T;
}
}
int main() {
long long int i, j, p, q, N;
cin >> N;
for (i = 1; i < N; i++) {
cin >> p;
ady[p].push_back(i);
ady[i].push_back(p);
}
for (i = 0; i < N; i++) cin >> C[i];
DFS(0, 0);
cout << f[0] << "\n";
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int mxmx = 1e5 + 10;
const long long int mod = 1e9 + 7;
long long int n;
vector<long long int> adj[mxmx];
bool color[mxmx];
long long int dp[2][mxmx];
long long int power(long long int a, long long int b) {
if (b == 0) return 1;
if (b == 1) return a;
long long int ANS;
ANS = power(a, b / 2);
ANS *= ANS;
ANS %= mod;
ANS *= power(a, b % 2);
ANS %= mod;
return ANS;
}
void go(long long int v) {
if (color[v] == 0) {
dp[1][v] = 0;
dp[0][v] = 1;
} else {
dp[0][v] = 0;
dp[1][v] = 1;
}
long long int next;
long long int zarb = 1;
for (long long int i = 0; i < adj[v].size(); i++) {
next = adj[v][i];
go(next);
zarb *= ((dp[0][next] + dp[1][next]) % mod);
zarb %= mod;
}
long long int A, B;
for (long long int i = 0; i < adj[v].size(); i++) {
next = adj[v][i];
if (color[v] == 0) {
dp[0][v] *= ((dp[1][next] + dp[0][next]) % mod);
dp[0][v] %= mod;
A = (zarb * dp[1][next]) % mod;
B = (dp[0][next] + dp[1][next]) % mod;
dp[1][v] += (A * power(B, mod - 2)) % mod;
dp[1][v] %= mod;
} else {
dp[0][v] = 0;
dp[1][v] *= ((dp[1][next] + dp[0][next]) % mod);
dp[1][v] %= mod;
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (long long int i = 1, p; i < n; i++) {
cin >> p;
adj[p].push_back(i);
}
for (long long int i = 0; i < n; i++) {
cin >> color[i];
}
go(0);
cout << dp[1][0];
}
|
### Prompt
Please create a solution in Cpp to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int mxmx = 1e5 + 10;
const long long int mod = 1e9 + 7;
long long int n;
vector<long long int> adj[mxmx];
bool color[mxmx];
long long int dp[2][mxmx];
long long int power(long long int a, long long int b) {
if (b == 0) return 1;
if (b == 1) return a;
long long int ANS;
ANS = power(a, b / 2);
ANS *= ANS;
ANS %= mod;
ANS *= power(a, b % 2);
ANS %= mod;
return ANS;
}
void go(long long int v) {
if (color[v] == 0) {
dp[1][v] = 0;
dp[0][v] = 1;
} else {
dp[0][v] = 0;
dp[1][v] = 1;
}
long long int next;
long long int zarb = 1;
for (long long int i = 0; i < adj[v].size(); i++) {
next = adj[v][i];
go(next);
zarb *= ((dp[0][next] + dp[1][next]) % mod);
zarb %= mod;
}
long long int A, B;
for (long long int i = 0; i < adj[v].size(); i++) {
next = adj[v][i];
if (color[v] == 0) {
dp[0][v] *= ((dp[1][next] + dp[0][next]) % mod);
dp[0][v] %= mod;
A = (zarb * dp[1][next]) % mod;
B = (dp[0][next] + dp[1][next]) % mod;
dp[1][v] += (A * power(B, mod - 2)) % mod;
dp[1][v] %= mod;
} else {
dp[0][v] = 0;
dp[1][v] *= ((dp[1][next] + dp[0][next]) % mod);
dp[1][v] %= mod;
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (long long int i = 1, p; i < n; i++) {
cin >> p;
adj[p].push_back(i);
}
for (long long int i = 0; i < n; i++) {
cin >> color[i];
}
go(0);
cout << dp[1][0];
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[200000];
int a[200000];
long long dp[200000][2];
void dfs(int v) {
dp[v][0] = 1;
dp[v][1] = 0;
for (int i = 0; i < adj[v].size(); i++) {
int u = adj[v][i];
dfs(u);
dp[v][1] = (dp[v][1] * dp[u][0]) % 1000000007;
dp[v][1] = (dp[v][1] + ((dp[v][0] * dp[u][1]) % 1000000007)) % 1000000007;
dp[v][0] = (dp[v][0] * dp[u][0]) % 1000000007;
}
if (a[v] == 1)
dp[v][1] = dp[v][0];
else
dp[v][0] = (dp[v][0] + dp[v][1]) % 1000000007;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int b;
scanf(" %d", &b);
adj[b].push_back(i);
}
for (int i = 0; i < n; i++) scanf(" %d", &a[i]);
dfs(0);
printf("%d\n", dp[0][1] % 1000000007);
}
|
### Prompt
Generate a cpp solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[200000];
int a[200000];
long long dp[200000][2];
void dfs(int v) {
dp[v][0] = 1;
dp[v][1] = 0;
for (int i = 0; i < adj[v].size(); i++) {
int u = adj[v][i];
dfs(u);
dp[v][1] = (dp[v][1] * dp[u][0]) % 1000000007;
dp[v][1] = (dp[v][1] + ((dp[v][0] * dp[u][1]) % 1000000007)) % 1000000007;
dp[v][0] = (dp[v][0] * dp[u][0]) % 1000000007;
}
if (a[v] == 1)
dp[v][1] = dp[v][0];
else
dp[v][0] = (dp[v][0] + dp[v][1]) % 1000000007;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int b;
scanf(" %d", &b);
adj[b].push_back(i);
}
for (int i = 0; i < n; i++) scanf(" %d", &a[i]);
dfs(0);
printf("%d\n", dp[0][1] % 1000000007);
}
```
|
#include <bits/stdc++.h>
using namespace std;
static const long long PRIME = 1e9 + 7;
long long add(long long a, long long b) { return (a + b) % PRIME; }
long long mul(long long a, long long b) { return (a * b) % PRIME; }
long long inv(long long n) {
n = n % PRIME;
long long r = 1;
long long pow = PRIME - 2;
while (pow > 0) {
if (pow & 1) {
r = mul(r, n);
}
pow = pow >> 1;
n = mul(n, n);
}
return r;
}
template <typename T>
struct two {
long long n, m;
vector<T> arr;
two(long long x, long long y) : n(x), m(y), arr(x * y) {}
T& at(long long x, long long y) { return arr[x + n * y]; }
};
struct solve {
long long n;
two<long long> dp;
two<unsigned char> marked;
vector<vector<long long>> children;
vector<long long> colors;
solve(long long nodes, const vector<vector<long long>>& ch,
const vector<long long>& col)
: n(nodes), dp(2, n), marked(2, n), children(ch), colors(col) {}
long long get(long long a, long long b) {
long long result;
if (marked.at(a, b)) {
result = dp.at(a, b);
} else if (a == 0 && colors[b] == 1) {
result = 0;
} else if (a == 0 && colors[b] == 0 && children[b].size() == 0) {
result = 1;
} else if (a == 0 && colors[b] == 0 && children[b].size() > 0) {
result = 1;
for (const long long& child : children[b]) {
result = mul(add(get(0, child), get(1, child)), result);
}
} else if (a == 1 && colors[b] == 1 && children[b].size() == 0) {
result = 1;
} else if (a == 1 && colors[b] == 1 && children[b].size() > 0) {
result = 1;
for (const long long& child : children[b]) {
result = mul(add(get(0, child), get(1, child)), result);
}
} else if (a == 1 && colors[b] == 0 && children[b].size() == 0) {
result = 0;
} else if (a == 1 && colors[b] == 0 && children[b].size() > 0) {
vector<long long> d1list;
vector<long long> pairlist;
bool zeroden = false;
long long zeroindex;
for (const long long& i : children[b]) {
long long d0 = get(0, i);
long long d1 = get(1, i);
long long pair = add(d0, d1);
d1list.push_back(d1);
pairlist.push_back(pair);
if (pair == 0) {
zeroden = true;
zeroindex = i;
}
}
if (!zeroden) {
long long prod = 1;
long long sum = 0;
for (long long i = 0; i < children[b].size(); i++) {
prod = mul(prod, pairlist[i]);
long long term = mul(d1list[i], inv(pairlist[i]));
sum = add(sum, term);
}
result = mul(sum, prod);
} else {
long long prod = 1;
for (const long long& j : children[b]) {
if (j != zeroindex) {
prod = mul(prod, pairlist[j]);
} else {
prod = mul(prod, d1list[j]);
}
}
result = prod;
}
} else {
assert(false);
}
dp.at(a, b) = result;
marked.at(a, b) = true;
return result;
}
};
int main(int argc, char const* argv[]) {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n;
{ scanf("%lld", &n); }
vector<vector<long long>> children(n);
{
for (long long i = 1; i < n; i++) {
long long v;
scanf("%lld", &v);
children[v].push_back(i);
}
}
vector<long long> colors(n);
{
for (long long i = 0; i < n; i++) {
scanf("%lld", &colors[i]);
}
}
solve solution(n, children, colors);
printf("%lld\n", solution.get(1, 0));
return 0;
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
static const long long PRIME = 1e9 + 7;
long long add(long long a, long long b) { return (a + b) % PRIME; }
long long mul(long long a, long long b) { return (a * b) % PRIME; }
long long inv(long long n) {
n = n % PRIME;
long long r = 1;
long long pow = PRIME - 2;
while (pow > 0) {
if (pow & 1) {
r = mul(r, n);
}
pow = pow >> 1;
n = mul(n, n);
}
return r;
}
template <typename T>
struct two {
long long n, m;
vector<T> arr;
two(long long x, long long y) : n(x), m(y), arr(x * y) {}
T& at(long long x, long long y) { return arr[x + n * y]; }
};
struct solve {
long long n;
two<long long> dp;
two<unsigned char> marked;
vector<vector<long long>> children;
vector<long long> colors;
solve(long long nodes, const vector<vector<long long>>& ch,
const vector<long long>& col)
: n(nodes), dp(2, n), marked(2, n), children(ch), colors(col) {}
long long get(long long a, long long b) {
long long result;
if (marked.at(a, b)) {
result = dp.at(a, b);
} else if (a == 0 && colors[b] == 1) {
result = 0;
} else if (a == 0 && colors[b] == 0 && children[b].size() == 0) {
result = 1;
} else if (a == 0 && colors[b] == 0 && children[b].size() > 0) {
result = 1;
for (const long long& child : children[b]) {
result = mul(add(get(0, child), get(1, child)), result);
}
} else if (a == 1 && colors[b] == 1 && children[b].size() == 0) {
result = 1;
} else if (a == 1 && colors[b] == 1 && children[b].size() > 0) {
result = 1;
for (const long long& child : children[b]) {
result = mul(add(get(0, child), get(1, child)), result);
}
} else if (a == 1 && colors[b] == 0 && children[b].size() == 0) {
result = 0;
} else if (a == 1 && colors[b] == 0 && children[b].size() > 0) {
vector<long long> d1list;
vector<long long> pairlist;
bool zeroden = false;
long long zeroindex;
for (const long long& i : children[b]) {
long long d0 = get(0, i);
long long d1 = get(1, i);
long long pair = add(d0, d1);
d1list.push_back(d1);
pairlist.push_back(pair);
if (pair == 0) {
zeroden = true;
zeroindex = i;
}
}
if (!zeroden) {
long long prod = 1;
long long sum = 0;
for (long long i = 0; i < children[b].size(); i++) {
prod = mul(prod, pairlist[i]);
long long term = mul(d1list[i], inv(pairlist[i]));
sum = add(sum, term);
}
result = mul(sum, prod);
} else {
long long prod = 1;
for (const long long& j : children[b]) {
if (j != zeroindex) {
prod = mul(prod, pairlist[j]);
} else {
prod = mul(prod, d1list[j]);
}
}
result = prod;
}
} else {
assert(false);
}
dp.at(a, b) = result;
marked.at(a, b) = true;
return result;
}
};
int main(int argc, char const* argv[]) {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n;
{ scanf("%lld", &n); }
vector<vector<long long>> children(n);
{
for (long long i = 1; i < n; i++) {
long long v;
scanf("%lld", &v);
children[v].push_back(i);
}
}
vector<long long> colors(n);
{
for (long long i = 0; i < n; i++) {
scanf("%lld", &colors[i]);
}
}
solve solution(n, children, colors);
printf("%lld\n", solution.get(1, 0));
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
long long dp[maxn][2];
int num[maxn];
vector<int> g[maxn];
long long q_mod(long long a, long long b, long long n) {
long long ret = 1;
long long tmp = a;
while (b) {
if (b & 0x1) ret = ret * tmp % n;
tmp = tmp * tmp % n;
b >>= 1;
}
return ret;
}
long long Del(long long x, long long y, long long z) {
x = x * z;
x = x % 1000000007;
x = x * q_mod(y, 1000000007 - 2, 1000000007);
x = x % 1000000007;
return x;
}
void dfs(int x) {
int flag = 0;
long long sum = 1;
int n = g[x].size();
for (int i = 0; i < n; i++) {
int y = g[x][i];
dfs(y);
flag = 1;
sum *= dp[y][0];
sum %= 1000000007;
}
if (!flag) {
if (num[x]) {
dp[x][0] = 1;
dp[x][1] = 1;
} else {
dp[x][0] = 1;
dp[x][1] = 0;
}
return;
}
if (num[x]) {
dp[x][1] = sum;
dp[x][0] = sum;
} else {
dp[x][0] = sum;
dp[x][1] = 0;
for (int i = 0; i < n; i++) {
int y = g[x][i];
dp[x][1] += Del(sum, dp[y][0], dp[y][1]);
dp[x][1] %= 1000000007;
}
dp[x][0] += dp[x][1];
dp[x][0] %= 1000000007;
}
return;
}
int main() {
int n;
while (cin >> n) {
int x;
for (int i = 0; i <= n; i++) g[i].clear();
for (int i = 1; i < n; i++) {
scanf("%d", &x);
g[x].push_back(i);
}
for (int i = 0; i < n; i++) scanf("%d", &num[i]);
memset(dp, 0, sizeof(dp));
dfs(0);
cout << dp[0][1] << endl;
}
return 0;
}
|
### Prompt
Please create a solution in cpp to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
long long dp[maxn][2];
int num[maxn];
vector<int> g[maxn];
long long q_mod(long long a, long long b, long long n) {
long long ret = 1;
long long tmp = a;
while (b) {
if (b & 0x1) ret = ret * tmp % n;
tmp = tmp * tmp % n;
b >>= 1;
}
return ret;
}
long long Del(long long x, long long y, long long z) {
x = x * z;
x = x % 1000000007;
x = x * q_mod(y, 1000000007 - 2, 1000000007);
x = x % 1000000007;
return x;
}
void dfs(int x) {
int flag = 0;
long long sum = 1;
int n = g[x].size();
for (int i = 0; i < n; i++) {
int y = g[x][i];
dfs(y);
flag = 1;
sum *= dp[y][0];
sum %= 1000000007;
}
if (!flag) {
if (num[x]) {
dp[x][0] = 1;
dp[x][1] = 1;
} else {
dp[x][0] = 1;
dp[x][1] = 0;
}
return;
}
if (num[x]) {
dp[x][1] = sum;
dp[x][0] = sum;
} else {
dp[x][0] = sum;
dp[x][1] = 0;
for (int i = 0; i < n; i++) {
int y = g[x][i];
dp[x][1] += Del(sum, dp[y][0], dp[y][1]);
dp[x][1] %= 1000000007;
}
dp[x][0] += dp[x][1];
dp[x][0] %= 1000000007;
}
return;
}
int main() {
int n;
while (cin >> n) {
int x;
for (int i = 0; i <= n; i++) g[i].clear();
for (int i = 1; i < n; i++) {
scanf("%d", &x);
g[x].push_back(i);
}
for (int i = 0; i < n; i++) scanf("%d", &num[i]);
memset(dp, 0, sizeof(dp));
dfs(0);
cout << dp[0][1] << endl;
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int p[100010];
long long d[100010][2];
int main() {
int n, xi;
scanf("%d", &n);
for (int i = 0; i < n - 1; ++i) {
scanf("%d", &p[i + 1]);
}
for (int i = 0; i < n; ++i) {
scanf("%d", &xi);
d[i][xi] = 1;
}
for (int i = n - 1; i > 0; --i) {
d[p[i]][1] =
(d[p[i]][1] * (d[i][0] + d[i][1]) + d[p[i]][0] * d[i][1]) % 1000000007;
d[p[i]][0] = (d[p[i]][0] * (d[i][0] + d[i][1])) % 1000000007;
}
printf("%I64d\n", d[0][1]);
return 0;
}
|
### Prompt
Generate a cpp solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int p[100010];
long long d[100010][2];
int main() {
int n, xi;
scanf("%d", &n);
for (int i = 0; i < n - 1; ++i) {
scanf("%d", &p[i + 1]);
}
for (int i = 0; i < n; ++i) {
scanf("%d", &xi);
d[i][xi] = 1;
}
for (int i = n - 1; i > 0; --i) {
d[p[i]][1] =
(d[p[i]][1] * (d[i][0] + d[i][1]) + d[p[i]][0] * d[i][1]) % 1000000007;
d[p[i]][0] = (d[p[i]][0] * (d[i][0] + d[i][1])) % 1000000007;
}
printf("%I64d\n", d[0][1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int max_n = 111111, inf = 1000000007;
int n, col[max_n];
long long dp[max_n][2];
vector<int> g[max_n];
long long power(long long a, long long b) {
if (b == 0) {
return 1;
}
if (b % 2 == 0) {
return power((a * a) % inf, b / 2);
}
return (a * power(a, b - 1)) % inf;
}
void dfs(int v, int p) {
for (int i = 0; i < g[v].size(); ++i) {
if (g[v][i] != p) {
dfs(g[v][i], v);
}
}
if (g[v].size() == 1 && v) {
dp[v][0] = 1;
dp[v][1] = 1;
if (col[v] == 0) {
dp[v][1] = 0;
}
return;
}
if (col[v] == 1) {
dp[v][1] = 1;
for (int i = 0; i < g[v].size(); ++i) {
if (g[v][i] != p) {
dp[v][1] = (dp[v][1] * dp[g[v][i]][0]) % inf;
}
}
} else {
vector<long long> a, b;
int f = 0, poz;
for (int i = 0; i < g[v].size(); ++i) {
if (g[v][i] != p) {
a.push_back(dp[g[v][i]][0]);
b.push_back(dp[g[v][i]][1]);
}
}
for (int i = 0; i < a.size(); ++i) {
if (a[i] == 0) {
++f;
poz = i;
}
}
if (f >= 2) {
dp[v][1] = 0;
} else if (f == 1) {
long long x = b[poz];
for (int i = 0; i < poz; ++i) {
if (g[v][i] != p) x = (x * a[i]) % inf;
}
for (int i = poz + 1; i < a.size(); ++i) {
if (g[v][i] != p) x = (x * a[i]) % inf;
}
dp[v][1] = x;
} else {
long long x = 1;
for (int i = 0; i < a.size(); ++i) {
x = (x * a[i]) % inf;
}
dp[v][1] = 0;
for (int i = 0; i < a.size(); ++i) {
dp[v][1] =
(dp[v][1] + ((x * power(a[i], inf - 2)) % inf * b[i]) % inf) % inf;
}
}
}
dp[v][0] = dp[v][1];
if (col[v] == 0) {
long long x = 1;
for (int i = 0; i < g[v].size(); ++i) {
if (g[v][i] != p) {
x = (x * dp[g[v][i]][0]) % inf;
}
}
dp[v][0] = (dp[v][0] + x) % inf;
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; ++i) {
int p;
scanf("%d", &p);
g[i].push_back(p);
g[p].push_back(i);
}
for (int i = 0; i < n; ++i) {
scanf("%d", &col[i]);
}
dfs(0, -1);
printf("%d\n", dp[0][1] % inf);
return 0;
}
|
### Prompt
Please formulate a cpp solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int max_n = 111111, inf = 1000000007;
int n, col[max_n];
long long dp[max_n][2];
vector<int> g[max_n];
long long power(long long a, long long b) {
if (b == 0) {
return 1;
}
if (b % 2 == 0) {
return power((a * a) % inf, b / 2);
}
return (a * power(a, b - 1)) % inf;
}
void dfs(int v, int p) {
for (int i = 0; i < g[v].size(); ++i) {
if (g[v][i] != p) {
dfs(g[v][i], v);
}
}
if (g[v].size() == 1 && v) {
dp[v][0] = 1;
dp[v][1] = 1;
if (col[v] == 0) {
dp[v][1] = 0;
}
return;
}
if (col[v] == 1) {
dp[v][1] = 1;
for (int i = 0; i < g[v].size(); ++i) {
if (g[v][i] != p) {
dp[v][1] = (dp[v][1] * dp[g[v][i]][0]) % inf;
}
}
} else {
vector<long long> a, b;
int f = 0, poz;
for (int i = 0; i < g[v].size(); ++i) {
if (g[v][i] != p) {
a.push_back(dp[g[v][i]][0]);
b.push_back(dp[g[v][i]][1]);
}
}
for (int i = 0; i < a.size(); ++i) {
if (a[i] == 0) {
++f;
poz = i;
}
}
if (f >= 2) {
dp[v][1] = 0;
} else if (f == 1) {
long long x = b[poz];
for (int i = 0; i < poz; ++i) {
if (g[v][i] != p) x = (x * a[i]) % inf;
}
for (int i = poz + 1; i < a.size(); ++i) {
if (g[v][i] != p) x = (x * a[i]) % inf;
}
dp[v][1] = x;
} else {
long long x = 1;
for (int i = 0; i < a.size(); ++i) {
x = (x * a[i]) % inf;
}
dp[v][1] = 0;
for (int i = 0; i < a.size(); ++i) {
dp[v][1] =
(dp[v][1] + ((x * power(a[i], inf - 2)) % inf * b[i]) % inf) % inf;
}
}
}
dp[v][0] = dp[v][1];
if (col[v] == 0) {
long long x = 1;
for (int i = 0; i < g[v].size(); ++i) {
if (g[v][i] != p) {
x = (x * dp[g[v][i]][0]) % inf;
}
}
dp[v][0] = (dp[v][0] + x) % inf;
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; ++i) {
int p;
scanf("%d", &p);
g[i].push_back(p);
g[p].push_back(i);
}
for (int i = 0; i < n; ++i) {
scanf("%d", &col[i]);
}
dfs(0, -1);
printf("%d\n", dp[0][1] % inf);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const int INF = 2e9;
const long long INFLL = 1e18;
const int MAX_N = 100000;
int N;
int p[MAX_N + 1];
int c[MAX_N + 1];
vector<int> gp[MAX_N + 1];
long long dp[MAX_N + 1][2];
long long multi(long long x, long long y) {
if (y == 1) return x;
long long m = multi(x, y / 2);
if (y % 2) {
return (((m * m) % MOD) * x) % MOD;
} else {
return (m * m) % MOD;
}
}
long long inv(long long x) { return multi(x, MOD - 2); }
void dfs(int x) {
for (int i : gp[x]) {
dfs(i);
}
if (c[x] == 0) {
dp[x][0] = 1;
for (int i : gp[x]) {
dp[x][0] = (dp[x][0] * dp[i][0]) % MOD;
}
for (int i : gp[x]) {
dp[x][1] =
(dp[x][1] + ((dp[x][0] * inv(dp[i][0])) % MOD * dp[i][1]) % MOD) %
MOD;
}
} else {
dp[x][0] = 0;
dp[x][1] = 1;
for (int i : gp[x]) {
dp[x][1] = dp[x][1] * dp[i][0] % MOD;
}
}
dp[x][0] = (dp[x][0] + dp[x][1]) % MOD;
if (0) cout << x << " " << dp[x][0] << " " << dp[x][1] << endl;
}
int main() {
scanf("%d", &N);
for (int i = 1; i < N; i++) {
int x;
scanf("%d", &x);
gp[x].push_back(i);
p[i] = x;
}
for (int i = 0; i < N; i++) {
scanf("%d", &c[i]);
}
dfs(0);
cout << dp[0][1];
return 0;
}
|
### Prompt
Your challenge is to write a CPP solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const int INF = 2e9;
const long long INFLL = 1e18;
const int MAX_N = 100000;
int N;
int p[MAX_N + 1];
int c[MAX_N + 1];
vector<int> gp[MAX_N + 1];
long long dp[MAX_N + 1][2];
long long multi(long long x, long long y) {
if (y == 1) return x;
long long m = multi(x, y / 2);
if (y % 2) {
return (((m * m) % MOD) * x) % MOD;
} else {
return (m * m) % MOD;
}
}
long long inv(long long x) { return multi(x, MOD - 2); }
void dfs(int x) {
for (int i : gp[x]) {
dfs(i);
}
if (c[x] == 0) {
dp[x][0] = 1;
for (int i : gp[x]) {
dp[x][0] = (dp[x][0] * dp[i][0]) % MOD;
}
for (int i : gp[x]) {
dp[x][1] =
(dp[x][1] + ((dp[x][0] * inv(dp[i][0])) % MOD * dp[i][1]) % MOD) %
MOD;
}
} else {
dp[x][0] = 0;
dp[x][1] = 1;
for (int i : gp[x]) {
dp[x][1] = dp[x][1] * dp[i][0] % MOD;
}
}
dp[x][0] = (dp[x][0] + dp[x][1]) % MOD;
if (0) cout << x << " " << dp[x][0] << " " << dp[x][1] << endl;
}
int main() {
scanf("%d", &N);
for (int i = 1; i < N; i++) {
int x;
scanf("%d", &x);
gp[x].push_back(i);
p[i] = x;
}
for (int i = 0; i < N; i++) {
scanf("%d", &c[i]);
}
dfs(0);
cout << dp[0][1];
return 0;
}
```
|
#include <bits/stdc++.h>
const double PI = 3.141592653589793238460;
const int dx4[] = {0, 0, -1, 1};
const int dy4[] = {1, -1, 0, 0};
const int dx8[] = {-1, 1, 0, 0, -1, -1, 1, 1};
const int dy8[] = {0, 0, -1, 1, -1, 1, -1, 1};
void input() {}
using namespace std;
vector<int> g[100005];
bool f[100005];
long long dp[100005][2];
void dfs(int v, int par) {
dp[v][f[v]] = 1;
for (auto ch : g[v]) {
if (ch != par) {
int u = ch;
dfs(ch, v);
long long x0 = dp[v][0];
long long x1 = dp[v][1];
long long y0 = dp[u][0];
long long y1 = dp[u][1];
dp[v][0] = (x0 * y0 + x0 * y1) % 1000000007;
dp[v][1] = (x1 * y1 + x0 * y1 + x1 * y0) % 1000000007;
}
}
}
void solve1() {
int n, i, a;
cin >> n;
for (int i = 0; i < n - 1; i++) cin >> a, g[a].push_back(i + 1);
for (i = 0; i < n; i++) cin >> a, f[i] = a;
dfs(0, -1);
printf("%d\n", dp[0][1]);
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
input();
solve1();
return 0;
}
|
### Prompt
Your task is to create a cpp solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
const double PI = 3.141592653589793238460;
const int dx4[] = {0, 0, -1, 1};
const int dy4[] = {1, -1, 0, 0};
const int dx8[] = {-1, 1, 0, 0, -1, -1, 1, 1};
const int dy8[] = {0, 0, -1, 1, -1, 1, -1, 1};
void input() {}
using namespace std;
vector<int> g[100005];
bool f[100005];
long long dp[100005][2];
void dfs(int v, int par) {
dp[v][f[v]] = 1;
for (auto ch : g[v]) {
if (ch != par) {
int u = ch;
dfs(ch, v);
long long x0 = dp[v][0];
long long x1 = dp[v][1];
long long y0 = dp[u][0];
long long y1 = dp[u][1];
dp[v][0] = (x0 * y0 + x0 * y1) % 1000000007;
dp[v][1] = (x1 * y1 + x0 * y1 + x1 * y0) % 1000000007;
}
}
}
void solve1() {
int n, i, a;
cin >> n;
for (int i = 0; i < n - 1; i++) cin >> a, g[a].push_back(i + 1);
for (i = 0; i < n; i++) cin >> a, f[i] = a;
dfs(0, -1);
printf("%d\n", dp[0][1]);
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
input();
solve1();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
const long long MOD = 1000000007;
int n, node[N];
vector<int> g[N];
long long dp[N][2];
long long pow_mod(long long x, long long k) {
long long ans = 1;
while (k) {
if (k & 1) ans = ans * x % MOD;
x = x * x % MOD;
k >>= 1;
}
return ans;
}
long long inv(long long x) { return pow_mod(x, MOD - 2); }
void init() {
scanf("%d", &n);
int u;
for (int i = 1; i < n; i++) {
scanf("%d", &u);
g[u].push_back(i);
}
for (int i = 0; i < n; i++) scanf("%d", &node[i]);
}
void dfs(int u) {
if (g[u].size() == 0) {
dp[u][node[u]] = 1;
return;
}
for (int i = 0; i < g[u].size(); i++) dfs(g[u][i]);
dp[u][0] = dp[u][1] = 1;
if (node[u]) {
dp[u][0] = 0;
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
dp[u][1] = dp[u][1] * (dp[v][0] + dp[v][1]) % MOD;
}
} else {
long long cnt = 0;
long long mul = 1;
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
dp[u][0] = dp[u][0] * (dp[v][0] + dp[v][1]) % MOD;
mul = mul * (dp[v][0] + dp[v][1]) % MOD;
}
dp[u][1] = 0;
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
dp[u][1] =
(dp[u][1] + mul * inv((dp[v][0] + dp[v][1]) % MOD) % MOD * dp[v][1]) %
MOD;
}
}
}
int main() {
init();
dfs(0);
printf("%I64d\n", dp[0][1] % MOD);
return 0;
}
|
### Prompt
Please provide a cpp coded solution to the problem described below:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
const long long MOD = 1000000007;
int n, node[N];
vector<int> g[N];
long long dp[N][2];
long long pow_mod(long long x, long long k) {
long long ans = 1;
while (k) {
if (k & 1) ans = ans * x % MOD;
x = x * x % MOD;
k >>= 1;
}
return ans;
}
long long inv(long long x) { return pow_mod(x, MOD - 2); }
void init() {
scanf("%d", &n);
int u;
for (int i = 1; i < n; i++) {
scanf("%d", &u);
g[u].push_back(i);
}
for (int i = 0; i < n; i++) scanf("%d", &node[i]);
}
void dfs(int u) {
if (g[u].size() == 0) {
dp[u][node[u]] = 1;
return;
}
for (int i = 0; i < g[u].size(); i++) dfs(g[u][i]);
dp[u][0] = dp[u][1] = 1;
if (node[u]) {
dp[u][0] = 0;
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
dp[u][1] = dp[u][1] * (dp[v][0] + dp[v][1]) % MOD;
}
} else {
long long cnt = 0;
long long mul = 1;
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
dp[u][0] = dp[u][0] * (dp[v][0] + dp[v][1]) % MOD;
mul = mul * (dp[v][0] + dp[v][1]) % MOD;
}
dp[u][1] = 0;
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
dp[u][1] =
(dp[u][1] + mul * inv((dp[v][0] + dp[v][1]) % MOD) % MOD * dp[v][1]) %
MOD;
}
}
}
int main() {
init();
dfs(0);
printf("%I64d\n", dp[0][1] % MOD);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
void dout() { cerr << endl; }
template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
cerr << H << ' ';
dout(T...);
}
long long MOD = 1000000000 + 7;
int color[100000];
vector<int> g[100000];
int n, p;
long long dp[100000][2];
int was[100000][2];
long long pw(long long a, long long p) {
if (p == 0) return 1;
if (p % 2 == 0) {
long long ans = pw(a, p / 2);
return ans * ans % MOD;
}
return a * pw(a, p - 1) % MOD;
}
long long inv(long long a) { return pw(a, MOD - 2); }
long long get(int v, int was_b, int p) {
if (was[v][was_b]) return dp[v][was_b];
was[v][was_b] = 1;
if (color[v] == 1 && !was_b) return 0;
if (color[v] == 1 || was_b == 0) {
long long ans = 0;
long long product = 1;
for (int i = 0; i < g[v].size(); ++i) {
if (g[v][i] == p) continue;
product *= get(g[v][i], 0, v) + get(g[v][i], 1, v);
product %= MOD;
}
ans = product;
return dp[v][was_b] = ans;
}
if (was_b) {
long long ans = 0;
long long all = 1;
for (int i = 0; i < g[v].size(); ++i) {
if (g[v][i] == p) continue;
all *= get(g[v][i], 0, v) + get(g[v][i], 1, v);
all %= MOD;
}
for (int i = 0; i < g[v].size(); ++i) {
if (g[v][i] == p) continue;
long long prod =
(all * inv(get(g[v][i], 1, v) + get(g[v][i], 0, v)) % MOD) *
get(g[v][i], 1, v) % MOD;
ans += prod;
ans %= MOD;
}
return dp[v][was_b] = ans;
}
return 0;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n - 1; ++i) {
scanf("%d", &p);
g[i + 1].push_back(p), g[p].push_back(i + 1);
}
for (int i = 0; i < n; ++i) scanf("%d", &color[i]);
printf("%I64d", get(0, 1, -1));
return 0;
}
|
### Prompt
Please provide a CPP coded solution to the problem described below:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void dout() { cerr << endl; }
template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
cerr << H << ' ';
dout(T...);
}
long long MOD = 1000000000 + 7;
int color[100000];
vector<int> g[100000];
int n, p;
long long dp[100000][2];
int was[100000][2];
long long pw(long long a, long long p) {
if (p == 0) return 1;
if (p % 2 == 0) {
long long ans = pw(a, p / 2);
return ans * ans % MOD;
}
return a * pw(a, p - 1) % MOD;
}
long long inv(long long a) { return pw(a, MOD - 2); }
long long get(int v, int was_b, int p) {
if (was[v][was_b]) return dp[v][was_b];
was[v][was_b] = 1;
if (color[v] == 1 && !was_b) return 0;
if (color[v] == 1 || was_b == 0) {
long long ans = 0;
long long product = 1;
for (int i = 0; i < g[v].size(); ++i) {
if (g[v][i] == p) continue;
product *= get(g[v][i], 0, v) + get(g[v][i], 1, v);
product %= MOD;
}
ans = product;
return dp[v][was_b] = ans;
}
if (was_b) {
long long ans = 0;
long long all = 1;
for (int i = 0; i < g[v].size(); ++i) {
if (g[v][i] == p) continue;
all *= get(g[v][i], 0, v) + get(g[v][i], 1, v);
all %= MOD;
}
for (int i = 0; i < g[v].size(); ++i) {
if (g[v][i] == p) continue;
long long prod =
(all * inv(get(g[v][i], 1, v) + get(g[v][i], 0, v)) % MOD) *
get(g[v][i], 1, v) % MOD;
ans += prod;
ans %= MOD;
}
return dp[v][was_b] = ans;
}
return 0;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n - 1; ++i) {
scanf("%d", &p);
g[i + 1].push_back(p), g[p].push_back(i + 1);
}
for (int i = 0; i < n; ++i) scanf("%d", &color[i]);
printf("%I64d", get(0, 1, -1));
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
int n;
vector<int> x;
vector<vector<int>> g;
vector<vector<long long>> dp;
long long BinPow(long long x, int n) {
if (n == 0) return 1;
if (n % 2 == 0) return BinPow(x * x % MOD, n / 2);
return x * BinPow(x, n - 1) % MOD;
}
long long Rev(long long x) { return BinPow(x, MOD - 2); }
void DFS(int u) {
for (int v : g[u]) DFS(v);
if (x[u] == 0) {
dp[u][0] = 1;
for (int v : g[u]) {
dp[u][0] *= (dp[v][0] + dp[v][1]) % MOD;
dp[u][0] %= MOD;
}
long long s = 1;
for (int v : g[u]) {
s *= (dp[v][0] + dp[v][1]) % MOD;
s %= MOD;
}
dp[u][1] = 0;
for (int v : g[u]) {
dp[u][1] += ((dp[v][2] * s) % MOD) * Rev(dp[v][0] + dp[v][1]) % MOD;
dp[u][1] %= MOD;
}
dp[u][2] = 0;
for (int v : g[u]) {
dp[u][2] += ((dp[v][2] * s) % MOD) * Rev(dp[v][0] + dp[v][1]) % MOD;
dp[u][2] %= MOD;
}
}
if (x[u] == 1) {
dp[u][0] = 0;
dp[u][1] = 1;
for (int v : g[u]) {
dp[u][1] *= (dp[v][0] + dp[v][1]) % MOD;
dp[u][1] %= MOD;
}
dp[u][2] = 1;
for (int v : g[u]) {
dp[u][2] *= (dp[v][0] + dp[v][1]) % MOD;
dp[u][2] %= MOD;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n;
g.resize(n);
for (int i = 1; i < n; ++i) {
int p;
cin >> p;
g[p].push_back(i);
}
x.resize(n);
for (int i = 0; i < n; ++i) cin >> x[i];
dp.resize(n, vector<long long>(3, 0));
DFS(0);
cout << dp[0][1] << endl;
return 0;
}
|
### Prompt
Generate a CPP solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
int n;
vector<int> x;
vector<vector<int>> g;
vector<vector<long long>> dp;
long long BinPow(long long x, int n) {
if (n == 0) return 1;
if (n % 2 == 0) return BinPow(x * x % MOD, n / 2);
return x * BinPow(x, n - 1) % MOD;
}
long long Rev(long long x) { return BinPow(x, MOD - 2); }
void DFS(int u) {
for (int v : g[u]) DFS(v);
if (x[u] == 0) {
dp[u][0] = 1;
for (int v : g[u]) {
dp[u][0] *= (dp[v][0] + dp[v][1]) % MOD;
dp[u][0] %= MOD;
}
long long s = 1;
for (int v : g[u]) {
s *= (dp[v][0] + dp[v][1]) % MOD;
s %= MOD;
}
dp[u][1] = 0;
for (int v : g[u]) {
dp[u][1] += ((dp[v][2] * s) % MOD) * Rev(dp[v][0] + dp[v][1]) % MOD;
dp[u][1] %= MOD;
}
dp[u][2] = 0;
for (int v : g[u]) {
dp[u][2] += ((dp[v][2] * s) % MOD) * Rev(dp[v][0] + dp[v][1]) % MOD;
dp[u][2] %= MOD;
}
}
if (x[u] == 1) {
dp[u][0] = 0;
dp[u][1] = 1;
for (int v : g[u]) {
dp[u][1] *= (dp[v][0] + dp[v][1]) % MOD;
dp[u][1] %= MOD;
}
dp[u][2] = 1;
for (int v : g[u]) {
dp[u][2] *= (dp[v][0] + dp[v][1]) % MOD;
dp[u][2] %= MOD;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n;
g.resize(n);
for (int i = 1; i < n; ++i) {
int p;
cin >> p;
g[p].push_back(i);
}
x.resize(n);
for (int i = 0; i < n; ++i) cin >> x[i];
dp.resize(n, vector<long long>(3, 0));
DFS(0);
cout << dp[0][1] << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
;
const int M = 100005;
const int mod = 1e9 + 7;
vector<int> adj[M];
int n, color[M];
long long dp[M][3];
void dfs(int node, int p) {
dp[node][1] = color[node];
dp[node][0] = (color[node] == 0);
for (auto i : adj[node]) {
if (i == p) continue;
dfs(i, node);
dp[node][1] = dp[node][1] * dp[i][0] + dp[node][0] * dp[i][1] +
dp[node][1] * dp[i][1];
dp[node][1] %= mod;
dp[node][0] = dp[node][0] * dp[i][0] + dp[node][0] * dp[i][1];
dp[node][0] %= mod;
}
return;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 2; i <= n; ++i) {
int a;
cin >> a;
adj[a + 1].push_back(i);
adj[i].push_back(a + 1);
}
for (int i = 1; i <= n; ++i) cin >> color[i];
dfs(1, 0);
cout << dp[1][1] << "\n";
return 0;
}
|
### Prompt
Your task is to create a CPP solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
;
const int M = 100005;
const int mod = 1e9 + 7;
vector<int> adj[M];
int n, color[M];
long long dp[M][3];
void dfs(int node, int p) {
dp[node][1] = color[node];
dp[node][0] = (color[node] == 0);
for (auto i : adj[node]) {
if (i == p) continue;
dfs(i, node);
dp[node][1] = dp[node][1] * dp[i][0] + dp[node][0] * dp[i][1] +
dp[node][1] * dp[i][1];
dp[node][1] %= mod;
dp[node][0] = dp[node][0] * dp[i][0] + dp[node][0] * dp[i][1];
dp[node][0] %= mod;
}
return;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 2; i <= n; ++i) {
int a;
cin >> a;
adj[a + 1].push_back(i);
adj[i].push_back(a + 1);
}
for (int i = 1; i <= n; ++i) cin >> color[i];
dfs(1, 0);
cout << dp[1][1] << "\n";
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<int> edge[100005];
long long dp[100005][2];
int c[100005], n;
void dfs(int u) {
dp[u][0] = 1;
dp[u][1] = 0;
for (unsigned int i = 0; i < edge[u].size(); i++) {
int v = edge[u][i];
dfs(v);
dp[u][1] = dp[u][1] * dp[v][0] + dp[u][0] * dp[v][1];
dp[u][1] %= (1000000000 + 7);
dp[u][0] *= dp[v][0];
dp[u][0] %= (1000000000 + 7);
}
if (c[u])
dp[u][1] = dp[u][0];
else {
dp[u][0] += dp[u][1];
dp[u][0] %= (1000000000 + 7);
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int p;
scanf("%d", &p);
edge[p].push_back(i);
}
for (int i = 0; i < n; i++) scanf("%d", &c[i]);
dfs(0);
printf("%I64d\n", dp[0][1]);
return 0;
}
|
### Prompt
Your task is to create a Cpp solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> edge[100005];
long long dp[100005][2];
int c[100005], n;
void dfs(int u) {
dp[u][0] = 1;
dp[u][1] = 0;
for (unsigned int i = 0; i < edge[u].size(); i++) {
int v = edge[u][i];
dfs(v);
dp[u][1] = dp[u][1] * dp[v][0] + dp[u][0] * dp[v][1];
dp[u][1] %= (1000000000 + 7);
dp[u][0] *= dp[v][0];
dp[u][0] %= (1000000000 + 7);
}
if (c[u])
dp[u][1] = dp[u][0];
else {
dp[u][0] += dp[u][1];
dp[u][0] %= (1000000000 + 7);
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int p;
scanf("%d", &p);
edge[p].push_back(i);
}
for (int i = 0; i < n; i++) scanf("%d", &c[i]);
dfs(0);
printf("%I64d\n", dp[0][1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<int> G[100000 + 5];
int n, cor[100000 + 5];
long long dp[100000 + 5][2];
void dfs(int u) {
if (cor[u]) {
dp[u][1] = 1;
} else {
dp[u][0] = 1;
}
for (auto v : G[u]) {
dfs(v);
dp[u][1] = (dp[u][1] * (dp[v][0] + dp[v][1]) % 1000000007 +
dp[u][0] * dp[v][1] % 1000000007) %
1000000007;
dp[u][0] = dp[u][0] * (dp[v][0] + dp[v][1]) % 1000000007;
}
}
void read_and_parse() {
scanf("%d", &n);
for (int i = 2, fa; i <= n; i++) {
scanf("%d", &fa);
fa++;
G[fa].push_back(i);
}
for (int i = 1; i <= n; i++) scanf("%d", &cor[i]);
}
void solve() {
dfs(1);
cout << dp[1][1] << endl;
}
int main() {
read_and_parse();
solve();
return 0;
}
|
### Prompt
Develop a solution in Cpp to the problem described below:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> G[100000 + 5];
int n, cor[100000 + 5];
long long dp[100000 + 5][2];
void dfs(int u) {
if (cor[u]) {
dp[u][1] = 1;
} else {
dp[u][0] = 1;
}
for (auto v : G[u]) {
dfs(v);
dp[u][1] = (dp[u][1] * (dp[v][0] + dp[v][1]) % 1000000007 +
dp[u][0] * dp[v][1] % 1000000007) %
1000000007;
dp[u][0] = dp[u][0] * (dp[v][0] + dp[v][1]) % 1000000007;
}
}
void read_and_parse() {
scanf("%d", &n);
for (int i = 2, fa; i <= n; i++) {
scanf("%d", &fa);
fa++;
G[fa].push_back(i);
}
for (int i = 1; i <= n; i++) scanf("%d", &cor[i]);
}
void solve() {
dfs(1);
cout << dp[1][1] << endl;
}
int main() {
read_and_parse();
solve();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 111111;
const int maxm = 111111;
const int mod = 1000000007;
int n;
vector<int> a[maxn];
int col[maxn];
int s[maxn];
int num;
int v[maxn];
long long dp[maxn][2];
void dfs(int x) {
v[x] = 1;
int len = a[x].size();
for (int i = 0; i < len; i++)
if (!v[a[x][i]]) {
dfs(a[x][i]);
}
s[num++] = x;
}
void solve() {
num = 0;
int t;
memset(v, 0, sizeof(v));
for (int i = 0; i <= n; i++) {
a[i].clear();
}
for (int i = 0; i < n - 1; i++) {
scanf("%d", &t);
a[i + 1].push_back(t);
a[t].push_back(i + 1);
}
for (int i = 0; i < n; i++) {
scanf("%d", &col[i]);
}
dfs(0);
int len;
memset(v, 0, sizeof(v));
for (int i = 0; i < num; i++) {
t = s[i];
v[t] = 1;
if (col[t] == 1) {
dp[t][0] = 0;
dp[t][1] = 1;
len = a[t].size();
for (int i = 0; i < len; i++)
if (v[a[t][i]]) {
dp[t][1] = dp[t][1] * (dp[a[t][i]][1] + dp[a[t][i]][0]) % mod;
}
} else {
dp[t][0] = 1;
dp[t][1] = 0;
len = a[t].size();
for (int i = 0; i < len; i++)
if (v[a[t][i]]) {
dp[t][1] = (dp[t][1] * (dp[a[t][i]][1] + dp[a[t][i]][0]) +
dp[t][0] * dp[a[t][i]][1]) %
mod;
dp[t][0] = dp[t][0] * (dp[a[t][i]][1] + dp[a[t][i]][0]) % mod;
}
}
}
cout << dp[0][1] << endl;
}
int main() {
int t;
while (~scanf("%d", &n)) {
solve();
}
return 0;
}
|
### Prompt
Please formulate a cpp solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 111111;
const int maxm = 111111;
const int mod = 1000000007;
int n;
vector<int> a[maxn];
int col[maxn];
int s[maxn];
int num;
int v[maxn];
long long dp[maxn][2];
void dfs(int x) {
v[x] = 1;
int len = a[x].size();
for (int i = 0; i < len; i++)
if (!v[a[x][i]]) {
dfs(a[x][i]);
}
s[num++] = x;
}
void solve() {
num = 0;
int t;
memset(v, 0, sizeof(v));
for (int i = 0; i <= n; i++) {
a[i].clear();
}
for (int i = 0; i < n - 1; i++) {
scanf("%d", &t);
a[i + 1].push_back(t);
a[t].push_back(i + 1);
}
for (int i = 0; i < n; i++) {
scanf("%d", &col[i]);
}
dfs(0);
int len;
memset(v, 0, sizeof(v));
for (int i = 0; i < num; i++) {
t = s[i];
v[t] = 1;
if (col[t] == 1) {
dp[t][0] = 0;
dp[t][1] = 1;
len = a[t].size();
for (int i = 0; i < len; i++)
if (v[a[t][i]]) {
dp[t][1] = dp[t][1] * (dp[a[t][i]][1] + dp[a[t][i]][0]) % mod;
}
} else {
dp[t][0] = 1;
dp[t][1] = 0;
len = a[t].size();
for (int i = 0; i < len; i++)
if (v[a[t][i]]) {
dp[t][1] = (dp[t][1] * (dp[a[t][i]][1] + dp[a[t][i]][0]) +
dp[t][0] * dp[a[t][i]][1]) %
mod;
dp[t][0] = dp[t][0] * (dp[a[t][i]][1] + dp[a[t][i]][0]) % mod;
}
}
}
cout << dp[0][1] << endl;
}
int main() {
int t;
while (~scanf("%d", &n)) {
solve();
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int V = 100100;
const int P = 1000000007;
struct Edge {
int v, ne;
} e[V * 2];
int p[V], K;
void add(int u, int v) {
e[K].v = v;
e[K].ne = p[u];
p[u] = K++;
}
int col[V], dp[V][2];
void dfs(int u) {
dp[u][0] = dp[u][1] = 0;
if (col[u] == 1)
dp[u][1] = 1;
else
dp[u][0] = 1;
for (int i = p[u]; i != -1; i = e[i].ne) {
int v = e[i].v;
dfs(v);
int tmp0 = (long long)dp[u][0] * (dp[v][0] + dp[v][1]) % P;
int tmp1 = ((long long)(dp[u][0] + dp[u][1]) * dp[v][1] % P +
(long long)dp[u][1] * dp[v][0] % P) %
P;
dp[u][0] = tmp0;
dp[u][1] = tmp1;
}
}
int n, x;
int main() {
while (~scanf("%d", &n)) {
memset(p, -1, sizeof(p));
K = 0;
for (int i = 1; i < n; i++) {
scanf("%d", &x);
add(x, i);
}
for (int i = 0; i < n; i++) scanf("%d", &col[i]);
dfs(0);
printf("%d\n", dp[0][1]);
}
return 0;
}
|
### Prompt
In Cpp, your task is to solve the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int V = 100100;
const int P = 1000000007;
struct Edge {
int v, ne;
} e[V * 2];
int p[V], K;
void add(int u, int v) {
e[K].v = v;
e[K].ne = p[u];
p[u] = K++;
}
int col[V], dp[V][2];
void dfs(int u) {
dp[u][0] = dp[u][1] = 0;
if (col[u] == 1)
dp[u][1] = 1;
else
dp[u][0] = 1;
for (int i = p[u]; i != -1; i = e[i].ne) {
int v = e[i].v;
dfs(v);
int tmp0 = (long long)dp[u][0] * (dp[v][0] + dp[v][1]) % P;
int tmp1 = ((long long)(dp[u][0] + dp[u][1]) * dp[v][1] % P +
(long long)dp[u][1] * dp[v][0] % P) %
P;
dp[u][0] = tmp0;
dp[u][1] = tmp1;
}
}
int n, x;
int main() {
while (~scanf("%d", &n)) {
memset(p, -1, sizeof(p));
K = 0;
for (int i = 1; i < n; i++) {
scanf("%d", &x);
add(x, i);
}
for (int i = 0; i < n; i++) scanf("%d", &col[i]);
dfs(0);
printf("%d\n", dp[0][1]);
}
return 0;
}
```
|
#include <bits/stdc++.h>
const int mod = (int)1e9 + 7;
int add(int a, int b) { return (a += b) >= mod ? a -= mod : a; }
int sub(int a, int b) { return add(a, mod - b); }
int mul(int a, int b) { return int(1LL * a * b % mod); }
int pow(int a, int64_t n) {
int res = 1;
while (n > 0) {
if (n & 1) {
res = mul(res, a);
}
a = mul(a, a);
n >>= 1;
}
return res;
}
int inv(int a) { return pow(a, mod - 2); }
int divmod(int a, int b) { return mul(a, inv(b)); }
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(0);
for (int n; std::cin >> n;) {
std::vector<std::vector<int> > next(n);
for (int u = 1, p; u < n; ++u) {
std::cin >> p;
next[p].push_back(u);
next[u].push_back(p);
}
std::vector<int> color(n);
for (int i = 0; i < n; ++i) {
std::cin >> color[i];
}
std::vector<int> cnt0(n, -1), cnt1(n);
std::function<void(int, int)> dfs = [&](int u, int p) {
if (color[u] == 1) {
cnt0[u] = 0;
cnt1[u] = 1;
return;
}
cnt0[u] = 1;
cnt1[u] = 0;
for (int v : next[u]) {
if (v == p) {
continue;
}
dfs(v, u);
cnt0[u] = mul(cnt0[u], add(cnt0[v], cnt1[v]));
cnt1[u] = add(cnt1[u], divmod(cnt1[v], add(cnt0[v], cnt1[v])));
}
cnt1[u] = mul(cnt1[u], cnt0[u]);
};
int answ = 1;
for (int u = 0; u < n; ++u) {
if (cnt0[u] == -1 && color[u] == 0) {
dfs(u, -1);
answ = mul(answ, cnt1[u]);
}
}
std::cout << answ << std::endl;
}
return 0;
}
|
### Prompt
Develop a solution in cpp to the problem described below:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
const int mod = (int)1e9 + 7;
int add(int a, int b) { return (a += b) >= mod ? a -= mod : a; }
int sub(int a, int b) { return add(a, mod - b); }
int mul(int a, int b) { return int(1LL * a * b % mod); }
int pow(int a, int64_t n) {
int res = 1;
while (n > 0) {
if (n & 1) {
res = mul(res, a);
}
a = mul(a, a);
n >>= 1;
}
return res;
}
int inv(int a) { return pow(a, mod - 2); }
int divmod(int a, int b) { return mul(a, inv(b)); }
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(0);
for (int n; std::cin >> n;) {
std::vector<std::vector<int> > next(n);
for (int u = 1, p; u < n; ++u) {
std::cin >> p;
next[p].push_back(u);
next[u].push_back(p);
}
std::vector<int> color(n);
for (int i = 0; i < n; ++i) {
std::cin >> color[i];
}
std::vector<int> cnt0(n, -1), cnt1(n);
std::function<void(int, int)> dfs = [&](int u, int p) {
if (color[u] == 1) {
cnt0[u] = 0;
cnt1[u] = 1;
return;
}
cnt0[u] = 1;
cnt1[u] = 0;
for (int v : next[u]) {
if (v == p) {
continue;
}
dfs(v, u);
cnt0[u] = mul(cnt0[u], add(cnt0[v], cnt1[v]));
cnt1[u] = add(cnt1[u], divmod(cnt1[v], add(cnt0[v], cnt1[v])));
}
cnt1[u] = mul(cnt1[u], cnt0[u]);
};
int answ = 1;
for (int u = 0; u < n; ++u) {
if (cnt0[u] == -1 && color[u] == 0) {
dfs(u, -1);
answ = mul(answ, cnt1[u]);
}
}
std::cout << answ << std::endl;
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long visited[100005] = {false};
vector<int> g[100005];
long long n, x, b[100005], dp[100005][2];
void dfs(int v) {
dp[v][0] = 1 - b[v];
dp[v][1] = b[v];
int old[2];
for (int i = 0; i < g[v].size(); ++i) {
int u = g[v][i];
old[0] = dp[v][0];
old[1] = dp[v][1];
dp[v][0] = dp[v][1] = 0;
dfs(u);
dp[v][0] = (dp[v][0] + old[0] * dp[u][1]) % 1000000007;
dp[v][1] = (dp[v][1] + old[1] * dp[u][1]) % 1000000007;
dp[v][1] = (dp[v][1] + old[0] * dp[u][1]) % 1000000007;
dp[v][1] = (dp[v][1] + old[1] * dp[u][0]) % 1000000007;
dp[v][0] = (dp[v][0] + old[0] * dp[u][0]) % 1000000007;
}
}
int main() {
cin >> n;
for (int i = 0; i < n - 1; ++i) {
cin >> x;
g[x].push_back(i + 1);
}
for (int i = 0; i < n; ++i) cin >> b[i];
dfs(0);
cout << dp[0][1] % 1000000007 << endl;
return 0;
}
|
### Prompt
Your task is to create a CPP solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long visited[100005] = {false};
vector<int> g[100005];
long long n, x, b[100005], dp[100005][2];
void dfs(int v) {
dp[v][0] = 1 - b[v];
dp[v][1] = b[v];
int old[2];
for (int i = 0; i < g[v].size(); ++i) {
int u = g[v][i];
old[0] = dp[v][0];
old[1] = dp[v][1];
dp[v][0] = dp[v][1] = 0;
dfs(u);
dp[v][0] = (dp[v][0] + old[0] * dp[u][1]) % 1000000007;
dp[v][1] = (dp[v][1] + old[1] * dp[u][1]) % 1000000007;
dp[v][1] = (dp[v][1] + old[0] * dp[u][1]) % 1000000007;
dp[v][1] = (dp[v][1] + old[1] * dp[u][0]) % 1000000007;
dp[v][0] = (dp[v][0] + old[0] * dp[u][0]) % 1000000007;
}
}
int main() {
cin >> n;
for (int i = 0; i < n - 1; ++i) {
cin >> x;
g[x].push_back(i + 1);
}
for (int i = 0; i < n; ++i) cin >> b[i];
dfs(0);
cout << dp[0][1] % 1000000007 << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const int N = 1e5 + 10;
const int MOD = 1e9 + 7;
long long in[N], ex[N], col[N];
vector<int> child[N];
long long fex(int r);
long long fin(int r) {
if (in[r] != -1) return in[r];
if (col[r]) {
in[r] = 1LL;
for (long long i = 0; i < (child[r].size()); i++)
in[r] = in[r] * ((fin(child[r][i]) + fex(child[r][i])) % MOD) % MOD;
} else {
in[r] = 0;
long long mul = 1LL;
for (long long i = 0; i < (child[r].size()); i++) {
long long param = (fin(child[r][i]) + fex(child[r][i])) % MOD;
in[r] = (in[r] * param % MOD + mul * fin(child[r][i]) % MOD) % MOD;
mul = mul * param % MOD;
}
}
return in[r];
}
long long fex(int r) {
if (ex[r] != -1) return ex[r];
if (col[r]) return ex[r] = 0;
ex[r] = 1LL;
for (long long i = 0; i < (child[r].size()); i++)
ex[r] = ex[r] * ((fin(child[r][i]) + fex(child[r][i])) % MOD) % MOD;
return ex[r];
}
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
memset(col, 0, sizeof(col));
memset(in, -1, sizeof(in));
memset(ex, -1, sizeof(ex));
for (long long i = 0; i < (n - 1); i++) {
int p;
cin >> p;
child[p].push_back(i + 1);
}
for (long long i = 0; i < (n); i++) cin >> col[i];
cout << fin(0) << endl;
}
|
### Prompt
Develop a solution in Cpp to the problem described below:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const int N = 1e5 + 10;
const int MOD = 1e9 + 7;
long long in[N], ex[N], col[N];
vector<int> child[N];
long long fex(int r);
long long fin(int r) {
if (in[r] != -1) return in[r];
if (col[r]) {
in[r] = 1LL;
for (long long i = 0; i < (child[r].size()); i++)
in[r] = in[r] * ((fin(child[r][i]) + fex(child[r][i])) % MOD) % MOD;
} else {
in[r] = 0;
long long mul = 1LL;
for (long long i = 0; i < (child[r].size()); i++) {
long long param = (fin(child[r][i]) + fex(child[r][i])) % MOD;
in[r] = (in[r] * param % MOD + mul * fin(child[r][i]) % MOD) % MOD;
mul = mul * param % MOD;
}
}
return in[r];
}
long long fex(int r) {
if (ex[r] != -1) return ex[r];
if (col[r]) return ex[r] = 0;
ex[r] = 1LL;
for (long long i = 0; i < (child[r].size()); i++)
ex[r] = ex[r] * ((fin(child[r][i]) + fex(child[r][i])) % MOD) % MOD;
return ex[r];
}
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
memset(col, 0, sizeof(col));
memset(in, -1, sizeof(in));
memset(ex, -1, sizeof(ex));
for (long long i = 0; i < (n - 1); i++) {
int p;
cin >> p;
child[p].push_back(i + 1);
}
for (long long i = 0; i < (n); i++) cin >> col[i];
cout << fin(0) << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cerr << name << " : " << arg1 << "\n";
}
template <typename Arg1, typename... Args>
void __f(const char* na, Arg1&& arg1, Args&&... args) {
const char* c = strchr(na + 1, ',');
cerr.write(na, c - na) << " : " << arg1 << " , ";
__f(c + 1, args...);
}
const int MOD = 1e9 + 7;
const int inf = 1e9;
const double EPS = 1e-9;
const double PI = acos(-1.0);
const long long INF = 1e18;
const int N = 1e5 + 5;
int col[N];
vector<int> graph[N];
long long int dp[N][2];
void dfs(int s, int p) {
dp[s][0] = 1;
dp[s][1] = 0;
for (auto x : graph[s]) {
if (x == p) continue;
dfs(x, s);
dp[s][1] = (dp[s][1] % MOD * dp[x][0] % MOD) % MOD;
dp[s][1] = (dp[s][1] % MOD + (dp[s][0] % MOD * dp[x][1] % MOD) % MOD) % MOD;
dp[s][0] = (dp[s][0] % MOD * dp[x][0] % MOD) % MOD;
}
if (col[s] == 1)
dp[s][1] = dp[s][0] % MOD;
else
dp[s][0] = (dp[s][0] % MOD + dp[s][1] % MOD) % MOD;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int u;
cin >> u;
graph[u].push_back(i);
graph[i].push_back(u);
}
for (int i = 0; i < n; i++) cin >> col[i];
dfs(0, -1);
cout << dp[0][1] % MOD << "\n";
return 0;
}
|
### Prompt
Please create a solution in CPP to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cerr << name << " : " << arg1 << "\n";
}
template <typename Arg1, typename... Args>
void __f(const char* na, Arg1&& arg1, Args&&... args) {
const char* c = strchr(na + 1, ',');
cerr.write(na, c - na) << " : " << arg1 << " , ";
__f(c + 1, args...);
}
const int MOD = 1e9 + 7;
const int inf = 1e9;
const double EPS = 1e-9;
const double PI = acos(-1.0);
const long long INF = 1e18;
const int N = 1e5 + 5;
int col[N];
vector<int> graph[N];
long long int dp[N][2];
void dfs(int s, int p) {
dp[s][0] = 1;
dp[s][1] = 0;
for (auto x : graph[s]) {
if (x == p) continue;
dfs(x, s);
dp[s][1] = (dp[s][1] % MOD * dp[x][0] % MOD) % MOD;
dp[s][1] = (dp[s][1] % MOD + (dp[s][0] % MOD * dp[x][1] % MOD) % MOD) % MOD;
dp[s][0] = (dp[s][0] % MOD * dp[x][0] % MOD) % MOD;
}
if (col[s] == 1)
dp[s][1] = dp[s][0] % MOD;
else
dp[s][0] = (dp[s][0] % MOD + dp[s][1] % MOD) % MOD;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int u;
cin >> u;
graph[u].push_back(i);
graph[i].push_back(u);
}
for (int i = 0; i < n; i++) cin >> col[i];
dfs(0, -1);
cout << dp[0][1] % MOD << "\n";
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
int n, dp[MAXN][2], mod = 1e9 + 7;
bool A[MAXN];
vector<int> g[MAXN];
int plu(int a, int b) { return (a + b) % mod; }
int mul(int a, int b) { return ((long long)a * b) % (long long)mod; }
void dfs(int u, int p) {
dp[u][0] = 1, dp[u][1] = 0;
for (auto v : g[u])
if (v != p) {
dfs(v, u);
dp[u][1] = plu(mul(dp[u][1], dp[v][0]), mul(dp[u][0], dp[v][1]));
dp[u][0] = mul(dp[u][0], dp[v][0]);
}
if (A[u])
dp[u][1] = dp[u][0];
else
dp[u][0] = plu(dp[u][0], dp[u][1]);
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; ++i) {
int v;
scanf("%d", &v);
g[i].emplace_back(v), g[v].emplace_back(i);
}
for (int i = 0; i < n; ++i) scanf("%d", A + i);
dfs(0, -1);
printf("%d\n", dp[0][1]);
}
|
### Prompt
Generate a CPP solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
int n, dp[MAXN][2], mod = 1e9 + 7;
bool A[MAXN];
vector<int> g[MAXN];
int plu(int a, int b) { return (a + b) % mod; }
int mul(int a, int b) { return ((long long)a * b) % (long long)mod; }
void dfs(int u, int p) {
dp[u][0] = 1, dp[u][1] = 0;
for (auto v : g[u])
if (v != p) {
dfs(v, u);
dp[u][1] = plu(mul(dp[u][1], dp[v][0]), mul(dp[u][0], dp[v][1]));
dp[u][0] = mul(dp[u][0], dp[v][0]);
}
if (A[u])
dp[u][1] = dp[u][0];
else
dp[u][0] = plu(dp[u][0], dp[u][1]);
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; ++i) {
int v;
scanf("%d", &v);
g[i].emplace_back(v), g[v].emplace_back(i);
}
for (int i = 0; i < n; ++i) scanf("%d", A + i);
dfs(0, -1);
printf("%d\n", dp[0][1]);
}
```
|
#include <bits/stdc++.h>
using namespace std;
struct node {
node *next;
int where;
} * first[100001], a[1000001];
int n, l, w[100001], c[100001];
const int p = 1000000007;
long long f[100001][2], g[2];
inline void makelist(int x, int y) {
a[++l].where = y;
a[l].next = first[x];
first[x] = &a[l];
}
int main() {
scanf("%d", &n);
memset(first, 0, sizeof(first));
l = 0;
for (int i = 2; i <= n; i++) {
int x;
scanf("%d", &x);
++x;
makelist(x, i);
}
for (int i = 1; i <= n; i++) scanf("%d", &w[i]);
memset(f, 0, sizeof(f));
for (int i = n; i; --i) {
if (w[i]) {
f[i][0] = 0;
f[i][1] = 1;
for (node *x = first[i]; x; x = x->next)
f[i][1] *= (f[x->where][0] + f[x->where][1]) % p, f[i][1] %= p;
} else {
f[i][0] = 1;
g[0] = 1;
g[1] = 0;
for (node *x = first[i]; x; x = x->next) {
f[i][0] *= (f[x->where][0] + f[x->where][1]) % p, f[i][0] %= p;
g[1] = g[0] * f[x->where][1] % p +
g[1] * (f[x->where][0] + f[x->where][1]) % p;
g[1] %= p;
g[0] *= (f[x->where][0] + f[x->where][1]);
g[0] %= p;
}
f[i][1] = g[1];
}
}
cout << f[1][1] << endl;
}
|
### Prompt
Generate a Cpp solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
struct node {
node *next;
int where;
} * first[100001], a[1000001];
int n, l, w[100001], c[100001];
const int p = 1000000007;
long long f[100001][2], g[2];
inline void makelist(int x, int y) {
a[++l].where = y;
a[l].next = first[x];
first[x] = &a[l];
}
int main() {
scanf("%d", &n);
memset(first, 0, sizeof(first));
l = 0;
for (int i = 2; i <= n; i++) {
int x;
scanf("%d", &x);
++x;
makelist(x, i);
}
for (int i = 1; i <= n; i++) scanf("%d", &w[i]);
memset(f, 0, sizeof(f));
for (int i = n; i; --i) {
if (w[i]) {
f[i][0] = 0;
f[i][1] = 1;
for (node *x = first[i]; x; x = x->next)
f[i][1] *= (f[x->where][0] + f[x->where][1]) % p, f[i][1] %= p;
} else {
f[i][0] = 1;
g[0] = 1;
g[1] = 0;
for (node *x = first[i]; x; x = x->next) {
f[i][0] *= (f[x->where][0] + f[x->where][1]) % p, f[i][0] %= p;
g[1] = g[0] * f[x->where][1] % p +
g[1] * (f[x->where][0] + f[x->where][1]) % p;
g[1] %= p;
g[0] *= (f[x->where][0] + f[x->where][1]);
g[0] %= p;
}
f[i][1] = g[1];
}
}
cout << f[1][1] << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000 * 1000 * 1000 + 7;
int power(int b, int n) {
if (n == 1) {
return b % MOD;
} else if (n % 2 == 0) {
int t = power(b, n / 2);
return (1LL * t * t) % MOD;
} else {
return (1LL * b * power(b, n - 1)) % MOD;
}
}
int MODdivide(int a, int b) { return (1LL * a * power(b, MOD - 2)) % MOD; }
void dfs(int curr, const vector<vector<int>>& gr, vector<pair<int, int>>& dp,
vector<int>& color, int parent = -1) {
int sum = 1;
for (int next : gr[curr]) {
if (next == parent) {
continue;
}
dfs(next, gr, dp, color, curr);
sum = (1LL * sum * (dp[next].first + dp[next].second)) % MOD;
}
if (gr[curr].size() == 1 && curr > 0) {
dp[curr].first = (color[curr] == 0 ? 1 : 0);
dp[curr].second = (color[curr] == 1 ? 1 : 0);
return;
}
if (color[curr] == 1) {
dp[curr].first = 0;
dp[curr].second = sum;
} else {
dp[curr].first = sum;
for (int next : gr[curr]) {
if (next == parent) {
continue;
}
dp[curr].second = (1LL * dp[curr].second +
1LL * dp[next].second *
MODdivide(sum, dp[next].first + dp[next].second)) %
MOD;
}
}
return;
}
int main() {
int n;
cin >> n;
vector<vector<int>> gr(n);
for (int i = 0; i < n - 1; ++i) {
int p;
cin >> p;
gr[p].push_back(i + 1);
gr[i + 1].push_back(p);
}
vector<int> color(n);
for (int i = 0; i < n; ++i) {
cin >> color[i];
}
vector<pair<int, int>> dp(n, {0, 0});
dfs(0, gr, dp, color);
cout << dp[0].second << endl;
return 0;
}
|
### Prompt
Construct a Cpp code solution to the problem outlined:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000 * 1000 * 1000 + 7;
int power(int b, int n) {
if (n == 1) {
return b % MOD;
} else if (n % 2 == 0) {
int t = power(b, n / 2);
return (1LL * t * t) % MOD;
} else {
return (1LL * b * power(b, n - 1)) % MOD;
}
}
int MODdivide(int a, int b) { return (1LL * a * power(b, MOD - 2)) % MOD; }
void dfs(int curr, const vector<vector<int>>& gr, vector<pair<int, int>>& dp,
vector<int>& color, int parent = -1) {
int sum = 1;
for (int next : gr[curr]) {
if (next == parent) {
continue;
}
dfs(next, gr, dp, color, curr);
sum = (1LL * sum * (dp[next].first + dp[next].second)) % MOD;
}
if (gr[curr].size() == 1 && curr > 0) {
dp[curr].first = (color[curr] == 0 ? 1 : 0);
dp[curr].second = (color[curr] == 1 ? 1 : 0);
return;
}
if (color[curr] == 1) {
dp[curr].first = 0;
dp[curr].second = sum;
} else {
dp[curr].first = sum;
for (int next : gr[curr]) {
if (next == parent) {
continue;
}
dp[curr].second = (1LL * dp[curr].second +
1LL * dp[next].second *
MODdivide(sum, dp[next].first + dp[next].second)) %
MOD;
}
}
return;
}
int main() {
int n;
cin >> n;
vector<vector<int>> gr(n);
for (int i = 0; i < n - 1; ++i) {
int p;
cin >> p;
gr[p].push_back(i + 1);
gr[i + 1].push_back(p);
}
vector<int> color(n);
for (int i = 0; i < n; ++i) {
cin >> color[i];
}
vector<pair<int, int>> dp(n, {0, 0});
dfs(0, gr, dp, color);
cout << dp[0].second << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1e5 + 5, MOD = 1e9 + 7;
long long n;
long long par[MAXN];
long long cnt1[MAXN];
long long cnt2[MAXN];
long long dp1[MAXN];
long long dp2[MAXN];
long long color[MAXN];
bool subtree[MAXN];
vector<long long> G[MAXN];
void dfs_up1(long long v = 0) {
for (long long i = 0; i < G[v].size(); i++) {
dfs_up1(G[v][i]);
subtree[v] |= subtree[G[v][i]];
}
subtree[v] |= color[v];
}
void dfs_up2(long long v = 0) {
for (long long i = 0; i < G[v].size(); i++) dfs_up2(G[v][i]);
if (!subtree[v]) {
dp1[v] = 0;
dp2[v] = 1;
return;
}
if (color[v]) {
dp1[v] = 1;
for (long long i = 0; i < G[v].size(); i++)
if (subtree[G[v][i]]) {
if (color[G[v][i]]) dp1[v] = (dp1[v] * dp1[G[v][i]]) % MOD;
if (!color[G[v][i]])
dp1[v] = (dp1[v] * (dp1[G[v][i]] + dp2[G[v][i]])) % MOD;
}
dp2[v] = 0;
return;
}
long long bu = 1, num = 0;
for (long long i = 0; i < G[v].size(); i++)
if (subtree[G[v][i]]) {
if (color[G[v][i]]) {
num++;
bu = (bu * dp1[G[v][i]]) % MOD;
}
if (!color[G[v][i]]) bu = (bu * (dp1[G[v][i]] + dp2[G[v][i]])) % MOD;
}
dp1[v] = num * bu % MOD;
bu = 1;
num = 0;
for (long long i = 0; i < G[v].size(); i++)
if (subtree[G[v][i]]) {
if (color[G[v][i]]) bu = (bu * dp1[G[v][i]]) % MOD;
if (!color[G[v][i]]) {
cnt1[num] =
((num ? cnt1[num - 1] : 1) * (dp1[G[v][i]] + dp2[G[v][i]])) % MOD;
num++;
}
}
long long ind = num - 1;
for (long long i = G[v].size() - 1; ~i; i--)
if (subtree[G[v][i]]) {
if (!color[G[v][i]]) {
cnt2[ind] = ((ind != num - 1 ? cnt2[ind + 1] : 1) *
(dp1[G[v][i]] + dp2[G[v][i]])) %
MOD;
ind--;
}
}
ind = 0;
for (long long i = 0; i < G[v].size(); i++)
if (subtree[G[v][i]]) {
if (!color[G[v][i]]) {
dp1[v] = (dp1[v] + bu * (ind ? cnt1[ind - 1] : 1) % MOD *
(ind + 1 != num ? cnt2[ind + 1] : 1) % MOD *
dp1[G[v][i]]) %
MOD;
ind++;
}
}
dp2[v] = 1;
for (long long i = 0; i < G[v].size(); i++)
if (subtree[G[v][i]]) {
if (color[G[v][i]]) dp2[v] = (dp2[v] * dp1[G[v][i]]) % MOD;
if (!color[G[v][i]])
dp2[v] = (dp2[v] * (dp1[G[v][i]] + dp2[G[v][i]])) % MOD;
}
}
int main() {
scanf("%lld", &n);
for (long long i = 1; i < n; i++) {
scanf("%lld", &par[i]);
G[par[i]].push_back(i);
}
for (long long i = 0; i < n; i++) scanf("%lld", &color[i]);
dfs_up1();
dfs_up2();
printf("%lld\n", dp1[0]);
return 0;
}
|
### Prompt
Your task is to create a cpp solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1e5 + 5, MOD = 1e9 + 7;
long long n;
long long par[MAXN];
long long cnt1[MAXN];
long long cnt2[MAXN];
long long dp1[MAXN];
long long dp2[MAXN];
long long color[MAXN];
bool subtree[MAXN];
vector<long long> G[MAXN];
void dfs_up1(long long v = 0) {
for (long long i = 0; i < G[v].size(); i++) {
dfs_up1(G[v][i]);
subtree[v] |= subtree[G[v][i]];
}
subtree[v] |= color[v];
}
void dfs_up2(long long v = 0) {
for (long long i = 0; i < G[v].size(); i++) dfs_up2(G[v][i]);
if (!subtree[v]) {
dp1[v] = 0;
dp2[v] = 1;
return;
}
if (color[v]) {
dp1[v] = 1;
for (long long i = 0; i < G[v].size(); i++)
if (subtree[G[v][i]]) {
if (color[G[v][i]]) dp1[v] = (dp1[v] * dp1[G[v][i]]) % MOD;
if (!color[G[v][i]])
dp1[v] = (dp1[v] * (dp1[G[v][i]] + dp2[G[v][i]])) % MOD;
}
dp2[v] = 0;
return;
}
long long bu = 1, num = 0;
for (long long i = 0; i < G[v].size(); i++)
if (subtree[G[v][i]]) {
if (color[G[v][i]]) {
num++;
bu = (bu * dp1[G[v][i]]) % MOD;
}
if (!color[G[v][i]]) bu = (bu * (dp1[G[v][i]] + dp2[G[v][i]])) % MOD;
}
dp1[v] = num * bu % MOD;
bu = 1;
num = 0;
for (long long i = 0; i < G[v].size(); i++)
if (subtree[G[v][i]]) {
if (color[G[v][i]]) bu = (bu * dp1[G[v][i]]) % MOD;
if (!color[G[v][i]]) {
cnt1[num] =
((num ? cnt1[num - 1] : 1) * (dp1[G[v][i]] + dp2[G[v][i]])) % MOD;
num++;
}
}
long long ind = num - 1;
for (long long i = G[v].size() - 1; ~i; i--)
if (subtree[G[v][i]]) {
if (!color[G[v][i]]) {
cnt2[ind] = ((ind != num - 1 ? cnt2[ind + 1] : 1) *
(dp1[G[v][i]] + dp2[G[v][i]])) %
MOD;
ind--;
}
}
ind = 0;
for (long long i = 0; i < G[v].size(); i++)
if (subtree[G[v][i]]) {
if (!color[G[v][i]]) {
dp1[v] = (dp1[v] + bu * (ind ? cnt1[ind - 1] : 1) % MOD *
(ind + 1 != num ? cnt2[ind + 1] : 1) % MOD *
dp1[G[v][i]]) %
MOD;
ind++;
}
}
dp2[v] = 1;
for (long long i = 0; i < G[v].size(); i++)
if (subtree[G[v][i]]) {
if (color[G[v][i]]) dp2[v] = (dp2[v] * dp1[G[v][i]]) % MOD;
if (!color[G[v][i]])
dp2[v] = (dp2[v] * (dp1[G[v][i]] + dp2[G[v][i]])) % MOD;
}
}
int main() {
scanf("%lld", &n);
for (long long i = 1; i < n; i++) {
scanf("%lld", &par[i]);
G[par[i]].push_back(i);
}
for (long long i = 0; i < n; i++) scanf("%lld", &color[i]);
dfs_up1();
dfs_up2();
printf("%lld\n", dp1[0]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long n, u, col[300005], dp[300005][2];
vector<long long> graph[300005];
void dfs(long long u) {
dp[u][col[u]] = 1;
for (long long i = 0; i < graph[u].size(); i++) {
long long v = graph[u][i];
dfs(v);
dp[u][1] = (((dp[u][1] * dp[v][0]) % 1000000007 +
(dp[u][0] * dp[v][1]) % 1000000007) %
1000000007 +
(dp[u][1] * dp[v][1]) % 1000000007) %
1000000007;
dp[u][0] = (dp[u][0] * ((dp[v][0] + dp[v][1]) % 1000000007)) % 1000000007;
}
}
int main() {
scanf("%lld", &n);
for (long long i = 1; i < n; i++) {
scanf("%lld", &u);
graph[u].push_back(i);
}
for (long long i = 0; i < n; i++) scanf("%lld", &col[i]);
dfs(0);
printf("%lld\n", dp[0][1]);
return 0;
}
|
### Prompt
Please create a solution in cpp to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long n, u, col[300005], dp[300005][2];
vector<long long> graph[300005];
void dfs(long long u) {
dp[u][col[u]] = 1;
for (long long i = 0; i < graph[u].size(); i++) {
long long v = graph[u][i];
dfs(v);
dp[u][1] = (((dp[u][1] * dp[v][0]) % 1000000007 +
(dp[u][0] * dp[v][1]) % 1000000007) %
1000000007 +
(dp[u][1] * dp[v][1]) % 1000000007) %
1000000007;
dp[u][0] = (dp[u][0] * ((dp[v][0] + dp[v][1]) % 1000000007)) % 1000000007;
}
}
int main() {
scanf("%lld", &n);
for (long long i = 1; i < n; i++) {
scanf("%lld", &u);
graph[u].push_back(i);
}
for (long long i = 0; i < n; i++) scanf("%lld", &col[i]);
dfs(0);
printf("%lld\n", dp[0][1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const int M = maxn * 100;
const int mod = 1e9 + 7;
vector<int> E[maxn];
long long ans;
long long dp[maxn][2];
int n, v;
int vis[maxn];
void dfs(int u, int fa) {
dp[u][vis[u]] = 1;
for (int i = 0; i < E[u].size(); i++) {
int v = E[u][i];
if (v == fa) continue;
dfs(v, u);
dp[u][1] = ((dp[u][1] * (dp[v][1] + dp[v][0])) + dp[u][0] * dp[v][1]) % mod;
dp[u][0] = (dp[u][0] * (dp[v][1] + dp[v][0])) % mod;
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
scanf("%d", &v);
E[v].push_back(i);
E[i].push_back(v);
}
for (int i = 0; i < n; i++) scanf("%d", &vis[i]);
dfs(0, -1);
cout << dp[0][1] << endl;
}
|
### Prompt
Please provide a Cpp coded solution to the problem described below:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const int M = maxn * 100;
const int mod = 1e9 + 7;
vector<int> E[maxn];
long long ans;
long long dp[maxn][2];
int n, v;
int vis[maxn];
void dfs(int u, int fa) {
dp[u][vis[u]] = 1;
for (int i = 0; i < E[u].size(); i++) {
int v = E[u][i];
if (v == fa) continue;
dfs(v, u);
dp[u][1] = ((dp[u][1] * (dp[v][1] + dp[v][0])) + dp[u][0] * dp[v][1]) % mod;
dp[u][0] = (dp[u][0] * (dp[v][1] + dp[v][0])) % mod;
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
scanf("%d", &v);
E[v].push_back(i);
E[i].push_back(v);
}
for (int i = 0; i < n; i++) scanf("%d", &vis[i]);
dfs(0, -1);
cout << dp[0][1] << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> g;
vector<bool> black;
vector<long long> memblack, memwhite;
const long long mod = 1e9 + 7;
long long calcblack(int x);
long long calcwhite(int x) {
if (black[x]) return 0;
if (memwhite[x] != -1) return memwhite[x];
long long res = 1;
for (int y : g[x]) {
res *= calcwhite(y) + calcblack(y);
res %= mod;
}
memwhite[x] = res;
return res;
}
long long calcblack(int x) {
if (memblack[x] != -1) return memblack[x];
if (black[x]) {
long long res = 1;
for (int y : g[x]) {
res *= calcwhite(y) + calcblack(y);
res %= mod;
}
memblack[x] = res;
return res;
}
vector<long long> prod(g[x].size() + 1, 1);
for (int i = g[x].size() - 1; i >= 0; i--)
prod[i] = (prod[i + 1] * (calcwhite(g[x][i]) + calcblack(g[x][i]))) % mod;
long long res = 0;
long long res2 = 1;
for (int i = 0; i < g[x].size(); i++) {
int y = g[x][i];
res += calcblack(y) * ((res2 * prod[i + 1]) % mod);
res %= mod;
res2 *= calcwhite(y) + calcblack(y);
res2 %= mod;
}
memblack[x] = res;
return res;
}
int main() {
int n;
cin >> n;
g.resize(n);
memblack = memwhite = vector<long long>(n, -1);
for (int i = 1; i < n; i++) {
int j;
cin >> j;
g[j].push_back(i);
}
black.resize(n);
for (int i = 0; i < n; i++) {
int x;
cin >> x;
black[i] = x == 1;
}
cout << calcblack(0) << endl;
}
|
### Prompt
In cpp, your task is to solve the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> g;
vector<bool> black;
vector<long long> memblack, memwhite;
const long long mod = 1e9 + 7;
long long calcblack(int x);
long long calcwhite(int x) {
if (black[x]) return 0;
if (memwhite[x] != -1) return memwhite[x];
long long res = 1;
for (int y : g[x]) {
res *= calcwhite(y) + calcblack(y);
res %= mod;
}
memwhite[x] = res;
return res;
}
long long calcblack(int x) {
if (memblack[x] != -1) return memblack[x];
if (black[x]) {
long long res = 1;
for (int y : g[x]) {
res *= calcwhite(y) + calcblack(y);
res %= mod;
}
memblack[x] = res;
return res;
}
vector<long long> prod(g[x].size() + 1, 1);
for (int i = g[x].size() - 1; i >= 0; i--)
prod[i] = (prod[i + 1] * (calcwhite(g[x][i]) + calcblack(g[x][i]))) % mod;
long long res = 0;
long long res2 = 1;
for (int i = 0; i < g[x].size(); i++) {
int y = g[x][i];
res += calcblack(y) * ((res2 * prod[i + 1]) % mod);
res %= mod;
res2 *= calcwhite(y) + calcblack(y);
res2 %= mod;
}
memblack[x] = res;
return res;
}
int main() {
int n;
cin >> n;
g.resize(n);
memblack = memwhite = vector<long long>(n, -1);
for (int i = 1; i < n; i++) {
int j;
cin >> j;
g[j].push_back(i);
}
black.resize(n);
for (int i = 0; i < n; i++) {
int x;
cin >> x;
black[i] = x == 1;
}
cout << calcblack(0) << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long maxinput = 1e5 + 4;
long long n, col[maxinput], parent[maxinput];
vector<long long> adj[maxinput];
long long dp[maxinput][2];
long long expo(long long base, long long exponent, long long mod) {
long long ans = 1;
while (exponent != 0) {
if ((exponent & 1) == 1) {
ans = ans * base;
ans = ans % mod;
}
base = base * base;
base %= mod;
exponent >>= 1;
}
return ans % mod;
}
long long inv(long long x) { return expo(x, 1000000007 - 2, 1000000007); }
void dfs(long long u) {
long long v;
dp[u][0] = 0LL;
dp[u][1] = 0LL;
long long prod = 1LL;
long long temp_sum = 0LL;
for (long long j = 0; j < adj[u].size(); j++) {
v = adj[u][j];
if (v != parent[u]) {
parent[v] = u;
dfs(v);
prod *= (dp[v][0] + dp[v][1]);
prod %= 1000000007;
temp_sum += (dp[v][1] * inv(dp[v][0] + dp[v][1]));
temp_sum %= 1000000007;
}
}
temp_sum = (temp_sum * prod) % 1000000007;
if ((adj[u].size() == 1) && adj[u][0] == parent[u]) {
if (col[u] == 1)
dp[u][1] = 1LL;
else
dp[u][0] = 1LL;
return;
}
if (col[u] == 0) {
dp[u][0] = prod;
dp[u][0] %= 1000000007;
dp[u][1] = temp_sum;
dp[u][1] %= 1000000007;
} else {
dp[u][0] = 0;
dp[u][1] = prod;
dp[u][1] %= 1000000007;
}
}
int main() {
cin >> n;
long long u, v;
for (long long i = 1; i < n; i++) {
cin >> u;
adj[u].push_back(i);
adj[i].push_back(u);
}
for (long long i = 0; i < n; i++) cin >> col[i];
parent[0] = -1;
dfs(0);
cout << dp[0][1] << endl;
}
|
### Prompt
Construct a cpp code solution to the problem outlined:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long maxinput = 1e5 + 4;
long long n, col[maxinput], parent[maxinput];
vector<long long> adj[maxinput];
long long dp[maxinput][2];
long long expo(long long base, long long exponent, long long mod) {
long long ans = 1;
while (exponent != 0) {
if ((exponent & 1) == 1) {
ans = ans * base;
ans = ans % mod;
}
base = base * base;
base %= mod;
exponent >>= 1;
}
return ans % mod;
}
long long inv(long long x) { return expo(x, 1000000007 - 2, 1000000007); }
void dfs(long long u) {
long long v;
dp[u][0] = 0LL;
dp[u][1] = 0LL;
long long prod = 1LL;
long long temp_sum = 0LL;
for (long long j = 0; j < adj[u].size(); j++) {
v = adj[u][j];
if (v != parent[u]) {
parent[v] = u;
dfs(v);
prod *= (dp[v][0] + dp[v][1]);
prod %= 1000000007;
temp_sum += (dp[v][1] * inv(dp[v][0] + dp[v][1]));
temp_sum %= 1000000007;
}
}
temp_sum = (temp_sum * prod) % 1000000007;
if ((adj[u].size() == 1) && adj[u][0] == parent[u]) {
if (col[u] == 1)
dp[u][1] = 1LL;
else
dp[u][0] = 1LL;
return;
}
if (col[u] == 0) {
dp[u][0] = prod;
dp[u][0] %= 1000000007;
dp[u][1] = temp_sum;
dp[u][1] %= 1000000007;
} else {
dp[u][0] = 0;
dp[u][1] = prod;
dp[u][1] %= 1000000007;
}
}
int main() {
cin >> n;
long long u, v;
for (long long i = 1; i < n; i++) {
cin >> u;
adj[u].push_back(i);
adj[i].push_back(u);
}
for (long long i = 0; i < n; i++) cin >> col[i];
parent[0] = -1;
dfs(0);
cout << dp[0][1] << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long int Pow(long long int a, long long int b, long long int md,
long long int ans = 1) {
for (; b; b >>= 1, a = a * a % md)
if (b & 1) ans = ans * a % md;
return ans % md;
}
const long long int MAXN = 1e6 + 10;
const long long int INF = 8e18;
const long long int MOD = 1e9 + 7;
long long int A[MAXN], dp[2][MAXN], n;
vector<long long int> adj[MAXN];
long long int inv(long long int x) { return Pow(x, MOD - 2, MOD); }
void DFS(long long int v) {
if ((long long int)adj[v].size() == 0) {
if (A[v])
dp[0][v] = 1;
else
dp[1][v] = 1;
return;
}
long long int sum = 1;
for (long long int u : adj[v]) {
DFS(u);
sum = sum * (dp[0][u] + dp[1][u]) % MOD;
}
if (A[v] == 0)
dp[1][v] = sum;
else {
dp[0][v] = sum;
return;
}
for (long long int u : adj[v]) {
long long int x = sum * inv((dp[0][u] + dp[1][u]) % MOD) % MOD;
dp[0][v] = (dp[0][v] + x * dp[0][u] % MOD) % MOD;
}
}
int main() {
scanf("%lld", &n);
for (long long int i = 2, p; i <= n; i++) {
scanf("%lld", &p);
adj[p + 1].push_back(i);
}
for (long long int i = 1; i <= n; i++) scanf("%lld", &A[i]);
DFS(1);
printf("%lld\n", dp[0][1]);
return 0;
}
|
### Prompt
Develop a solution in cpp to the problem described below:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long int Pow(long long int a, long long int b, long long int md,
long long int ans = 1) {
for (; b; b >>= 1, a = a * a % md)
if (b & 1) ans = ans * a % md;
return ans % md;
}
const long long int MAXN = 1e6 + 10;
const long long int INF = 8e18;
const long long int MOD = 1e9 + 7;
long long int A[MAXN], dp[2][MAXN], n;
vector<long long int> adj[MAXN];
long long int inv(long long int x) { return Pow(x, MOD - 2, MOD); }
void DFS(long long int v) {
if ((long long int)adj[v].size() == 0) {
if (A[v])
dp[0][v] = 1;
else
dp[1][v] = 1;
return;
}
long long int sum = 1;
for (long long int u : adj[v]) {
DFS(u);
sum = sum * (dp[0][u] + dp[1][u]) % MOD;
}
if (A[v] == 0)
dp[1][v] = sum;
else {
dp[0][v] = sum;
return;
}
for (long long int u : adj[v]) {
long long int x = sum * inv((dp[0][u] + dp[1][u]) % MOD) % MOD;
dp[0][v] = (dp[0][v] + x * dp[0][u] % MOD) % MOD;
}
}
int main() {
scanf("%lld", &n);
for (long long int i = 2, p; i <= n; i++) {
scanf("%lld", &p);
adj[p + 1].push_back(i);
}
for (long long int i = 1; i <= n; i++) scanf("%lld", &A[i]);
DFS(1);
printf("%lld\n", dp[0][1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
void exgcd(long long a, long long b, long long& d, long long& x, long long& y) {
if (!b)
d = a, x = 1LL, y = 0LL;
else
exgcd(b, a % b, d, y, x), y -= x * (a / b);
}
long long inv(long long a, long long m) {
long long d, x, y;
exgcd(a, m, d, x, y);
return d == 1LL ? (x + m) % m : -1LL;
}
vector<int> vc[100010];
long long dp[100010][2], MOD = 1000000007;
int root, vis[100010], col[100010];
void dfs(int u) {
int i, j, k, len = vc[u].size();
if (col[u] == 1) {
dp[u][1] = 1;
for (i = 0; i < len; i++) {
dfs(vc[u][i]);
if (col[vc[u][i]] == 1)
dp[u][1] *= dp[vc[u][i]][1];
else
dp[u][1] *= (dp[vc[u][i]][0] + dp[vc[u][i]][1]);
dp[u][1] %= MOD;
}
} else if (col[u] == 0) {
dp[u][0] = 1;
for (i = 0; i < len; i++) {
dfs(vc[u][i]);
if (col[vc[u][i]] == 1)
dp[u][0] *= dp[vc[u][i]][1];
else
dp[u][0] *= (dp[vc[u][i]][0] + dp[vc[u][i]][1]);
dp[u][0] %= MOD;
}
for (i = 0; i < len; i++) {
if (col[vc[u][i]] == 1)
dp[u][1] +=
dp[u][0] * inv(dp[vc[u][i]][1], MOD) % MOD * dp[vc[u][i]][1];
else
dp[u][1] += dp[u][0] * inv(dp[vc[u][i]][0] + dp[vc[u][i]][1], MOD) %
MOD * dp[vc[u][i]][1];
dp[u][1] %= MOD;
}
}
dp[u][1] %= MOD;
dp[u][0] %= MOD;
}
void solve() {
int T, t, n, m, i, j, k = 0, u;
scanf("%d", &n);
for (i = 1; i < n; i++) {
scanf("%d", &u);
vc[u].push_back(i);
}
for (i = 0; i < n; i++) {
scanf("%d", &col[i]);
k += col[i];
}
if (k == 0) {
printf("0\n");
return;
}
if (k == 1) {
printf("1\n");
return;
}
dfs(0);
printf("%I64d\n", dp[0][1]);
}
int main() {
solve();
return 0;
}
|
### Prompt
Your task is to create a CPP solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void exgcd(long long a, long long b, long long& d, long long& x, long long& y) {
if (!b)
d = a, x = 1LL, y = 0LL;
else
exgcd(b, a % b, d, y, x), y -= x * (a / b);
}
long long inv(long long a, long long m) {
long long d, x, y;
exgcd(a, m, d, x, y);
return d == 1LL ? (x + m) % m : -1LL;
}
vector<int> vc[100010];
long long dp[100010][2], MOD = 1000000007;
int root, vis[100010], col[100010];
void dfs(int u) {
int i, j, k, len = vc[u].size();
if (col[u] == 1) {
dp[u][1] = 1;
for (i = 0; i < len; i++) {
dfs(vc[u][i]);
if (col[vc[u][i]] == 1)
dp[u][1] *= dp[vc[u][i]][1];
else
dp[u][1] *= (dp[vc[u][i]][0] + dp[vc[u][i]][1]);
dp[u][1] %= MOD;
}
} else if (col[u] == 0) {
dp[u][0] = 1;
for (i = 0; i < len; i++) {
dfs(vc[u][i]);
if (col[vc[u][i]] == 1)
dp[u][0] *= dp[vc[u][i]][1];
else
dp[u][0] *= (dp[vc[u][i]][0] + dp[vc[u][i]][1]);
dp[u][0] %= MOD;
}
for (i = 0; i < len; i++) {
if (col[vc[u][i]] == 1)
dp[u][1] +=
dp[u][0] * inv(dp[vc[u][i]][1], MOD) % MOD * dp[vc[u][i]][1];
else
dp[u][1] += dp[u][0] * inv(dp[vc[u][i]][0] + dp[vc[u][i]][1], MOD) %
MOD * dp[vc[u][i]][1];
dp[u][1] %= MOD;
}
}
dp[u][1] %= MOD;
dp[u][0] %= MOD;
}
void solve() {
int T, t, n, m, i, j, k = 0, u;
scanf("%d", &n);
for (i = 1; i < n; i++) {
scanf("%d", &u);
vc[u].push_back(i);
}
for (i = 0; i < n; i++) {
scanf("%d", &col[i]);
k += col[i];
}
if (k == 0) {
printf("0\n");
return;
}
if (k == 1) {
printf("1\n");
return;
}
dfs(0);
printf("%I64d\n", dp[0][1]);
}
int main() {
solve();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
struct dpi {
long long zero;
long long black;
};
int n;
vector<vector<int> > edges;
vector<int> colors;
vector<dpi> dp;
vector<int> was;
void recalc(int v, int parent) {
long long last_dp = 1;
pair<long long, long long> last = make_pair(!colors[v], colors[v]);
pair<long long, long long> nlast;
for (int i = 0; i < (int)edges[v].size(); ++i) {
if (edges[v][i] != parent) {
long long newval =
(last_dp * (dp[edges[v][i]].zero + dp[edges[v][i]].black)) % mod;
last_dp = newval;
dp[v].zero = last_dp;
nlast.first =
(last.first * (dp[edges[v][i]].zero + dp[edges[v][i]].black)) % mod;
nlast.second =
(last.second * (dp[edges[v][i]].zero + dp[edges[v][i]].black)) % mod +
(last.first * dp[edges[v][i]].black) % mod;
nlast.second %= mod;
last = nlast;
dp[v].zero = last.first;
dp[v].black = last.second;
}
}
}
void dfs(int v, int parent = -1) {
bool have_son = true;
was[v] = true;
for (int i = 0; i < (int)edges[v].size(); ++i) {
if ((!was[edges[v][i]]) && (edges[v][i] != parent)) {
dfs(edges[v][i], v);
have_son = false;
}
}
if (have_son) {
if (colors[v]) {
dp[v].black = 1;
dp[v].zero = 0;
} else {
dp[v].black = 0;
dp[v].zero = 1;
}
} else {
recalc(v, parent);
}
}
int main() {
cin >> n;
edges.resize(n);
colors.resize(n);
dp.resize(n);
was.resize(n);
for (int i = 1; i < n; ++i) {
int dest;
cin >> dest;
edges[i].push_back(dest);
edges[dest].push_back(i);
}
for (int i = 0; i < n; ++i) {
cin >> colors[i];
}
dfs(0);
cout << dp[0].black << "\n";
return 0;
}
|
### Prompt
Your task is to create a CPP solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
struct dpi {
long long zero;
long long black;
};
int n;
vector<vector<int> > edges;
vector<int> colors;
vector<dpi> dp;
vector<int> was;
void recalc(int v, int parent) {
long long last_dp = 1;
pair<long long, long long> last = make_pair(!colors[v], colors[v]);
pair<long long, long long> nlast;
for (int i = 0; i < (int)edges[v].size(); ++i) {
if (edges[v][i] != parent) {
long long newval =
(last_dp * (dp[edges[v][i]].zero + dp[edges[v][i]].black)) % mod;
last_dp = newval;
dp[v].zero = last_dp;
nlast.first =
(last.first * (dp[edges[v][i]].zero + dp[edges[v][i]].black)) % mod;
nlast.second =
(last.second * (dp[edges[v][i]].zero + dp[edges[v][i]].black)) % mod +
(last.first * dp[edges[v][i]].black) % mod;
nlast.second %= mod;
last = nlast;
dp[v].zero = last.first;
dp[v].black = last.second;
}
}
}
void dfs(int v, int parent = -1) {
bool have_son = true;
was[v] = true;
for (int i = 0; i < (int)edges[v].size(); ++i) {
if ((!was[edges[v][i]]) && (edges[v][i] != parent)) {
dfs(edges[v][i], v);
have_son = false;
}
}
if (have_son) {
if (colors[v]) {
dp[v].black = 1;
dp[v].zero = 0;
} else {
dp[v].black = 0;
dp[v].zero = 1;
}
} else {
recalc(v, parent);
}
}
int main() {
cin >> n;
edges.resize(n);
colors.resize(n);
dp.resize(n);
was.resize(n);
for (int i = 1; i < n; ++i) {
int dest;
cin >> dest;
edges[i].push_back(dest);
edges[dest].push_back(i);
}
for (int i = 0; i < n; ++i) {
cin >> colors[i];
}
dfs(0);
cout << dp[0].black << "\n";
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 11;
const int Mod = 1e9 + 7;
struct Edge {
int next, to;
Edge() {}
Edge(int a, int b) : next(a), to(b) {}
};
int head[N];
Edge err[N << 1];
int nedge;
int isb[N];
long long dp[N][2];
int n;
void add_edge(int a, int b) {
err[++nedge] = Edge(head[a], b);
head[a] = nedge;
err[++nedge] = Edge(head[b], a);
head[b] = nedge;
}
long long inv(long long x, long long p) {
long long ret = 1;
for (int i = p - 2; i; i >>= 1, x = (x * x) % p) {
if (i & 1) ret = (ret * x) % p;
}
return ret;
}
void dfs(int u, int fa) {
if (isb[u])
dp[u][0] = 0, dp[u][1] = 1;
else
dp[u][0] = 1, dp[u][1] = 0;
long long ss = 1, xx = 0;
for (int i = head[u]; i != -1; i = err[i].next) {
int v = err[i].to;
if (v != fa) {
dfs(v, u);
ss *= (dp[v][0] + dp[v][1]);
ss %= Mod;
}
}
for (int i = head[u]; i != -1; i = err[i].next) {
int v = err[i].to;
if (v != fa) {
xx += ((ss * inv(dp[v][0] + dp[v][1], Mod)) % Mod * dp[v][1]) % Mod;
xx %= Mod;
}
}
if (isb[u])
dp[u][0] = 0, dp[u][1] = ss;
else
dp[u][0] = ss, dp[u][1] = xx;
}
int main() {
scanf("%d", &n);
memset(head, -1, sizeof(head));
nedge = -1;
for (int i = 1; i < n; ++i) {
int x;
scanf("%d", &x);
add_edge(i, x);
}
for (int i = 0; i < n; ++i) scanf("%d", &isb[i]);
dfs(0, -1);
printf("%lld\n", dp[0][1]);
}
|
### Prompt
Develop a solution in CPP to the problem described below:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 11;
const int Mod = 1e9 + 7;
struct Edge {
int next, to;
Edge() {}
Edge(int a, int b) : next(a), to(b) {}
};
int head[N];
Edge err[N << 1];
int nedge;
int isb[N];
long long dp[N][2];
int n;
void add_edge(int a, int b) {
err[++nedge] = Edge(head[a], b);
head[a] = nedge;
err[++nedge] = Edge(head[b], a);
head[b] = nedge;
}
long long inv(long long x, long long p) {
long long ret = 1;
for (int i = p - 2; i; i >>= 1, x = (x * x) % p) {
if (i & 1) ret = (ret * x) % p;
}
return ret;
}
void dfs(int u, int fa) {
if (isb[u])
dp[u][0] = 0, dp[u][1] = 1;
else
dp[u][0] = 1, dp[u][1] = 0;
long long ss = 1, xx = 0;
for (int i = head[u]; i != -1; i = err[i].next) {
int v = err[i].to;
if (v != fa) {
dfs(v, u);
ss *= (dp[v][0] + dp[v][1]);
ss %= Mod;
}
}
for (int i = head[u]; i != -1; i = err[i].next) {
int v = err[i].to;
if (v != fa) {
xx += ((ss * inv(dp[v][0] + dp[v][1], Mod)) % Mod * dp[v][1]) % Mod;
xx %= Mod;
}
}
if (isb[u])
dp[u][0] = 0, dp[u][1] = ss;
else
dp[u][0] = ss, dp[u][1] = xx;
}
int main() {
scanf("%d", &n);
memset(head, -1, sizeof(head));
nedge = -1;
for (int i = 1; i < n; ++i) {
int x;
scanf("%d", &x);
add_edge(i, x);
}
for (int i = 0; i < n; ++i) scanf("%d", &isb[i]);
dfs(0, -1);
printf("%lld\n", dp[0][1]);
}
```
|
#include <bits/stdc++.h>
using namespace std;
char string_in_buffer[(int)260];
void fast_scan(int &first) { scanf("%d", &first); }
void fast_scan(long long &first) { scanf("%lld", &first); }
void fast_scan(unsigned long long &first) { scanf("%llu", &first); }
void fast_scan(double &first) { scanf("%lf", &first); }
void fast_scan(long double &first) { scanf("%Lf", &first); }
void fast_scan(char &first) {
scanf("%c", &first);
if (first == '\n') {
fast_scan(first);
}
}
void fast_scan(string &first) {
scanf("%s", string_in_buffer);
first = string(string_in_buffer);
}
template <class TFirst, class TSecond>
void fast_scan(pair<TFirst, TSecond> &p) {
fast_scan(p.first);
fast_scan(p.second);
}
template <class T>
void fast_scan(vector<T> &v) {
for (auto &first : v) fast_scan(first);
}
void fast_print(const int &first) { printf("%d", first); }
void fast_print(const unsigned int &first) { printf("%u", first); }
void fast_print(const long long &first) { printf("%lld", first); }
void fast_print(const unsigned long long &first) { printf("%llu", first); }
void fast_print(const double &first) { printf("%.15lf", first); }
void fast_print(const long double &first) { printf("%.15Lf", first); }
void fast_print(const char &first) { printf("%c", first); };
void fast_print(const string &first) { printf("%s", first.c_str()); }
void fast_print(const char v[]) { fast_print((string)v); }
template <class TFirst, class TSecond>
void fast_print(const pair<TFirst, TSecond> &p) {
fast_print(p.first);
fast_print(' ');
fast_print(p.second);
}
template <class T>
void fast_print(const vector<T> &v) {
if (v.empty()) return;
fast_print(v[0]);
for (int i = 1; i < v.size(); i++) {
fast_print(' ');
fast_print(v[i]);
}
}
template <class T>
void fast_print(const vector<vector<T>> &v) {
if (v.empty()) return;
fast_print(v[0]);
for (int i = 1; i < v.size(); i++) {
fast_print('\n');
fast_print(v[i]);
}
}
template <class T>
void fast_print(const T &v) {
for (const auto &first : v) {
fast_print(first);
fast_print(' ');
}
}
using namespace std;
namespace smart_io {
string print_start = "";
string sep = " ";
bool first_print = false;
void precall_print() {
fast_print(print_start);
print_start = "\n";
first_print = true;
}
void _print(deque<string>) {}
template <class T, class... Args>
void _print(deque<string> names, T elem, Args... args) {
if (!first_print) {
fast_print("\n");
} else {
first_print = false;
}
fast_print(names.front());
fast_print(" = ");
fast_print(elem);
names.pop_front();
_print(names, args...);
}
} // namespace smart_io
template <class T>
ostream &operator,(ostream &os, const T &object) {
if (!smart_io::first_print) {
fast_print(smart_io::sep);
} else {
smart_io::first_print = false;
}
fast_print(object);
return os;
}
template <class T>
istream &operator,(istream &is, T &object) {
fast_scan(object);
return is;
}
namespace std::chrono {
long long get_time_ms() {
return duration_cast<milliseconds>(system_clock::now().time_since_epoch())
.count();
}
}; // namespace std::chrono
namespace typedefs {}
namespace numbers_operation {
template <class T>
T floor_mod(T a, T b) {
if (a % b == 0) return 0;
if (a >= 0 && b >= 0) return a % b;
if (a <= 0 && b <= 0) return a % b;
return abs(b) - (abs(a) % abs(b));
}
} // namespace numbers_operation
using namespace numbers_operation;
using namespace typedefs;
const long long MOD = 1e9 + 7;
long long fast_pow(long long first, long long p) {
if (p == 0) return 1;
if (p & 1) {
return (fast_pow(first, p - 1) * first) % MOD;
} else {
long long sub = fast_pow(first, p / 2);
return (sub * sub) % MOD;
}
}
long long n;
vector<vector<long long>> g;
vector<long long> black;
vector<vector<long long>> mem(3e5, vector<long long>(2, -1));
long long dp(long long v, long long emp) {
if (mem[v][emp] != -1) return mem[v][emp];
long long cnt = 1;
for (long long sub : g[v]) {
cnt *= dp(sub, 1);
cnt %= MOD;
}
long long rez = 0;
if (emp) {
if (black[v]) {
rez += cnt;
} else {
rez += cnt;
for (long long sub : g[v]) {
rez += ((cnt * fast_pow(dp(sub, 1), MOD - 2)) % MOD) * dp(sub, 0);
rez %= MOD;
}
}
} else {
if (black[v]) {
rez += cnt;
} else {
for (long long sub : g[v]) {
rez += ((cnt * fast_pow(dp(sub, 1), MOD - 2)) % MOD) * dp(sub, 0);
rez %= MOD;
}
}
}
return mem[v][emp] = rez % MOD;
}
signed main(signed argc, char *argv[]) {
cin, n;
g.resize(n);
for (long long i = 0; i < (n - 1); i++) {
long long p;
cin, p;
g[p].push_back(i + 1);
}
black.resize(n);
cin, black;
smart_io::precall_print();
cout, dp(0, 0);
}
|
### Prompt
Please formulate a cpp solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
char string_in_buffer[(int)260];
void fast_scan(int &first) { scanf("%d", &first); }
void fast_scan(long long &first) { scanf("%lld", &first); }
void fast_scan(unsigned long long &first) { scanf("%llu", &first); }
void fast_scan(double &first) { scanf("%lf", &first); }
void fast_scan(long double &first) { scanf("%Lf", &first); }
void fast_scan(char &first) {
scanf("%c", &first);
if (first == '\n') {
fast_scan(first);
}
}
void fast_scan(string &first) {
scanf("%s", string_in_buffer);
first = string(string_in_buffer);
}
template <class TFirst, class TSecond>
void fast_scan(pair<TFirst, TSecond> &p) {
fast_scan(p.first);
fast_scan(p.second);
}
template <class T>
void fast_scan(vector<T> &v) {
for (auto &first : v) fast_scan(first);
}
void fast_print(const int &first) { printf("%d", first); }
void fast_print(const unsigned int &first) { printf("%u", first); }
void fast_print(const long long &first) { printf("%lld", first); }
void fast_print(const unsigned long long &first) { printf("%llu", first); }
void fast_print(const double &first) { printf("%.15lf", first); }
void fast_print(const long double &first) { printf("%.15Lf", first); }
void fast_print(const char &first) { printf("%c", first); };
void fast_print(const string &first) { printf("%s", first.c_str()); }
void fast_print(const char v[]) { fast_print((string)v); }
template <class TFirst, class TSecond>
void fast_print(const pair<TFirst, TSecond> &p) {
fast_print(p.first);
fast_print(' ');
fast_print(p.second);
}
template <class T>
void fast_print(const vector<T> &v) {
if (v.empty()) return;
fast_print(v[0]);
for (int i = 1; i < v.size(); i++) {
fast_print(' ');
fast_print(v[i]);
}
}
template <class T>
void fast_print(const vector<vector<T>> &v) {
if (v.empty()) return;
fast_print(v[0]);
for (int i = 1; i < v.size(); i++) {
fast_print('\n');
fast_print(v[i]);
}
}
template <class T>
void fast_print(const T &v) {
for (const auto &first : v) {
fast_print(first);
fast_print(' ');
}
}
using namespace std;
namespace smart_io {
string print_start = "";
string sep = " ";
bool first_print = false;
void precall_print() {
fast_print(print_start);
print_start = "\n";
first_print = true;
}
void _print(deque<string>) {}
template <class T, class... Args>
void _print(deque<string> names, T elem, Args... args) {
if (!first_print) {
fast_print("\n");
} else {
first_print = false;
}
fast_print(names.front());
fast_print(" = ");
fast_print(elem);
names.pop_front();
_print(names, args...);
}
} // namespace smart_io
template <class T>
ostream &operator,(ostream &os, const T &object) {
if (!smart_io::first_print) {
fast_print(smart_io::sep);
} else {
smart_io::first_print = false;
}
fast_print(object);
return os;
}
template <class T>
istream &operator,(istream &is, T &object) {
fast_scan(object);
return is;
}
namespace std::chrono {
long long get_time_ms() {
return duration_cast<milliseconds>(system_clock::now().time_since_epoch())
.count();
}
}; // namespace std::chrono
namespace typedefs {}
namespace numbers_operation {
template <class T>
T floor_mod(T a, T b) {
if (a % b == 0) return 0;
if (a >= 0 && b >= 0) return a % b;
if (a <= 0 && b <= 0) return a % b;
return abs(b) - (abs(a) % abs(b));
}
} // namespace numbers_operation
using namespace numbers_operation;
using namespace typedefs;
const long long MOD = 1e9 + 7;
long long fast_pow(long long first, long long p) {
if (p == 0) return 1;
if (p & 1) {
return (fast_pow(first, p - 1) * first) % MOD;
} else {
long long sub = fast_pow(first, p / 2);
return (sub * sub) % MOD;
}
}
long long n;
vector<vector<long long>> g;
vector<long long> black;
vector<vector<long long>> mem(3e5, vector<long long>(2, -1));
long long dp(long long v, long long emp) {
if (mem[v][emp] != -1) return mem[v][emp];
long long cnt = 1;
for (long long sub : g[v]) {
cnt *= dp(sub, 1);
cnt %= MOD;
}
long long rez = 0;
if (emp) {
if (black[v]) {
rez += cnt;
} else {
rez += cnt;
for (long long sub : g[v]) {
rez += ((cnt * fast_pow(dp(sub, 1), MOD - 2)) % MOD) * dp(sub, 0);
rez %= MOD;
}
}
} else {
if (black[v]) {
rez += cnt;
} else {
for (long long sub : g[v]) {
rez += ((cnt * fast_pow(dp(sub, 1), MOD - 2)) % MOD) * dp(sub, 0);
rez %= MOD;
}
}
}
return mem[v][emp] = rez % MOD;
}
signed main(signed argc, char *argv[]) {
cin, n;
g.resize(n);
for (long long i = 0; i < (n - 1); i++) {
long long p;
cin, p;
g[p].push_back(i + 1);
}
black.resize(n);
cin, black;
smart_io::precall_print();
cout, dp(0, 0);
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
std::cerr << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
std::cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
template <typename T, typename U>
static inline void amin(T& x, U y) {
if (y < x) x = y;
}
template <typename T, typename U>
static inline void amax(T& x, U y) {
if (x < y) x = y;
}
long long max(long long a, long long b) { return (a > b) ? a : b; }
long long min(long long a, long long b) { return (a < b) ? a : b; }
const long long N = 1e5 + 10;
vector<long long> adj[N];
long long inv[N];
long long mark[N];
void modularInverse(long long n, long long prime) {
inv[0] = inv[1] = 1;
for (long long i = 2; i <= n; i++)
inv[i] = inv[prime % i] * (prime - prime / i) % prime;
}
long long power(long long x, unsigned long long y, unsigned long long m) {
if (y == 0) return 1;
long long p = power(x, y / 2, m) % m;
p = (p * p) % m;
return (y % 2 == 0) ? p : (x * p) % m;
}
long long dp[N][2];
void dfs(long long v, long long p) {
for (auto i : adj[v]) {
if (i == p) continue;
dfs(i, v);
}
if (mark[v]) {
dp[v][0] = 0;
dp[v][1] = 1;
for (auto i : adj[v]) {
if (i == p) continue;
dp[v][1] *= (dp[i][0] + dp[i][1]);
dp[v][1] %= 1000000007;
}
} else {
dp[v][0] = 1;
for (auto i : adj[v]) {
if (i == p) continue;
dp[v][0] *= (dp[i][0] + dp[i][1]);
dp[v][0] %= 1000000007;
}
for (auto i : adj[v]) {
if (i == p) continue;
dp[v][1] += (((dp[v][0] *
power(dp[i][0] + dp[i][1], 1000000007 - 2, 1000000007)) %
1000000007) *
dp[i][1]) %
1000000007;
dp[v][1] %= 1000000007;
}
}
}
long long solve() {
long long n;
cin >> n;
for (long long i = 2; i <= n; i++) {
long long x;
cin >> x;
x++;
adj[x].push_back(i);
adj[i].push_back(x);
}
for (long long i = 1; i <= n; i++) {
long long x;
cin >> x;
mark[i] = x;
}
dfs(1, 0);
cout << dp[1][1];
return 0;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t = 1;
modularInverse(N - 1, 1000000007);
while (t--) {
solve();
}
return 0;
}
|
### Prompt
Your challenge is to write a Cpp solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
std::cerr << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
std::cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
template <typename T, typename U>
static inline void amin(T& x, U y) {
if (y < x) x = y;
}
template <typename T, typename U>
static inline void amax(T& x, U y) {
if (x < y) x = y;
}
long long max(long long a, long long b) { return (a > b) ? a : b; }
long long min(long long a, long long b) { return (a < b) ? a : b; }
const long long N = 1e5 + 10;
vector<long long> adj[N];
long long inv[N];
long long mark[N];
void modularInverse(long long n, long long prime) {
inv[0] = inv[1] = 1;
for (long long i = 2; i <= n; i++)
inv[i] = inv[prime % i] * (prime - prime / i) % prime;
}
long long power(long long x, unsigned long long y, unsigned long long m) {
if (y == 0) return 1;
long long p = power(x, y / 2, m) % m;
p = (p * p) % m;
return (y % 2 == 0) ? p : (x * p) % m;
}
long long dp[N][2];
void dfs(long long v, long long p) {
for (auto i : adj[v]) {
if (i == p) continue;
dfs(i, v);
}
if (mark[v]) {
dp[v][0] = 0;
dp[v][1] = 1;
for (auto i : adj[v]) {
if (i == p) continue;
dp[v][1] *= (dp[i][0] + dp[i][1]);
dp[v][1] %= 1000000007;
}
} else {
dp[v][0] = 1;
for (auto i : adj[v]) {
if (i == p) continue;
dp[v][0] *= (dp[i][0] + dp[i][1]);
dp[v][0] %= 1000000007;
}
for (auto i : adj[v]) {
if (i == p) continue;
dp[v][1] += (((dp[v][0] *
power(dp[i][0] + dp[i][1], 1000000007 - 2, 1000000007)) %
1000000007) *
dp[i][1]) %
1000000007;
dp[v][1] %= 1000000007;
}
}
}
long long solve() {
long long n;
cin >> n;
for (long long i = 2; i <= n; i++) {
long long x;
cin >> x;
x++;
adj[x].push_back(i);
adj[i].push_back(x);
}
for (long long i = 1; i <= n; i++) {
long long x;
cin >> x;
mark[i] = x;
}
dfs(1, 0);
cout << dp[1][1];
return 0;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t = 1;
modularInverse(N - 1, 1000000007);
while (t--) {
solve();
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
constexpr ll inf = 1000000000;
constexpr ll mod = 1000000007;
constexpr long double eps = 1e-6;
template <typename T1, typename T2>
ostream& operator<<(ostream& os, pair<T1, T2> p) {
os << to_string(p.first) << " " << to_string(p.second);
return os;
}
template <typename T>
ostream& operator<<(ostream& os, vector<T>& v) {
for (ll i = (0); i < (ll)(v.size()); i++) {
if (i) os << " ";
os << v[i];
}
return os;
}
struct modint {
ll n;
public:
modint(const ll n = 0) : n((n % mod + mod) % mod) {}
static modint pow(modint a, int m) {
modint r = 1;
while (m > 0) {
if (m & 1) {
r *= a;
}
a = (a * a);
m /= 2;
}
return r;
}
modint& operator++() {
*this += 1;
return *this;
}
modint& operator--() {
*this -= 1;
return *this;
}
modint operator++(int) {
modint ret = *this;
*this += 1;
return ret;
}
modint operator--(int) {
modint ret = *this;
*this -= 1;
return ret;
}
modint operator~() const { return (this->pow(n, mod - 2)); }
friend bool operator==(const modint& lhs, const modint& rhs) {
return lhs.n == rhs.n;
}
friend bool operator<(const modint& lhs, const modint& rhs) {
return lhs.n < rhs.n;
}
friend bool operator>(const modint& lhs, const modint& rhs) {
return lhs.n > rhs.n;
}
friend modint& operator+=(modint& lhs, const modint& rhs) {
lhs.n += rhs.n;
if (lhs.n >= mod) lhs.n -= mod;
return lhs;
}
friend modint& operator-=(modint& lhs, const modint& rhs) {
lhs.n -= rhs.n;
if (lhs.n < 0) lhs.n += mod;
return lhs;
}
friend modint& operator*=(modint& lhs, const modint& rhs) {
lhs.n = (lhs.n * rhs.n) % mod;
return lhs;
}
friend modint& operator/=(modint& lhs, const modint& rhs) {
lhs.n = (lhs.n * (~rhs).n) % mod;
return lhs;
}
friend modint operator+(const modint& lhs, const modint& rhs) {
return modint(lhs.n + rhs.n);
}
friend modint operator-(const modint& lhs, const modint& rhs) {
return modint(lhs.n - rhs.n);
}
friend modint operator*(const modint& lhs, const modint& rhs) {
return modint(lhs.n * rhs.n);
}
friend modint operator/(const modint& lhs, const modint& rhs) {
return modint(lhs.n * (~rhs).n);
}
};
istream& operator>>(istream& is, modint m) {
is >> m.n;
return is;
}
ostream& operator<<(ostream& os, modint m) {
os << m.n;
return os;
}
long long extgcd(long long a, long long b, long long& x, long long& y) {
long long d = a;
if (b != 0) {
d = extgcd(b, a % b, y, x);
y -= (a / b) * x;
} else {
x = 1;
y = 0;
}
return d;
}
long long mod_inverse(long long a, long long m) {
long long x, y;
if (extgcd(a, m, x, y) == 1)
return (m + x % m) % m;
else
return -1;
}
vector<long long> fact(1010101 + 1, inf);
long long mod_fact(long long n, long long& e) {
if (fact[0] == inf) {
fact[0] = 1;
if (1010101 != 0) fact[1] = 1;
for (ll i = 2; i <= 1010101; ++i) {
fact[i] = (fact[i - 1] * i) % mod;
}
}
e = 0;
if (n == 0) return 1;
long long res = mod_fact(n / mod, e);
e += n / mod;
if ((n / mod) % 2 != 0) return (res * (mod - fact[n % mod])) % mod;
return (res * fact[n % mod]) % mod;
}
long long mod_comb(long long n, long long k) {
if (n < 0 || k < 0 || n < k) return 0;
long long e1, e2, e3;
long long a1 = mod_fact(n, e1), a2 = mod_fact(k, e2),
a3 = mod_fact(n - k, e3);
if (e1 > e2 + e3) return 0;
return (a1 * mod_inverse((a2 * a3) % mod, mod)) % mod;
}
using mi = modint;
mi mod_pow(mi a, ll n) {
mi ret = 1;
mi tmp = a;
while (n > 0) {
if (n % 2) ret *= tmp;
tmp = tmp * tmp;
n /= 2;
}
return ret;
}
ll gcd(ll a, ll b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int n;
vector<vector<int>> t;
vector<int> x;
pair<mi, mi> dfs(int v, int p) {
pair<mi, mi> ret;
ret.first = 0;
ret.second = 0;
vector<pair<mi, mi>> vec;
for (auto adj : t[v]) {
if (adj == p) continue;
auto tmp = dfs(adj, v);
vec.push_back(tmp);
}
if (vec.empty()) {
if (x[v] == 1)
return {1, 0};
else
return {0, 1};
}
if (x[v] == 1) {
ret.first = 1;
for (auto elm : vec) {
ret.first *= (elm.first + elm.second);
}
return ret;
} else {
ret.first = 0;
ret.second = 1;
mi sum = 1;
for (auto elm : vec) {
ret.second *= (elm.first + elm.second);
sum *= elm.first + elm.second;
}
for (auto elm : vec) {
ret.first += (sum / (elm.first + elm.second)) * elm.first;
}
return ret;
}
return ret;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n;
t.resize(n);
x.resize(n);
for (ll i = (0); i < (ll)(n - 1); i++) {
int p;
cin >> p;
t[i + 1].push_back(p);
t[p].push_back(i + 1);
}
for (ll i = (0); i < (ll)(n); i++) cin >> x[i];
auto ans = dfs(0, -1);
cout << ans.first << endl;
return 0;
}
|
### Prompt
Develop a solution in cpp to the problem described below:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
constexpr ll inf = 1000000000;
constexpr ll mod = 1000000007;
constexpr long double eps = 1e-6;
template <typename T1, typename T2>
ostream& operator<<(ostream& os, pair<T1, T2> p) {
os << to_string(p.first) << " " << to_string(p.second);
return os;
}
template <typename T>
ostream& operator<<(ostream& os, vector<T>& v) {
for (ll i = (0); i < (ll)(v.size()); i++) {
if (i) os << " ";
os << v[i];
}
return os;
}
struct modint {
ll n;
public:
modint(const ll n = 0) : n((n % mod + mod) % mod) {}
static modint pow(modint a, int m) {
modint r = 1;
while (m > 0) {
if (m & 1) {
r *= a;
}
a = (a * a);
m /= 2;
}
return r;
}
modint& operator++() {
*this += 1;
return *this;
}
modint& operator--() {
*this -= 1;
return *this;
}
modint operator++(int) {
modint ret = *this;
*this += 1;
return ret;
}
modint operator--(int) {
modint ret = *this;
*this -= 1;
return ret;
}
modint operator~() const { return (this->pow(n, mod - 2)); }
friend bool operator==(const modint& lhs, const modint& rhs) {
return lhs.n == rhs.n;
}
friend bool operator<(const modint& lhs, const modint& rhs) {
return lhs.n < rhs.n;
}
friend bool operator>(const modint& lhs, const modint& rhs) {
return lhs.n > rhs.n;
}
friend modint& operator+=(modint& lhs, const modint& rhs) {
lhs.n += rhs.n;
if (lhs.n >= mod) lhs.n -= mod;
return lhs;
}
friend modint& operator-=(modint& lhs, const modint& rhs) {
lhs.n -= rhs.n;
if (lhs.n < 0) lhs.n += mod;
return lhs;
}
friend modint& operator*=(modint& lhs, const modint& rhs) {
lhs.n = (lhs.n * rhs.n) % mod;
return lhs;
}
friend modint& operator/=(modint& lhs, const modint& rhs) {
lhs.n = (lhs.n * (~rhs).n) % mod;
return lhs;
}
friend modint operator+(const modint& lhs, const modint& rhs) {
return modint(lhs.n + rhs.n);
}
friend modint operator-(const modint& lhs, const modint& rhs) {
return modint(lhs.n - rhs.n);
}
friend modint operator*(const modint& lhs, const modint& rhs) {
return modint(lhs.n * rhs.n);
}
friend modint operator/(const modint& lhs, const modint& rhs) {
return modint(lhs.n * (~rhs).n);
}
};
istream& operator>>(istream& is, modint m) {
is >> m.n;
return is;
}
ostream& operator<<(ostream& os, modint m) {
os << m.n;
return os;
}
long long extgcd(long long a, long long b, long long& x, long long& y) {
long long d = a;
if (b != 0) {
d = extgcd(b, a % b, y, x);
y -= (a / b) * x;
} else {
x = 1;
y = 0;
}
return d;
}
long long mod_inverse(long long a, long long m) {
long long x, y;
if (extgcd(a, m, x, y) == 1)
return (m + x % m) % m;
else
return -1;
}
vector<long long> fact(1010101 + 1, inf);
long long mod_fact(long long n, long long& e) {
if (fact[0] == inf) {
fact[0] = 1;
if (1010101 != 0) fact[1] = 1;
for (ll i = 2; i <= 1010101; ++i) {
fact[i] = (fact[i - 1] * i) % mod;
}
}
e = 0;
if (n == 0) return 1;
long long res = mod_fact(n / mod, e);
e += n / mod;
if ((n / mod) % 2 != 0) return (res * (mod - fact[n % mod])) % mod;
return (res * fact[n % mod]) % mod;
}
long long mod_comb(long long n, long long k) {
if (n < 0 || k < 0 || n < k) return 0;
long long e1, e2, e3;
long long a1 = mod_fact(n, e1), a2 = mod_fact(k, e2),
a3 = mod_fact(n - k, e3);
if (e1 > e2 + e3) return 0;
return (a1 * mod_inverse((a2 * a3) % mod, mod)) % mod;
}
using mi = modint;
mi mod_pow(mi a, ll n) {
mi ret = 1;
mi tmp = a;
while (n > 0) {
if (n % 2) ret *= tmp;
tmp = tmp * tmp;
n /= 2;
}
return ret;
}
ll gcd(ll a, ll b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int n;
vector<vector<int>> t;
vector<int> x;
pair<mi, mi> dfs(int v, int p) {
pair<mi, mi> ret;
ret.first = 0;
ret.second = 0;
vector<pair<mi, mi>> vec;
for (auto adj : t[v]) {
if (adj == p) continue;
auto tmp = dfs(adj, v);
vec.push_back(tmp);
}
if (vec.empty()) {
if (x[v] == 1)
return {1, 0};
else
return {0, 1};
}
if (x[v] == 1) {
ret.first = 1;
for (auto elm : vec) {
ret.first *= (elm.first + elm.second);
}
return ret;
} else {
ret.first = 0;
ret.second = 1;
mi sum = 1;
for (auto elm : vec) {
ret.second *= (elm.first + elm.second);
sum *= elm.first + elm.second;
}
for (auto elm : vec) {
ret.first += (sum / (elm.first + elm.second)) * elm.first;
}
return ret;
}
return ret;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n;
t.resize(n);
x.resize(n);
for (ll i = (0); i < (ll)(n - 1); i++) {
int p;
cin >> p;
t[i + 1].push_back(p);
t[p].push_back(i + 1);
}
for (ll i = (0); i < (ll)(n); i++) cin >> x[i];
auto ans = dfs(0, -1);
cout << ans.first << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
long long mod = 1e9 + 7;
vector<int> g[N];
int color[N];
long long dp[N][2];
bool vis[N];
void dfs(int src) {
vis[src] = 1;
if (color[src] == 1) {
dp[src][1] = 1;
dp[src][0] = 0;
} else {
dp[src][0] = 1;
dp[src][1] = 0;
}
for (auto nbr : g[src]) {
if (vis[nbr]) continue;
dfs(nbr);
if (color[src] == 1) {
dp[src][1] *= (dp[nbr][0] + dp[nbr][1]) % mod;
dp[src][1] %= mod;
} else {
dp[src][1] *= (dp[nbr][0] + dp[nbr][1]) % mod;
dp[src][1] %= mod;
dp[src][1] += (dp[src][0] * dp[nbr][1]) % mod;
dp[src][1] %= mod;
dp[src][0] *= (dp[nbr][0] + dp[nbr][1]) % mod;
dp[src][0] %= mod;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int v;
cin >> v;
g[v].push_back(i);
g[i].push_back(v);
}
for (int i = 0; i < n; i++) cin >> color[i];
dfs(0);
cout << dp[0][1] % mod;
return 0;
}
|
### Prompt
Please provide a CPP coded solution to the problem described below:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
long long mod = 1e9 + 7;
vector<int> g[N];
int color[N];
long long dp[N][2];
bool vis[N];
void dfs(int src) {
vis[src] = 1;
if (color[src] == 1) {
dp[src][1] = 1;
dp[src][0] = 0;
} else {
dp[src][0] = 1;
dp[src][1] = 0;
}
for (auto nbr : g[src]) {
if (vis[nbr]) continue;
dfs(nbr);
if (color[src] == 1) {
dp[src][1] *= (dp[nbr][0] + dp[nbr][1]) % mod;
dp[src][1] %= mod;
} else {
dp[src][1] *= (dp[nbr][0] + dp[nbr][1]) % mod;
dp[src][1] %= mod;
dp[src][1] += (dp[src][0] * dp[nbr][1]) % mod;
dp[src][1] %= mod;
dp[src][0] *= (dp[nbr][0] + dp[nbr][1]) % mod;
dp[src][0] %= mod;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int v;
cin >> v;
g[v].push_back(i);
g[i].push_back(v);
}
for (int i = 0; i < n; i++) cin >> color[i];
dfs(0);
cout << dp[0][1] % mod;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<vector<long long int>> adj(100005);
vector<long long int> color(100005);
vector<vector<long long int>> dp(100005, vector<long long int>(2, 0));
long long int power(long long int x) {
long long int res = 1;
x %= 1000000007ll;
long long int y = 1000000007ll - 2;
while (y > 0) {
if (y & 1) {
res = (res * x) % 1000000007ll;
}
x = (x * x) % 1000000007ll;
y = y >> 1;
}
return res;
}
void dfs(long long int v, long long int p) {
long long int total = 1;
for (auto child : adj[v]) {
if (child == p) {
continue;
}
dfs(child, v);
total =
(total * ((dp[child][0] + dp[child][1]) % 1000000007ll)) % 1000000007ll;
}
if (color[v] == 1) {
dp[v][0] = 0;
dp[v][1] = total;
}
if (color[v] == 0) {
dp[v][0] = total;
long long int sum = 0;
for (auto child : adj[v]) {
if (child == p) {
continue;
}
sum = (sum +
((total * power((dp[child][0] + dp[child][1]) % 1000000007ll)) %
1000000007ll * dp[child][1]) %
1000000007ll) %
1000000007ll;
}
dp[v][1] = sum;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
long long int n;
cin >> n;
long long int v;
for (long long int i = 0; i < n - 1; i++) {
cin >> v;
adj[v].push_back(i + 1);
adj[i + 1].push_back(v);
}
for (long long int i = 0; i < n; i++) {
cin >> color[i];
}
dfs(0, -1);
cout << dp[0][1];
}
|
### Prompt
Please provide a cpp coded solution to the problem described below:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<vector<long long int>> adj(100005);
vector<long long int> color(100005);
vector<vector<long long int>> dp(100005, vector<long long int>(2, 0));
long long int power(long long int x) {
long long int res = 1;
x %= 1000000007ll;
long long int y = 1000000007ll - 2;
while (y > 0) {
if (y & 1) {
res = (res * x) % 1000000007ll;
}
x = (x * x) % 1000000007ll;
y = y >> 1;
}
return res;
}
void dfs(long long int v, long long int p) {
long long int total = 1;
for (auto child : adj[v]) {
if (child == p) {
continue;
}
dfs(child, v);
total =
(total * ((dp[child][0] + dp[child][1]) % 1000000007ll)) % 1000000007ll;
}
if (color[v] == 1) {
dp[v][0] = 0;
dp[v][1] = total;
}
if (color[v] == 0) {
dp[v][0] = total;
long long int sum = 0;
for (auto child : adj[v]) {
if (child == p) {
continue;
}
sum = (sum +
((total * power((dp[child][0] + dp[child][1]) % 1000000007ll)) %
1000000007ll * dp[child][1]) %
1000000007ll) %
1000000007ll;
}
dp[v][1] = sum;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
long long int n;
cin >> n;
long long int v;
for (long long int i = 0; i < n - 1; i++) {
cin >> v;
adj[v].push_back(i + 1);
adj[i + 1].push_back(v);
}
for (long long int i = 0; i < n; i++) {
cin >> color[i];
}
dfs(0, -1);
cout << dp[0][1];
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
int n, i, j, mark[100005];
long long dp[2][100005];
vector<int> edge[100005];
void solve(int id, int p) {
for (int k = 0; k < edge[id].size(); k++) {
int to = edge[id][k];
if (to == p) continue;
solve(to, id);
}
if (mark[id]) {
long long pd = 1LL;
for (int k = 0; k < edge[id].size(); k++) {
int to = edge[id][k];
if (to == p) continue;
pd = (pd * dp[0][to]) % MOD;
}
dp[1][id] = pd;
dp[0][id] = 0;
} else {
long long pd = 1LL;
vector<long long> num;
vector<long long> l;
vector<long long> r;
for (int k = 0; k < edge[id].size(); k++) {
int to = edge[id][k];
if (to == p) continue;
pd = (pd * dp[0][to]) % MOD;
num.push_back(dp[1][to]);
l.push_back(dp[0][to]);
r.push_back(dp[0][to]);
}
dp[0][id] = pd;
for (int k = 1; k < l.size(); k++) l[k] = (l[k] * l[k - 1]) % MOD;
for (int k = r.size() - 2; k >= 0; k--) r[k] = (r[k] * r[k + 1]) % MOD;
for (int k = 0; k < l.size(); k++) {
long long now = num[k];
if (k - 1 >= 0) now = (now * l[k - 1]) % MOD;
if (k + 1 < r.size()) now = (now * r[k + 1]) % MOD;
dp[1][id] = (dp[1][id] + now) % MOD;
}
}
if (p != -1) dp[0][id] = (dp[0][id] + dp[1][id]) % MOD;
}
int main() {
scanf("%d", &n);
for (i = 1; i < n; i++) {
int x;
scanf("%d", &x);
edge[x].push_back(i);
edge[i].push_back(x);
}
for (i = 0; i < n; i++) scanf("%d", &mark[i]);
solve(0, -1);
printf("%lld\n", dp[1][0]);
}
|
### Prompt
Please create a solution in CPP to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
int n, i, j, mark[100005];
long long dp[2][100005];
vector<int> edge[100005];
void solve(int id, int p) {
for (int k = 0; k < edge[id].size(); k++) {
int to = edge[id][k];
if (to == p) continue;
solve(to, id);
}
if (mark[id]) {
long long pd = 1LL;
for (int k = 0; k < edge[id].size(); k++) {
int to = edge[id][k];
if (to == p) continue;
pd = (pd * dp[0][to]) % MOD;
}
dp[1][id] = pd;
dp[0][id] = 0;
} else {
long long pd = 1LL;
vector<long long> num;
vector<long long> l;
vector<long long> r;
for (int k = 0; k < edge[id].size(); k++) {
int to = edge[id][k];
if (to == p) continue;
pd = (pd * dp[0][to]) % MOD;
num.push_back(dp[1][to]);
l.push_back(dp[0][to]);
r.push_back(dp[0][to]);
}
dp[0][id] = pd;
for (int k = 1; k < l.size(); k++) l[k] = (l[k] * l[k - 1]) % MOD;
for (int k = r.size() - 2; k >= 0; k--) r[k] = (r[k] * r[k + 1]) % MOD;
for (int k = 0; k < l.size(); k++) {
long long now = num[k];
if (k - 1 >= 0) now = (now * l[k - 1]) % MOD;
if (k + 1 < r.size()) now = (now * r[k + 1]) % MOD;
dp[1][id] = (dp[1][id] + now) % MOD;
}
}
if (p != -1) dp[0][id] = (dp[0][id] + dp[1][id]) % MOD;
}
int main() {
scanf("%d", &n);
for (i = 1; i < n; i++) {
int x;
scanf("%d", &x);
edge[x].push_back(i);
edge[i].push_back(x);
}
for (i = 0; i < n; i++) scanf("%d", &mark[i]);
solve(0, -1);
printf("%lld\n", dp[1][0]);
}
```
|
#include <bits/stdc++.h>
const int M = 100010, mod = 1e9 + 7;
int n;
int fa[M], col[M];
template <typename T>
inline void read(T &x) {
x = 0;
char ch = getchar();
int f = 1;
for (; ch < '0' || ch > '9'; ch = getchar())
if (ch == '-') f = -1;
for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
x *= f;
}
int head[M], tot = 0;
struct edge {
int to, next;
} e[M];
inline void add(int u, int v) {
e[++tot] = (edge){v, head[u]};
head[u] = tot;
}
long long dp[M][2];
void dfs(int pos) {
if (col[pos] == 1)
dp[pos][1] = 1;
else
dp[pos][0] = 1;
for (int i = head[pos], v; i; i = e[i].next)
if ((v = e[i].to) != fa[pos]) {
dfs(v);
dp[pos][1] = (dp[pos][1] * (dp[v][0] + dp[v][1]) % mod +
dp[pos][0] * dp[v][1] % mod) %
mod;
dp[pos][0] = dp[pos][0] * (dp[v][0] + dp[v][1]) % mod;
}
}
int main() {
read(n);
for (int i = 2; i <= n; i++) read(fa[i]), fa[i]++, add(fa[i], i);
for (int i = 1; i <= n; i++) read(col[i]);
dfs(1);
printf("%d\n", dp[1][1]);
return 0;
}
|
### Prompt
Develop a solution in Cpp to the problem described below:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
const int M = 100010, mod = 1e9 + 7;
int n;
int fa[M], col[M];
template <typename T>
inline void read(T &x) {
x = 0;
char ch = getchar();
int f = 1;
for (; ch < '0' || ch > '9'; ch = getchar())
if (ch == '-') f = -1;
for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
x *= f;
}
int head[M], tot = 0;
struct edge {
int to, next;
} e[M];
inline void add(int u, int v) {
e[++tot] = (edge){v, head[u]};
head[u] = tot;
}
long long dp[M][2];
void dfs(int pos) {
if (col[pos] == 1)
dp[pos][1] = 1;
else
dp[pos][0] = 1;
for (int i = head[pos], v; i; i = e[i].next)
if ((v = e[i].to) != fa[pos]) {
dfs(v);
dp[pos][1] = (dp[pos][1] * (dp[v][0] + dp[v][1]) % mod +
dp[pos][0] * dp[v][1] % mod) %
mod;
dp[pos][0] = dp[pos][0] * (dp[v][0] + dp[v][1]) % mod;
}
}
int main() {
read(n);
for (int i = 2; i <= n; i++) read(fa[i]), fa[i]++, add(fa[i], i);
for (int i = 1; i <= n; i++) read(col[i]);
dfs(1);
printf("%d\n", dp[1][1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
vector<int> adj[100005];
int n, num, colors[100005];
long long int dp0[100005], dp1[100005];
void dfs(int v, int pv) {
dp0[v] = 1 - colors[v];
dp1[v] = colors[v];
for (auto u : adj[v]) {
if (u == pv) continue;
dfs(u, v);
long long int temp = dp1[v];
dp1[v] =
(temp * dp0[u]) % MOD + (dp1[u] * temp) % MOD + (dp0[v] * dp1[u]) % MOD;
if (dp1[v] >= MOD) dp1[v] %= MOD;
temp = dp0[v];
dp0[v] = (temp * dp0[u]) % MOD + (dp1[u] * temp) % MOD;
if (dp0[v] >= MOD) dp0[v] %= MOD;
}
}
int main() {
cin >> n;
for (int i = 2; i <= n; i++) {
cin >> num;
adj[i].push_back(num + 1);
adj[num + 1].push_back(i);
}
for (int i = 1; i <= n; i++) cin >> colors[i];
dfs(1, 0);
cout << dp1[1] << endl;
return 0;
}
|
### Prompt
Generate a cpp solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
vector<int> adj[100005];
int n, num, colors[100005];
long long int dp0[100005], dp1[100005];
void dfs(int v, int pv) {
dp0[v] = 1 - colors[v];
dp1[v] = colors[v];
for (auto u : adj[v]) {
if (u == pv) continue;
dfs(u, v);
long long int temp = dp1[v];
dp1[v] =
(temp * dp0[u]) % MOD + (dp1[u] * temp) % MOD + (dp0[v] * dp1[u]) % MOD;
if (dp1[v] >= MOD) dp1[v] %= MOD;
temp = dp0[v];
dp0[v] = (temp * dp0[u]) % MOD + (dp1[u] * temp) % MOD;
if (dp0[v] >= MOD) dp0[v] %= MOD;
}
}
int main() {
cin >> n;
for (int i = 2; i <= n; i++) {
cin >> num;
adj[i].push_back(num + 1);
adj[num + 1].push_back(i);
}
for (int i = 1; i <= n; i++) cin >> colors[i];
dfs(1, 0);
cout << dp1[1] << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[300005];
long long dp[300005][2];
int color[300005];
long long modex(long long a, int b) {
long long ans = 1;
while (b) {
if (b & 1) ans = ans * a % 1000000007;
a = a * a % 1000000007;
b >>= 1;
}
return ans;
}
long long inv(long long a) { return modex(a, 1000000007 - 2); }
void dfs(int u) {
dp[u][0] = 1;
for (int j = 0; j < (int)adj[u].size(); ++j) {
int v = adj[u][j];
dfs(v);
dp[u][0] *= (dp[v][0] + dp[v][1]);
dp[u][0] %= 1000000007;
}
for (int j = 0; j < (int)adj[u].size(); ++j) {
int v = adj[u][j];
dp[u][1] += (dp[v][1] * dp[u][0] % 1000000007) * inv(dp[v][0] + dp[v][1]);
dp[u][1] %= 1000000007;
}
if (color[u] == 1) {
dp[u][1] = dp[u][0];
dp[u][0] = 0;
}
}
int main() {
int n, v;
cin >> n;
for (int i = 1; i < n; ++i) {
scanf("%d", &v);
adj[v].push_back(i);
}
for (int i = 0; i < n; ++i) scanf("%d", &color[i]);
dfs(0);
cout << dp[0][1] << endl;
return 0;
}
|
### Prompt
Please formulate a CPP solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[300005];
long long dp[300005][2];
int color[300005];
long long modex(long long a, int b) {
long long ans = 1;
while (b) {
if (b & 1) ans = ans * a % 1000000007;
a = a * a % 1000000007;
b >>= 1;
}
return ans;
}
long long inv(long long a) { return modex(a, 1000000007 - 2); }
void dfs(int u) {
dp[u][0] = 1;
for (int j = 0; j < (int)adj[u].size(); ++j) {
int v = adj[u][j];
dfs(v);
dp[u][0] *= (dp[v][0] + dp[v][1]);
dp[u][0] %= 1000000007;
}
for (int j = 0; j < (int)adj[u].size(); ++j) {
int v = adj[u][j];
dp[u][1] += (dp[v][1] * dp[u][0] % 1000000007) * inv(dp[v][0] + dp[v][1]);
dp[u][1] %= 1000000007;
}
if (color[u] == 1) {
dp[u][1] = dp[u][0];
dp[u][0] = 0;
}
}
int main() {
int n, v;
cin >> n;
for (int i = 1; i < n; ++i) {
scanf("%d", &v);
adj[v].push_back(i);
}
for (int i = 0; i < n; ++i) scanf("%d", &color[i]);
dfs(0);
cout << dp[0][1] << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000 * 100 + 10;
vector<int> vec[MAXN];
int c[MAXN];
long long dp[2][MAXN];
void dfs(int v) {
vector<int> temp;
long long all = 1;
for (int i = 0; i < (int)vec[v].size(); i++) {
int u = vec[v][i];
dfs(u);
temp.push_back(all);
all = (all * (dp[0][u] + dp[1][u])) % (1000 * 1000 * 1000 + 7);
}
if (c[v]) {
dp[1][v] = all;
dp[0][v] = 0;
} else {
dp[0][v] = all;
all = 1;
for (int i = (int)vec[v].size() - 1; i >= 0; i--) {
int u = vec[v][i];
dp[1][v] = (dp[1][v] +
(((temp[i] * all) % (1000 * 1000 * 1000 + 7)) * dp[1][u]) %
(1000 * 1000 * 1000 + 7)) %
(1000 * 1000 * 1000 + 7);
all = (all * (dp[0][u] + dp[1][u])) % (1000 * 1000 * 1000 + 7);
}
}
}
int main() {
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int a;
cin >> a;
vec[a].push_back(i);
}
for (int i = 0; i < n; i++) cin >> c[i];
dfs(0);
cout << dp[1][0] << endl;
return 0;
}
|
### Prompt
In Cpp, your task is to solve the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000 * 100 + 10;
vector<int> vec[MAXN];
int c[MAXN];
long long dp[2][MAXN];
void dfs(int v) {
vector<int> temp;
long long all = 1;
for (int i = 0; i < (int)vec[v].size(); i++) {
int u = vec[v][i];
dfs(u);
temp.push_back(all);
all = (all * (dp[0][u] + dp[1][u])) % (1000 * 1000 * 1000 + 7);
}
if (c[v]) {
dp[1][v] = all;
dp[0][v] = 0;
} else {
dp[0][v] = all;
all = 1;
for (int i = (int)vec[v].size() - 1; i >= 0; i--) {
int u = vec[v][i];
dp[1][v] = (dp[1][v] +
(((temp[i] * all) % (1000 * 1000 * 1000 + 7)) * dp[1][u]) %
(1000 * 1000 * 1000 + 7)) %
(1000 * 1000 * 1000 + 7);
all = (all * (dp[0][u] + dp[1][u])) % (1000 * 1000 * 1000 + 7);
}
}
}
int main() {
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int a;
cin >> a;
vec[a].push_back(i);
}
for (int i = 0; i < n; i++) cin >> c[i];
dfs(0);
cout << dp[1][0] << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int M = 200000;
int x[M];
long long dp[M][2];
vector<int> vt[M];
int n;
const int mod = 1000000007;
void dfs(int v) {
dp[v][x[v]] = 1;
for (int i = 0; i < vt[v].size(); i++) {
int u = vt[v][i];
dfs(u);
dp[v][1] = ((dp[v][1] * dp[u][0]) % mod + (dp[v][1] * dp[u][1]) % mod +
(dp[v][0] * dp[u][1]) % mod) %
mod;
dp[v][0] =
((dp[v][0] * dp[u][0]) % mod + (dp[v][0] * dp[u][1]) % mod) % mod;
}
}
int main() {
int i, j, k;
while (~scanf("%d", &n)) {
for (i = 0; i <= n; i++) vt[i].clear();
int u, v;
for (i = 1; i < n; i++) {
scanf("%d", &u);
vt[u].push_back(i);
}
for (i = 0; i < n; i++) scanf("%d", &x[i]);
memset(dp, 0, sizeof(dp));
dfs(0);
cout << dp[0][1] << endl;
}
return 0;
}
|
### Prompt
Develop a solution in cpp to the problem described below:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int M = 200000;
int x[M];
long long dp[M][2];
vector<int> vt[M];
int n;
const int mod = 1000000007;
void dfs(int v) {
dp[v][x[v]] = 1;
for (int i = 0; i < vt[v].size(); i++) {
int u = vt[v][i];
dfs(u);
dp[v][1] = ((dp[v][1] * dp[u][0]) % mod + (dp[v][1] * dp[u][1]) % mod +
(dp[v][0] * dp[u][1]) % mod) %
mod;
dp[v][0] =
((dp[v][0] * dp[u][0]) % mod + (dp[v][0] * dp[u][1]) % mod) % mod;
}
}
int main() {
int i, j, k;
while (~scanf("%d", &n)) {
for (i = 0; i <= n; i++) vt[i].clear();
int u, v;
for (i = 1; i < n; i++) {
scanf("%d", &u);
vt[u].push_back(i);
}
for (i = 0; i < n; i++) scanf("%d", &x[i]);
memset(dp, 0, sizeof(dp));
dfs(0);
cout << dp[0][1] << endl;
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<int> graph[100000];
bool colour[100000];
long long dp[100000][2];
void dfs(int vertex, int parent) {
int i, child;
dp[vertex][1] = colour[vertex];
dp[vertex][0] = 1 - colour[vertex];
for (i = 0; i < graph[vertex].size(); i++) {
child = graph[vertex][i];
if (child == parent) continue;
dfs(child, vertex);
if (colour[vertex]) {
dp[vertex][1] *= ((dp[child][0] + dp[child][1]) % 1000000007);
dp[vertex][1] = (dp[vertex][1]) % 1000000007;
} else {
dp[vertex][1] *= ((dp[child][0] + dp[child][1]) % 1000000007);
dp[vertex][1] = (dp[vertex][1]) % 1000000007;
dp[vertex][1] += ((dp[vertex][0] * dp[child][1]) % 1000000007);
dp[vertex][1] = (dp[vertex][1]) % 1000000007;
dp[vertex][0] *= ((dp[child][0] + dp[child][1]) % 1000000007);
dp[vertex][0] = (dp[vertex][0]) % 1000000007;
}
}
}
int main() {
int n;
int i;
int vertex;
scanf("%d", &n);
for (i = 1; i < n; i++) {
scanf("%d", &vertex);
graph[i].push_back(vertex);
graph[vertex].push_back(i);
}
for (i = 0; i < n; i++) scanf("%d", &colour[i]);
dfs(0, -1);
printf("%I64d\n", dp[0][1]);
return 0;
}
|
### Prompt
Please create a solution in Cpp to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> graph[100000];
bool colour[100000];
long long dp[100000][2];
void dfs(int vertex, int parent) {
int i, child;
dp[vertex][1] = colour[vertex];
dp[vertex][0] = 1 - colour[vertex];
for (i = 0; i < graph[vertex].size(); i++) {
child = graph[vertex][i];
if (child == parent) continue;
dfs(child, vertex);
if (colour[vertex]) {
dp[vertex][1] *= ((dp[child][0] + dp[child][1]) % 1000000007);
dp[vertex][1] = (dp[vertex][1]) % 1000000007;
} else {
dp[vertex][1] *= ((dp[child][0] + dp[child][1]) % 1000000007);
dp[vertex][1] = (dp[vertex][1]) % 1000000007;
dp[vertex][1] += ((dp[vertex][0] * dp[child][1]) % 1000000007);
dp[vertex][1] = (dp[vertex][1]) % 1000000007;
dp[vertex][0] *= ((dp[child][0] + dp[child][1]) % 1000000007);
dp[vertex][0] = (dp[vertex][0]) % 1000000007;
}
}
}
int main() {
int n;
int i;
int vertex;
scanf("%d", &n);
for (i = 1; i < n; i++) {
scanf("%d", &vertex);
graph[i].push_back(vertex);
graph[vertex].push_back(i);
}
for (i = 0; i < n; i++) scanf("%d", &colour[i]);
dfs(0, -1);
printf("%I64d\n", dp[0][1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const int N = 100000;
int n, root;
vector<vector<int> > a;
bool is_dist[N];
long long dp[N][2];
void read_input() {
scanf("%d ", &n);
a.resize(n);
for (int i = 0; i < n - 1; i++) {
int p;
scanf("%d ", &p);
a[i + 1].push_back(p);
a[p].push_back(i + 1);
}
root = -1;
for (int i = 0; i < n; i++) {
int is_dist_int;
scanf("%d ", &is_dist_int);
is_dist[i] = is_dist_int == 1;
if (root == -1 && is_dist[i]) root = i;
}
}
long long binary_exp(long long x, long long m) {
if (m == 0)
return 1;
else if (m % 2 == 1)
return (binary_exp(x, m - 1) * x) % MOD;
else {
long long val = binary_exp(x, m / 2);
return (val * val) % MOD;
}
}
inline long long mod_inv(long long x) { return binary_exp(x, MOD - 2); }
void dfs(int u, int parent) {
if (is_dist[u]) {
dp[u][0] = 0;
dp[u][1] = 1;
for (int v : a[u]) {
if (v == parent) continue;
dfs(v, u);
dp[u][1] = (dp[u][1] * ((dp[v][0] + dp[v][1]) % MOD)) % MOD;
}
} else {
dp[u][0] = 1;
dp[u][1] = 0;
long long zero_mul = 1;
for (int v : a[u]) {
if (v == parent) continue;
dfs(v, u);
dp[u][0] = (dp[u][0] * (dp[v][0] + dp[v][1])) % MOD;
zero_mul = (zero_mul * (dp[v][0] + dp[v][1])) % MOD;
}
for (int v : a[u]) {
if (v == parent) continue;
dp[u][1] =
(dp[u][1] +
(zero_mul * ((dp[v][1] * mod_inv(dp[v][0] + dp[v][1])) % MOD)) %
MOD) %
MOD;
}
}
}
void solve() {
if (root == -1) {
printf("0\n");
return;
}
dfs(root, -1);
printf("%lld\n", dp[root][1]);
}
int main() {
read_input();
solve();
return 0;
}
|
### Prompt
Generate a Cpp solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const int N = 100000;
int n, root;
vector<vector<int> > a;
bool is_dist[N];
long long dp[N][2];
void read_input() {
scanf("%d ", &n);
a.resize(n);
for (int i = 0; i < n - 1; i++) {
int p;
scanf("%d ", &p);
a[i + 1].push_back(p);
a[p].push_back(i + 1);
}
root = -1;
for (int i = 0; i < n; i++) {
int is_dist_int;
scanf("%d ", &is_dist_int);
is_dist[i] = is_dist_int == 1;
if (root == -1 && is_dist[i]) root = i;
}
}
long long binary_exp(long long x, long long m) {
if (m == 0)
return 1;
else if (m % 2 == 1)
return (binary_exp(x, m - 1) * x) % MOD;
else {
long long val = binary_exp(x, m / 2);
return (val * val) % MOD;
}
}
inline long long mod_inv(long long x) { return binary_exp(x, MOD - 2); }
void dfs(int u, int parent) {
if (is_dist[u]) {
dp[u][0] = 0;
dp[u][1] = 1;
for (int v : a[u]) {
if (v == parent) continue;
dfs(v, u);
dp[u][1] = (dp[u][1] * ((dp[v][0] + dp[v][1]) % MOD)) % MOD;
}
} else {
dp[u][0] = 1;
dp[u][1] = 0;
long long zero_mul = 1;
for (int v : a[u]) {
if (v == parent) continue;
dfs(v, u);
dp[u][0] = (dp[u][0] * (dp[v][0] + dp[v][1])) % MOD;
zero_mul = (zero_mul * (dp[v][0] + dp[v][1])) % MOD;
}
for (int v : a[u]) {
if (v == parent) continue;
dp[u][1] =
(dp[u][1] +
(zero_mul * ((dp[v][1] * mod_inv(dp[v][0] + dp[v][1])) % MOD)) %
MOD) %
MOD;
}
}
}
void solve() {
if (root == -1) {
printf("0\n");
return;
}
dfs(root, -1);
printf("%lld\n", dp[root][1]);
}
int main() {
read_input();
solve();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MX = 2 * 1e5 + 11;
int N, M, k;
char s[MX];
long long dp[MX][2];
vector<int> G[MX];
int arr[MX];
long long md = 1e9 + 7;
void dfs(int x, int p) {
dp[x][0] = arr[x] == 0;
dp[x][1] = arr[x] == 1;
for (int i = 0; i < G[x].size(); i++) {
int ch = G[x][i];
if (ch == p) continue;
dfs(ch, x);
dp[x][1] = dp[x][1] * (dp[ch][1] + dp[ch][0]);
dp[x][1] %= md;
dp[x][1] += dp[x][0] * dp[ch][1];
dp[x][1] %= md;
dp[x][0] = dp[x][0] * (dp[ch][0] + dp[ch][1]);
dp[x][0] %= md;
}
}
int main() {
cin >> N;
for (int i = 2; i <= N; i++) {
int a;
scanf("%d", &a);
G[i].push_back(++a);
G[a].push_back(i);
}
for (int i = 1; i <= N; i++) scanf("%d", &arr[i]);
dfs(1, -1);
cout << dp[1][1] << "\n";
}
|
### Prompt
Construct a Cpp code solution to the problem outlined:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MX = 2 * 1e5 + 11;
int N, M, k;
char s[MX];
long long dp[MX][2];
vector<int> G[MX];
int arr[MX];
long long md = 1e9 + 7;
void dfs(int x, int p) {
dp[x][0] = arr[x] == 0;
dp[x][1] = arr[x] == 1;
for (int i = 0; i < G[x].size(); i++) {
int ch = G[x][i];
if (ch == p) continue;
dfs(ch, x);
dp[x][1] = dp[x][1] * (dp[ch][1] + dp[ch][0]);
dp[x][1] %= md;
dp[x][1] += dp[x][0] * dp[ch][1];
dp[x][1] %= md;
dp[x][0] = dp[x][0] * (dp[ch][0] + dp[ch][1]);
dp[x][0] %= md;
}
}
int main() {
cin >> N;
for (int i = 2; i <= N; i++) {
int a;
scanf("%d", &a);
G[i].push_back(++a);
G[a].push_back(i);
}
for (int i = 1; i <= N; i++) scanf("%d", &arr[i]);
dfs(1, -1);
cout << dp[1][1] << "\n";
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long nax = 100005, mod = 1e9 + 7;
vector<long long> g[nax];
long long c[nax], dp[nax][2] = {{0}};
long long mod_expo(long long a, long long b) {
long long tans = 1;
a = a % mod;
while (b != 0) {
if (b & 1) tans = (1LL * tans * a) % mod;
a = (1LL * a * a) % mod;
b /= 2;
}
return tans;
}
long long mod_inv(long long a) { return mod_expo(a, mod - 2); }
long long sum(long long a, long long b) { return (a + b) % mod; }
long long mul(long long a, long long b) { return (a * b) % mod; }
long long divm(long long a, long long b) { return (a * mod_inv(b)) % mod; }
void dfs(long long u, long long p) {
for (auto &k : g[u])
if (k != p) dfs(k, u);
if (c[u] == 0) {
dp[u][0] = 1;
for (auto &k : g[u])
if (k != p) dp[u][0] = (dp[u][0] * (dp[k][0] + dp[k][1])) % mod;
dp[u][1] = 0;
long long zpi = 1;
for (auto &k : g[u])
if (k != p) zpi = (zpi * (dp[k][0] + dp[k][1])) % mod;
for (auto &k : g[u])
if (k != p)
dp[u][1] =
sum(dp[u][1], mul(divm(zpi, (dp[k][0] + dp[k][1])), dp[k][1]));
} else {
dp[u][0] = 0;
dp[u][1] = 1;
for (auto &k : g[u])
if (k != p) dp[u][1] = (dp[u][1] * (dp[k][0] + dp[k][1])) % mod;
}
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long n;
cin >> n;
for (long long i = 0; i < n - 1; i++) {
long long x;
cin >> x;
g[i + 1].push_back(x);
g[x].push_back(i + 1);
}
for (long long i = 0; i < n; i++) cin >> c[i];
dfs(0, 0);
cout << dp[0][1] << endl;
}
|
### Prompt
Your task is to create a cpp solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long nax = 100005, mod = 1e9 + 7;
vector<long long> g[nax];
long long c[nax], dp[nax][2] = {{0}};
long long mod_expo(long long a, long long b) {
long long tans = 1;
a = a % mod;
while (b != 0) {
if (b & 1) tans = (1LL * tans * a) % mod;
a = (1LL * a * a) % mod;
b /= 2;
}
return tans;
}
long long mod_inv(long long a) { return mod_expo(a, mod - 2); }
long long sum(long long a, long long b) { return (a + b) % mod; }
long long mul(long long a, long long b) { return (a * b) % mod; }
long long divm(long long a, long long b) { return (a * mod_inv(b)) % mod; }
void dfs(long long u, long long p) {
for (auto &k : g[u])
if (k != p) dfs(k, u);
if (c[u] == 0) {
dp[u][0] = 1;
for (auto &k : g[u])
if (k != p) dp[u][0] = (dp[u][0] * (dp[k][0] + dp[k][1])) % mod;
dp[u][1] = 0;
long long zpi = 1;
for (auto &k : g[u])
if (k != p) zpi = (zpi * (dp[k][0] + dp[k][1])) % mod;
for (auto &k : g[u])
if (k != p)
dp[u][1] =
sum(dp[u][1], mul(divm(zpi, (dp[k][0] + dp[k][1])), dp[k][1]));
} else {
dp[u][0] = 0;
dp[u][1] = 1;
for (auto &k : g[u])
if (k != p) dp[u][1] = (dp[u][1] * (dp[k][0] + dp[k][1])) % mod;
}
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long n;
cin >> n;
for (long long i = 0; i < n - 1; i++) {
long long x;
cin >> x;
g[i + 1].push_back(x);
g[x].push_back(i + 1);
}
for (long long i = 0; i < n; i++) cin >> c[i];
dfs(0, 0);
cout << dp[0][1] << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2 * 100 * 1000 + 100;
const long long delta = 1000 * 1000 * 1000 + 7;
int n, col[maxn];
vector<int> adj[maxn];
long long dp[2][maxn];
bool dfsmark[maxn];
void inp();
void dfs(int v);
int main() {
ios ::sync_with_stdio(false);
cin.tie(0);
inp();
dfs(0);
cout << dp[1][0] << '\n';
return 0;
}
void inp() {
cin >> n;
for (int i = 0; i < n - 1; i++) {
int u = i + 1, v;
cin >> v;
adj[v].push_back(u);
adj[u].push_back(v);
}
for (int i = 0; i < n; i++) cin >> col[i];
}
void dfs(int v) {
dfsmark[v] = true;
vector<int> cnt;
for (int i = 0; i < adj[v].size(); i++) {
int u = adj[v][i];
if (!dfsmark[u]) {
dfs(u);
cnt.push_back(u);
}
}
if (!cnt.size()) {
dp[0][v] = !col[v];
dp[1][v] = col[v];
return;
}
if (col[v]) {
dp[0][v] = 0;
long long res1 = 1;
for (int i = 0; i < cnt.size(); i++) {
int u = cnt[i];
res1 = (res1 * (dp[0][u] + dp[1][u])) % delta;
}
dp[1][v] = res1;
return;
}
long long res0 = 1;
for (int i = 0; i < cnt.size(); i++) {
int u = cnt[i];
res0 = (res0 * (dp[0][u] + dp[1][u])) % delta;
}
dp[0][v] = res0;
long long p = 1, res1 = 0;
for (int i = 0; i < cnt.size(); i++) {
int u = cnt[i];
long long tmp = 0;
tmp = (res1 * (dp[0][u] + dp[1][u])) % delta;
tmp = (tmp + p * dp[1][u]) % delta;
res1 = tmp;
p = (p * (dp[0][u] + dp[1][u])) % delta;
}
dp[1][v] = res1;
}
|
### Prompt
Please create a solution in CPP to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2 * 100 * 1000 + 100;
const long long delta = 1000 * 1000 * 1000 + 7;
int n, col[maxn];
vector<int> adj[maxn];
long long dp[2][maxn];
bool dfsmark[maxn];
void inp();
void dfs(int v);
int main() {
ios ::sync_with_stdio(false);
cin.tie(0);
inp();
dfs(0);
cout << dp[1][0] << '\n';
return 0;
}
void inp() {
cin >> n;
for (int i = 0; i < n - 1; i++) {
int u = i + 1, v;
cin >> v;
adj[v].push_back(u);
adj[u].push_back(v);
}
for (int i = 0; i < n; i++) cin >> col[i];
}
void dfs(int v) {
dfsmark[v] = true;
vector<int> cnt;
for (int i = 0; i < adj[v].size(); i++) {
int u = adj[v][i];
if (!dfsmark[u]) {
dfs(u);
cnt.push_back(u);
}
}
if (!cnt.size()) {
dp[0][v] = !col[v];
dp[1][v] = col[v];
return;
}
if (col[v]) {
dp[0][v] = 0;
long long res1 = 1;
for (int i = 0; i < cnt.size(); i++) {
int u = cnt[i];
res1 = (res1 * (dp[0][u] + dp[1][u])) % delta;
}
dp[1][v] = res1;
return;
}
long long res0 = 1;
for (int i = 0; i < cnt.size(); i++) {
int u = cnt[i];
res0 = (res0 * (dp[0][u] + dp[1][u])) % delta;
}
dp[0][v] = res0;
long long p = 1, res1 = 0;
for (int i = 0; i < cnt.size(); i++) {
int u = cnt[i];
long long tmp = 0;
tmp = (res1 * (dp[0][u] + dp[1][u])) % delta;
tmp = (tmp + p * dp[1][u]) % delta;
res1 = tmp;
p = (p * (dp[0][u] + dp[1][u])) % delta;
}
dp[1][v] = res1;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const long long LINF = 1e17;
const double DINF = numeric_limits<double>::max();
const int ITER = 300;
const int MOD = 1e9 + 7;
const double EPS = 1e-10;
const int MAXN = 1e5 + 10;
int n;
vector<int> g[MAXN];
int B[MAXN];
int dp[MAXN][2];
void mod(int v) {
for (int i = 0; i < 2; i++) dp[v][i] %= MOD;
}
void dfs(int v) {
dp[v][0] = 1 - B[v];
dp[v][1] = B[v];
for (int u : g[v]) {
int old[2];
old[0] = dp[v][0];
old[1] = dp[v][1];
dp[v][0] = dp[v][1] = 0;
dfs(u);
dp[v][0] += (1ll * old[0] * dp[u][1]) % MOD;
dp[v][1] += (1ll * old[1] * dp[u][1]) % MOD;
mod(v);
dp[v][1] += (1ll * old[1] * dp[u][0]) % MOD;
mod(v);
dp[v][1] += (1ll * old[0] * dp[u][1]) % MOD;
mod(v);
dp[v][0] += (1ll * old[0] * dp[u][0]) % MOD;
mod(v);
}
}
void solve() {
cin >> n;
for (int i = 1; i < n; i++) {
int p;
cin >> p;
g[p].push_back(i);
}
for (int i = 0; i < n; i++) {
cin >> B[i];
}
dfs(0);
cout << dp[0][1] << endl;
}
int main() {
cout << setprecision(12) << fixed;
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
while (t--) {
solve();
}
}
|
### Prompt
Please formulate a Cpp solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const long long LINF = 1e17;
const double DINF = numeric_limits<double>::max();
const int ITER = 300;
const int MOD = 1e9 + 7;
const double EPS = 1e-10;
const int MAXN = 1e5 + 10;
int n;
vector<int> g[MAXN];
int B[MAXN];
int dp[MAXN][2];
void mod(int v) {
for (int i = 0; i < 2; i++) dp[v][i] %= MOD;
}
void dfs(int v) {
dp[v][0] = 1 - B[v];
dp[v][1] = B[v];
for (int u : g[v]) {
int old[2];
old[0] = dp[v][0];
old[1] = dp[v][1];
dp[v][0] = dp[v][1] = 0;
dfs(u);
dp[v][0] += (1ll * old[0] * dp[u][1]) % MOD;
dp[v][1] += (1ll * old[1] * dp[u][1]) % MOD;
mod(v);
dp[v][1] += (1ll * old[1] * dp[u][0]) % MOD;
mod(v);
dp[v][1] += (1ll * old[0] * dp[u][1]) % MOD;
mod(v);
dp[v][0] += (1ll * old[0] * dp[u][0]) % MOD;
mod(v);
}
}
void solve() {
cin >> n;
for (int i = 1; i < n; i++) {
int p;
cin >> p;
g[p].push_back(i);
}
for (int i = 0; i < n; i++) {
cin >> B[i];
}
dfs(0);
cout << dp[0][1] << endl;
}
int main() {
cout << setprecision(12) << fixed;
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
while (t--) {
solve();
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
int n;
vector<vector<int> > sou;
vector<int> bl;
vector<long long> c, b;
void dfs(int v, int f = -1);
int main() {
scanf("%d", &n);
sou.resize(n);
bl.resize(n);
for (int i = 0; i < n - 1; i++) {
int p;
scanf("%d", &p);
sou[i + 1].push_back(p);
sou[p].push_back(i + 1);
}
for (int i = 0; i < n; i++) scanf("%d", &bl[i]);
c.resize(n);
b.resize(n);
dfs(0);
printf("%d\n", (int)(c[0]));
return 0;
}
void euc(long long a, long long b, long long &x, long long &y) {
if (a < b) {
euc(b, a, y, x);
return;
}
if (b == 0) {
x = 1;
y = 0;
return;
}
long long xx, yy;
euc(b, a % b, xx, yy);
x = yy;
y = xx - (a / b) * yy;
while (y < 0) {
x -= b;
y += a;
}
}
void dfs(int v, int f) {
if (sou[v].size() == 1 && sou[v][0] == f) {
if (bl[v]) {
c[v] = 1;
b[v] = 0;
} else {
c[v] = 0;
b[v] = 1;
}
} else {
if (bl[v]) {
b[v] = 0;
c[v] = 1;
for (int i = 0; i < sou[v].size(); i++) {
int s = sou[v][i];
if (s == f) continue;
dfs(s, v);
c[v] = (c[v] * (b[s] + c[s])) % mod;
}
} else {
b[v] = 1;
c[v] = 0;
long long prod = 1;
for (int i = 0; i < sou[v].size(); i++) {
int s = sou[v][i];
if (s == f) continue;
dfs(s, v);
prod = (prod * (b[s] + c[s])) % mod;
}
for (int i = 0; i < sou[v].size(); i++) {
int s = sou[v][i];
if (s == f) continue;
b[v] = (b[v] * (b[s] + c[s])) % mod;
long long t, trash;
euc((b[s] + c[s]) % mod, mod, t, trash);
t = (t * prod) % mod;
t = (t * c[s]) % mod;
c[v] = (c[v] + t) % mod;
}
}
}
}
|
### Prompt
Your challenge is to write a cpp solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
int n;
vector<vector<int> > sou;
vector<int> bl;
vector<long long> c, b;
void dfs(int v, int f = -1);
int main() {
scanf("%d", &n);
sou.resize(n);
bl.resize(n);
for (int i = 0; i < n - 1; i++) {
int p;
scanf("%d", &p);
sou[i + 1].push_back(p);
sou[p].push_back(i + 1);
}
for (int i = 0; i < n; i++) scanf("%d", &bl[i]);
c.resize(n);
b.resize(n);
dfs(0);
printf("%d\n", (int)(c[0]));
return 0;
}
void euc(long long a, long long b, long long &x, long long &y) {
if (a < b) {
euc(b, a, y, x);
return;
}
if (b == 0) {
x = 1;
y = 0;
return;
}
long long xx, yy;
euc(b, a % b, xx, yy);
x = yy;
y = xx - (a / b) * yy;
while (y < 0) {
x -= b;
y += a;
}
}
void dfs(int v, int f) {
if (sou[v].size() == 1 && sou[v][0] == f) {
if (bl[v]) {
c[v] = 1;
b[v] = 0;
} else {
c[v] = 0;
b[v] = 1;
}
} else {
if (bl[v]) {
b[v] = 0;
c[v] = 1;
for (int i = 0; i < sou[v].size(); i++) {
int s = sou[v][i];
if (s == f) continue;
dfs(s, v);
c[v] = (c[v] * (b[s] + c[s])) % mod;
}
} else {
b[v] = 1;
c[v] = 0;
long long prod = 1;
for (int i = 0; i < sou[v].size(); i++) {
int s = sou[v][i];
if (s == f) continue;
dfs(s, v);
prod = (prod * (b[s] + c[s])) % mod;
}
for (int i = 0; i < sou[v].size(); i++) {
int s = sou[v][i];
if (s == f) continue;
b[v] = (b[v] * (b[s] + c[s])) % mod;
long long t, trash;
euc((b[s] + c[s]) % mod, mod, t, trash);
t = (t * prod) % mod;
t = (t * c[s]) % mod;
c[v] = (c[v] + t) % mod;
}
}
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[100010];
long long int col[100010];
long long int dp[100010][2];
void dfs(int x, int f) {
if (col[x] == 1) {
dp[x][0] = 0;
dp[x][1] = 1;
} else {
dp[x][0] = 1;
dp[x][1] = 0;
}
for (int i = 0; i < adj[x].size(); i++) {
int y = adj[x][i];
if (y != f) {
dfs(y, x);
long long int no_black = dp[x][0];
long long int one_black = dp[x][1];
dp[x][0] = dp[x][1] = 0;
if (dp[y][1] > 0) {
dp[x][1] += one_black * dp[y][1];
dp[x][1] += no_black * dp[y][1];
dp[x][0] += no_black * dp[y][1];
}
if (dp[y][0] > 0) {
dp[x][1] += one_black * dp[y][0];
dp[x][0] += no_black * dp[y][0];
}
dp[x][0] = dp[x][0] % 1000000007;
dp[x][1] = dp[x][1] % 1000000007;
}
}
return;
}
int main() {
int n, x;
cin >> n;
for (int i = 1; i <= n - 1; i++) {
cin >> x;
adj[i].push_back(x);
adj[x].push_back(i);
}
for (int i = 0; i < n; i++) cin >> col[i];
dfs(0, -1);
cout << dp[0][1] << endl;
return 0;
}
|
### Prompt
Your challenge is to write a cpp solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[100010];
long long int col[100010];
long long int dp[100010][2];
void dfs(int x, int f) {
if (col[x] == 1) {
dp[x][0] = 0;
dp[x][1] = 1;
} else {
dp[x][0] = 1;
dp[x][1] = 0;
}
for (int i = 0; i < adj[x].size(); i++) {
int y = adj[x][i];
if (y != f) {
dfs(y, x);
long long int no_black = dp[x][0];
long long int one_black = dp[x][1];
dp[x][0] = dp[x][1] = 0;
if (dp[y][1] > 0) {
dp[x][1] += one_black * dp[y][1];
dp[x][1] += no_black * dp[y][1];
dp[x][0] += no_black * dp[y][1];
}
if (dp[y][0] > 0) {
dp[x][1] += one_black * dp[y][0];
dp[x][0] += no_black * dp[y][0];
}
dp[x][0] = dp[x][0] % 1000000007;
dp[x][1] = dp[x][1] % 1000000007;
}
}
return;
}
int main() {
int n, x;
cin >> n;
for (int i = 1; i <= n - 1; i++) {
cin >> x;
adj[i].push_back(x);
adj[x].push_back(i);
}
for (int i = 0; i < n; i++) cin >> col[i];
dfs(0, -1);
cout << dp[0][1] << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int parent[100100] = {0};
vector<int> child[100100];
int color[100100] = {0};
long long dyn_black[100100] = {0};
long long dyn_white[100100] = {0};
int run(int x, int par) {
if (color[x] == 0) {
dyn_white[x] = 1;
dyn_black[x] = 0;
} else {
dyn_white[x] = 0;
dyn_black[x] = 1;
}
for (int i = 0; i < child[x].size(); i++) run(child[x][i], x);
if (par == -1) return dyn_black[x];
long long parBlack = dyn_black[par];
long long parWhite = dyn_white[par];
dyn_black[par] = parWhite * dyn_black[x] + parBlack * dyn_white[x];
dyn_white[par] = parWhite * dyn_white[x];
dyn_white[par] += parWhite * dyn_black[x];
dyn_black[par] += parBlack * dyn_black[x];
dyn_black[par] %= 1000000007;
dyn_white[par] %= 1000000007;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
scanf("%d", &parent[i]);
child[parent[i]].push_back(i);
}
for (int i = 0; i < n; i++) scanf("%d", &color[i]);
printf("%lld", run(0, -1));
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int parent[100100] = {0};
vector<int> child[100100];
int color[100100] = {0};
long long dyn_black[100100] = {0};
long long dyn_white[100100] = {0};
int run(int x, int par) {
if (color[x] == 0) {
dyn_white[x] = 1;
dyn_black[x] = 0;
} else {
dyn_white[x] = 0;
dyn_black[x] = 1;
}
for (int i = 0; i < child[x].size(); i++) run(child[x][i], x);
if (par == -1) return dyn_black[x];
long long parBlack = dyn_black[par];
long long parWhite = dyn_white[par];
dyn_black[par] = parWhite * dyn_black[x] + parBlack * dyn_white[x];
dyn_white[par] = parWhite * dyn_white[x];
dyn_white[par] += parWhite * dyn_black[x];
dyn_black[par] += parBlack * dyn_black[x];
dyn_black[par] %= 1000000007;
dyn_white[par] %= 1000000007;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
scanf("%d", &parent[i]);
child[parent[i]].push_back(i);
}
for (int i = 0; i < n; i++) scanf("%d", &color[i]);
printf("%lld", run(0, -1));
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;
long long fast_pow(long long a, long long b) {
long long res = 1, p = a;
while (b) {
if (b & 1) (res *= p) %= MOD;
b >>= 1;
(p *= p) %= MOD;
}
return res;
}
vector<int> V[N];
pair<int, int> val[N];
bool has[N];
void dfs(int No, int par) {
long long s1 = 1, s2 = 0;
int chi = 0;
for (auto k : V[No]) {
if (k == par) continue;
dfs(k, No);
if (val[k].first != -1) {
(s1 *= val[k].first) %= MOD;
chi++;
}
}
for (auto k : V[No]) {
if (k == par) continue;
if (val[k].first != -1) {
(s2 += (s1 * fast_pow(val[k].first, MOD - 2)) % MOD * val[k].second %
MOD) %= MOD;
}
}
if (chi == 0) {
val[No] = {1, 1};
if (!has[No]) val[No].first = -1;
return;
}
if (has[No])
val[No] = {s1, s1};
else
val[No] = {s1 + s2, s2};
}
int main() {
int n, x;
scanf("%d", &n);
for (int i = 0; i < n - 1; i++) {
scanf("%d", &x);
V[i + 1].push_back(x);
V[x].push_back(i + 1);
}
for (int i = 0; i < n; i++) scanf("%d", &has[i]);
dfs(0, -1);
cout << val[0].second << '\n';
}
|
### Prompt
Your task is to create a CPP solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;
long long fast_pow(long long a, long long b) {
long long res = 1, p = a;
while (b) {
if (b & 1) (res *= p) %= MOD;
b >>= 1;
(p *= p) %= MOD;
}
return res;
}
vector<int> V[N];
pair<int, int> val[N];
bool has[N];
void dfs(int No, int par) {
long long s1 = 1, s2 = 0;
int chi = 0;
for (auto k : V[No]) {
if (k == par) continue;
dfs(k, No);
if (val[k].first != -1) {
(s1 *= val[k].first) %= MOD;
chi++;
}
}
for (auto k : V[No]) {
if (k == par) continue;
if (val[k].first != -1) {
(s2 += (s1 * fast_pow(val[k].first, MOD - 2)) % MOD * val[k].second %
MOD) %= MOD;
}
}
if (chi == 0) {
val[No] = {1, 1};
if (!has[No]) val[No].first = -1;
return;
}
if (has[No])
val[No] = {s1, s1};
else
val[No] = {s1 + s2, s2};
}
int main() {
int n, x;
scanf("%d", &n);
for (int i = 0; i < n - 1; i++) {
scanf("%d", &x);
V[i + 1].push_back(x);
V[x].push_back(i + 1);
}
for (int i = 0; i < n; i++) scanf("%d", &has[i]);
dfs(0, -1);
cout << val[0].second << '\n';
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long dp[100006][2];
long long a[100006];
vector<int> lista[100005];
void dfs(int nod, int daddy) {
dp[nod][0] = 1;
dp[nod][1] = 0;
for (int i = 0; i < lista[nod].size(); i++) {
int x = lista[nod][i];
if (x == daddy) continue;
dfs(x, nod);
dp[nod][1] = (dp[nod][1] * dp[x][0] + dp[nod][0] * dp[x][1]) % mod;
dp[nod][0] = (dp[x][0] * dp[nod][0]) % mod;
}
if (a[nod])
dp[nod][1] = dp[nod][0];
else
dp[nod][0] = (dp[nod][1] + dp[nod][0]) % mod;
}
int main() {
long long t, n, i, nr;
cin >> n;
for (i = 1; i < n; i++) {
cin >> nr;
lista[i].push_back(nr);
lista[nr].push_back(i);
}
for (i = 0; i < n; i++) cin >> a[i];
dfs(0, -1);
cout << dp[0][1];
return 0;
}
|
### Prompt
Your challenge is to write a Cpp solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long dp[100006][2];
long long a[100006];
vector<int> lista[100005];
void dfs(int nod, int daddy) {
dp[nod][0] = 1;
dp[nod][1] = 0;
for (int i = 0; i < lista[nod].size(); i++) {
int x = lista[nod][i];
if (x == daddy) continue;
dfs(x, nod);
dp[nod][1] = (dp[nod][1] * dp[x][0] + dp[nod][0] * dp[x][1]) % mod;
dp[nod][0] = (dp[x][0] * dp[nod][0]) % mod;
}
if (a[nod])
dp[nod][1] = dp[nod][0];
else
dp[nod][0] = (dp[nod][1] + dp[nod][0]) % mod;
}
int main() {
long long t, n, i, nr;
cin >> n;
for (i = 1; i < n; i++) {
cin >> nr;
lista[i].push_back(nr);
lista[nr].push_back(i);
}
for (i = 0; i < n; i++) cin >> a[i];
dfs(0, -1);
cout << dp[0][1];
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 100100;
const long long Mod = 1e9 + 7;
long long Rec(long long i, long long j, long long c);
long long p[N];
long long x[N];
vector<long long> Adj[N];
vector<long long> Dp[N][2];
int main() {
long long n;
cin >> n;
for (long long i = 2; i <= n; i++)
scanf("%I64d", p + i), Adj[++p[i]].push_back(i);
for (long long i = 1; i <= n; i++) scanf("%I64d", x + i);
for (long long i = 0; i < N; i++)
Dp[i][0] = Dp[i][1] = vector<long long>(Adj[i].size(), -1);
cout << Rec(1, 0, 0);
}
long long Rec(long long i, long long j, long long c) {
if (j == 0) c += x[i];
if (c > 1) return 0;
if (j == Adj[i].size()) return c;
if (Dp[i][c][j] + 1) return Dp[i][c][j];
long long Res1 = (Rec(i, j + 1, c) * Rec(Adj[i][j], 0, 0)) % Mod;
long long Res2 = (c ? Rec(i, j + 1, 1) * Rec(Adj[i][j], 0, 1) : 0) % Mod;
long long Res3 = (!c ? (Rec(i, j + 1, 1) * Rec(Adj[i][j], 0, 0)) % Mod +
(Rec(i, j + 1, 0) * Rec(Adj[i][j], 0, 1)) % Mod
: 0) %
Mod;
return Dp[i][c][j] = (Res1 + Res2 + Res3) % Mod;
}
|
### Prompt
Your challenge is to write a cpp solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long N = 100100;
const long long Mod = 1e9 + 7;
long long Rec(long long i, long long j, long long c);
long long p[N];
long long x[N];
vector<long long> Adj[N];
vector<long long> Dp[N][2];
int main() {
long long n;
cin >> n;
for (long long i = 2; i <= n; i++)
scanf("%I64d", p + i), Adj[++p[i]].push_back(i);
for (long long i = 1; i <= n; i++) scanf("%I64d", x + i);
for (long long i = 0; i < N; i++)
Dp[i][0] = Dp[i][1] = vector<long long>(Adj[i].size(), -1);
cout << Rec(1, 0, 0);
}
long long Rec(long long i, long long j, long long c) {
if (j == 0) c += x[i];
if (c > 1) return 0;
if (j == Adj[i].size()) return c;
if (Dp[i][c][j] + 1) return Dp[i][c][j];
long long Res1 = (Rec(i, j + 1, c) * Rec(Adj[i][j], 0, 0)) % Mod;
long long Res2 = (c ? Rec(i, j + 1, 1) * Rec(Adj[i][j], 0, 1) : 0) % Mod;
long long Res3 = (!c ? (Rec(i, j + 1, 1) * Rec(Adj[i][j], 0, 0)) % Mod +
(Rec(i, j + 1, 0) * Rec(Adj[i][j], 0, 1)) % Mod
: 0) %
Mod;
return Dp[i][c][j] = (Res1 + Res2 + Res3) % Mod;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
void dfs(int akt, int oj, vector<vector<int> >& T, vector<int>& col,
vector<vector<long long> >& dp) {
for (int i = 0; i < T[akt].size(); ++i)
if (T[akt][i] != oj) dfs(T[akt][i], akt, T, col, dp);
if (T[akt].size() == 1 && akt != 0) {
if (col[akt] == 0) {
dp[akt][0] = 1;
dp[akt][1] = 0;
} else {
dp[akt][0] = 0;
dp[akt][1] = 1;
}
} else {
int sa = T[akt].size();
vector<long long> pref(sa), suf(sa);
if (T[akt][0] == oj)
pref[0] = 1;
else
pref[0] = dp[T[akt][0]][0] + dp[T[akt][0]][1];
for (int i = 1; i < sa; ++i) {
pref[i] = pref[i - 1];
if (T[akt][i] != oj)
pref[i] = (pref[i] * (dp[T[akt][i]][0] + dp[T[akt][i]][1])) % MOD;
}
if (T[akt][sa - 1] == oj)
suf[sa - 1] = 1;
else
suf[sa - 1] = dp[T[akt][sa - 1]][0] + dp[T[akt][sa - 1]][1];
for (int i = sa - 2; i >= 0; --i) {
suf[i] = suf[i + 1];
if (T[akt][i] != oj)
suf[i] = (suf[i] * (dp[T[akt][i]][0] + dp[T[akt][i]][1])) % MOD;
}
if (col[akt] == 0) {
dp[akt][1] = 0;
for (int i = 0; i < sa; ++i) {
if (T[akt][i] != oj) {
long long resz = 1;
if (i != 0) resz *= pref[i - 1];
if (i != sa - 1) resz *= suf[i + 1];
resz %= MOD;
resz *= dp[T[akt][i]][1];
resz %= MOD;
dp[akt][1] = (dp[akt][1] + resz) % MOD;
}
}
dp[akt][0] = pref[sa - 1];
} else {
dp[akt][1] = pref[sa - 1];
dp[akt][0] = 0;
}
}
}
int main() {
ios_base::sync_with_stdio(0);
int n;
cin >> n;
vector<vector<int> > T(n);
for (int i = 0; i < n - 1; ++i) {
int p;
cin >> p;
T[i + 1].push_back(p);
T[p].push_back(i + 1);
}
vector<int> col(n);
for (int i = 0; i < n; ++i) cin >> col[i];
vector<vector<long long> > dp(n, vector<long long>(2));
dfs(0, -1, T, col, dp);
cout << dp[0][1];
return 0;
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
void dfs(int akt, int oj, vector<vector<int> >& T, vector<int>& col,
vector<vector<long long> >& dp) {
for (int i = 0; i < T[akt].size(); ++i)
if (T[akt][i] != oj) dfs(T[akt][i], akt, T, col, dp);
if (T[akt].size() == 1 && akt != 0) {
if (col[akt] == 0) {
dp[akt][0] = 1;
dp[akt][1] = 0;
} else {
dp[akt][0] = 0;
dp[akt][1] = 1;
}
} else {
int sa = T[akt].size();
vector<long long> pref(sa), suf(sa);
if (T[akt][0] == oj)
pref[0] = 1;
else
pref[0] = dp[T[akt][0]][0] + dp[T[akt][0]][1];
for (int i = 1; i < sa; ++i) {
pref[i] = pref[i - 1];
if (T[akt][i] != oj)
pref[i] = (pref[i] * (dp[T[akt][i]][0] + dp[T[akt][i]][1])) % MOD;
}
if (T[akt][sa - 1] == oj)
suf[sa - 1] = 1;
else
suf[sa - 1] = dp[T[akt][sa - 1]][0] + dp[T[akt][sa - 1]][1];
for (int i = sa - 2; i >= 0; --i) {
suf[i] = suf[i + 1];
if (T[akt][i] != oj)
suf[i] = (suf[i] * (dp[T[akt][i]][0] + dp[T[akt][i]][1])) % MOD;
}
if (col[akt] == 0) {
dp[akt][1] = 0;
for (int i = 0; i < sa; ++i) {
if (T[akt][i] != oj) {
long long resz = 1;
if (i != 0) resz *= pref[i - 1];
if (i != sa - 1) resz *= suf[i + 1];
resz %= MOD;
resz *= dp[T[akt][i]][1];
resz %= MOD;
dp[akt][1] = (dp[akt][1] + resz) % MOD;
}
}
dp[akt][0] = pref[sa - 1];
} else {
dp[akt][1] = pref[sa - 1];
dp[akt][0] = 0;
}
}
}
int main() {
ios_base::sync_with_stdio(0);
int n;
cin >> n;
vector<vector<int> > T(n);
for (int i = 0; i < n - 1; ++i) {
int p;
cin >> p;
T[i + 1].push_back(p);
T[p].push_back(i + 1);
}
vector<int> col(n);
for (int i = 0; i < n; ++i) cin >> col[i];
vector<vector<long long> > dp(n, vector<long long>(2));
dfs(0, -1, T, col, dp);
cout << dp[0][1];
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long M = 1000000007;
class IO {
static const int BUF_MB = 1;
char buf[BUF_MB * 1024 * 1024];
char *END;
char *pos;
int readfile() {
pos = buf;
END = fread(buf, 1, sizeof(buf), stdin) + buf;
return END - buf;
}
public:
IO() { END = pos = buf; }
int nextInt(int &ret) {
ret = 0;
while (true) {
while (pos < END && !isdigit(*pos)) pos++;
if (pos == END && readfile() == 0)
return -1;
else if (pos != END)
break;
}
while (true) {
while (pos < END && isdigit(*pos)) {
ret = ret * 10 + (*pos) - '0';
pos++;
}
if (pos != END || readfile() == 0) break;
}
return 1;
}
} io;
int cl[100005];
int dp[100005][2];
int to[100005];
int main() {
int n;
io.nextInt(n);
for (int i = 0; i < n; i++) {
dp[i][0] = 1;
dp[i][1] = 0;
}
for (int i = 1; i < n; i++) {
io.nextInt(to[i]);
}
for (int i = 0; i < n; i++) {
io.nextInt(cl[i]);
}
for (int i = n - 1; i >= 0; --i) {
if (cl[i] == 1) {
dp[i][1] = dp[i][0];
dp[i][0] = 0;
}
if (i == 0) {
continue;
}
int nxt = to[i];
dp[nxt][1] = (1ll * dp[nxt][1] * (dp[i][1] + dp[i][0]) +
1ll * dp[nxt][0] * dp[i][1]) %
M;
dp[nxt][0] = 1ll * dp[nxt][0] * (dp[i][1] + dp[i][0]) % M;
}
cout << dp[0][1] << "\n";
return 0;
}
|
### Prompt
Your challenge is to write a cpp solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long M = 1000000007;
class IO {
static const int BUF_MB = 1;
char buf[BUF_MB * 1024 * 1024];
char *END;
char *pos;
int readfile() {
pos = buf;
END = fread(buf, 1, sizeof(buf), stdin) + buf;
return END - buf;
}
public:
IO() { END = pos = buf; }
int nextInt(int &ret) {
ret = 0;
while (true) {
while (pos < END && !isdigit(*pos)) pos++;
if (pos == END && readfile() == 0)
return -1;
else if (pos != END)
break;
}
while (true) {
while (pos < END && isdigit(*pos)) {
ret = ret * 10 + (*pos) - '0';
pos++;
}
if (pos != END || readfile() == 0) break;
}
return 1;
}
} io;
int cl[100005];
int dp[100005][2];
int to[100005];
int main() {
int n;
io.nextInt(n);
for (int i = 0; i < n; i++) {
dp[i][0] = 1;
dp[i][1] = 0;
}
for (int i = 1; i < n; i++) {
io.nextInt(to[i]);
}
for (int i = 0; i < n; i++) {
io.nextInt(cl[i]);
}
for (int i = n - 1; i >= 0; --i) {
if (cl[i] == 1) {
dp[i][1] = dp[i][0];
dp[i][0] = 0;
}
if (i == 0) {
continue;
}
int nxt = to[i];
dp[nxt][1] = (1ll * dp[nxt][1] * (dp[i][1] + dp[i][0]) +
1ll * dp[nxt][0] * dp[i][1]) %
M;
dp[nxt][0] = 1ll * dp[nxt][0] * (dp[i][1] + dp[i][0]) % M;
}
cout << dp[0][1] << "\n";
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<int> g[100010];
bitset<100010> coler;
long long ans;
const int mod = 1e9 + 7;
long long d[100010][2];
void dfs(int x, int p) {
for (int i = 0; i < g[x].size(); i++) {
if (g[x][i] != p) dfs(g[x][i], x);
}
d[x][coler.test(x)] = 1;
for (int i = 0; i < g[x].size(); i++) {
if (g[x][i] == p) continue;
d[x][1] = d[g[x][i]][1] * d[x][0] + d[g[x][i]][0] * d[x][1];
d[x][0] = d[g[x][i]][0] * d[x][0];
d[x][0] %= mod;
d[x][1] %= mod;
}
d[x][0] += d[x][1];
d[x][0] %= mod;
d[x][1] %= mod;
return;
}
int main() {
int n;
while (cin >> n) {
ans = 1;
coler.reset();
memset(d, 0, sizeof(d));
for (int i = 0; i < n; i++) g[i].clear();
int v;
for (int i = 1; i < n; i++) {
scanf("%d", &v);
g[i].push_back(v);
g[v].push_back(i);
}
for (int i = 0; i < n; i++) {
scanf("%d", &v);
if (v == 1) {
coler.set(i);
}
}
dfs(0, 0);
cout << d[0][1] << endl;
}
return 0;
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> g[100010];
bitset<100010> coler;
long long ans;
const int mod = 1e9 + 7;
long long d[100010][2];
void dfs(int x, int p) {
for (int i = 0; i < g[x].size(); i++) {
if (g[x][i] != p) dfs(g[x][i], x);
}
d[x][coler.test(x)] = 1;
for (int i = 0; i < g[x].size(); i++) {
if (g[x][i] == p) continue;
d[x][1] = d[g[x][i]][1] * d[x][0] + d[g[x][i]][0] * d[x][1];
d[x][0] = d[g[x][i]][0] * d[x][0];
d[x][0] %= mod;
d[x][1] %= mod;
}
d[x][0] += d[x][1];
d[x][0] %= mod;
d[x][1] %= mod;
return;
}
int main() {
int n;
while (cin >> n) {
ans = 1;
coler.reset();
memset(d, 0, sizeof(d));
for (int i = 0; i < n; i++) g[i].clear();
int v;
for (int i = 1; i < n; i++) {
scanf("%d", &v);
g[i].push_back(v);
g[v].push_back(i);
}
for (int i = 0; i < n; i++) {
scanf("%d", &v);
if (v == 1) {
coler.set(i);
}
}
dfs(0, 0);
cout << d[0][1] << endl;
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long dp[100005][2];
int color[100005], par[100005];
bool notLeaf[100005];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
scanf("%d", par + i);
notLeaf[par[i]] = true;
}
for (int i = 0; i < n; i++) scanf("%d", color + i);
for (int i = 0; i < n; i++) {
dp[i][0] = 1;
dp[i][1] = color[i];
}
for (int i = n - 1; i > 0; i--) {
if (!notLeaf[i]) {
dp[i][color[i]] = 1;
dp[i][color[i] ^ 1] = 0;
}
if (color[par[i]] == 1) {
dp[par[i]][1] *= dp[i][1] + dp[i][0];
dp[par[i]][1] %= 1000000007LL;
dp[par[i]][0] = 0;
} else {
dp[par[i]][1] *= dp[i][1] + dp[i][0];
dp[par[i]][1] %= 1000000007LL;
dp[par[i]][1] += dp[i][1] * dp[par[i]][0];
dp[par[i]][1] %= 1000000007LL;
dp[par[i]][0] *= dp[i][1] + dp[i][0];
dp[par[i]][0] %= 1000000007LL;
}
}
printf("%I64d\n", dp[0][1]);
return 0;
}
|
### Prompt
In CPP, your task is to solve the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long dp[100005][2];
int color[100005], par[100005];
bool notLeaf[100005];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
scanf("%d", par + i);
notLeaf[par[i]] = true;
}
for (int i = 0; i < n; i++) scanf("%d", color + i);
for (int i = 0; i < n; i++) {
dp[i][0] = 1;
dp[i][1] = color[i];
}
for (int i = n - 1; i > 0; i--) {
if (!notLeaf[i]) {
dp[i][color[i]] = 1;
dp[i][color[i] ^ 1] = 0;
}
if (color[par[i]] == 1) {
dp[par[i]][1] *= dp[i][1] + dp[i][0];
dp[par[i]][1] %= 1000000007LL;
dp[par[i]][0] = 0;
} else {
dp[par[i]][1] *= dp[i][1] + dp[i][0];
dp[par[i]][1] %= 1000000007LL;
dp[par[i]][1] += dp[i][1] * dp[par[i]][0];
dp[par[i]][1] %= 1000000007LL;
dp[par[i]][0] *= dp[i][1] + dp[i][0];
dp[par[i]][0] %= 1000000007LL;
}
}
printf("%I64d\n", dp[0][1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
bool checkbit(int x, int y) { return (x & (1 << y)); }
int setbit(int x, int y) { return (x ^ (1 << y)); }
const int dirs[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int mod = (1e9 + 7);
const int INF = (0x7fffffff);
const int maxn = 1e5 + 7;
vector<int> e[maxn];
bitset<maxn> c;
bitset<maxn> vis;
long long dp[maxn][2];
void dfs(int u) {
vis[u] = 1;
dp[u][1] = c[u];
dp[u][0] = 1 - c[u];
for (auto v : e[u]) {
if (vis[v]) continue;
dfs(v);
long long old[2];
old[0] = dp[u][0];
old[1] = dp[u][1];
dp[u][0] = (old[0] * dp[v][0]);
dp[u][0] = (dp[u][0] + old[0] * dp[v][1]) % mod;
dp[u][1] = (old[1] * dp[v][0]);
dp[u][1] = (dp[u][1] + old[1] * dp[v][1]) % mod;
dp[u][1] = (dp[u][1] + old[0] * dp[v][1]) % mod;
}
}
int main() {
vis.reset();
int n;
scanf("%d", &n);
for (int i = (1); i < (n); i++) {
int p;
scanf("%d", &p);
e[p].push_back(i);
}
for (int i = (0); i < (n); i++) {
int clr;
scanf("%d", &clr);
c[i] = (clr == 1);
}
dfs(0);
printf("%I64d\n", dp[0][1]);
return 0;
}
|
### Prompt
Generate a CPP solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
bool checkbit(int x, int y) { return (x & (1 << y)); }
int setbit(int x, int y) { return (x ^ (1 << y)); }
const int dirs[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int mod = (1e9 + 7);
const int INF = (0x7fffffff);
const int maxn = 1e5 + 7;
vector<int> e[maxn];
bitset<maxn> c;
bitset<maxn> vis;
long long dp[maxn][2];
void dfs(int u) {
vis[u] = 1;
dp[u][1] = c[u];
dp[u][0] = 1 - c[u];
for (auto v : e[u]) {
if (vis[v]) continue;
dfs(v);
long long old[2];
old[0] = dp[u][0];
old[1] = dp[u][1];
dp[u][0] = (old[0] * dp[v][0]);
dp[u][0] = (dp[u][0] + old[0] * dp[v][1]) % mod;
dp[u][1] = (old[1] * dp[v][0]);
dp[u][1] = (dp[u][1] + old[1] * dp[v][1]) % mod;
dp[u][1] = (dp[u][1] + old[0] * dp[v][1]) % mod;
}
}
int main() {
vis.reset();
int n;
scanf("%d", &n);
for (int i = (1); i < (n); i++) {
int p;
scanf("%d", &p);
e[p].push_back(i);
}
for (int i = (0); i < (n); i++) {
int clr;
scanf("%d", &clr);
c[i] = (clr == 1);
}
dfs(0);
printf("%I64d\n", dp[0][1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<long long> G[100010], ter;
long long n, root, c[100010], par[100010], dt[100010][2], ans;
void dfs(int v, int pv) {
dt[v][0] = 1;
dt[v][1] = 0;
for (int w : G[v])
if (w != pv) {
dfs(w, v);
dt[v][1] *= dt[w][0];
dt[v][1] += dt[v][0] * dt[w][1];
dt[v][0] *= dt[w][0];
dt[v][1] %= 1000000007, dt[v][0] %= 1000000007;
}
if (c[v] == 1)
dt[v][1] = dt[v][0];
else
dt[v][0] += dt[v][1];
}
int main() {
ios::sync_with_stdio(0);
cin >> n;
for (int i = 1, t; i < n; i++) {
cin >> t;
G[i].push_back(t), G[t].push_back(i);
}
for (int i = 0; i < n; i++) cin >> c[i];
dfs(0, 0);
cout << dt[0][1] << endl;
return 0;
}
|
### Prompt
Construct a Cpp code solution to the problem outlined:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<long long> G[100010], ter;
long long n, root, c[100010], par[100010], dt[100010][2], ans;
void dfs(int v, int pv) {
dt[v][0] = 1;
dt[v][1] = 0;
for (int w : G[v])
if (w != pv) {
dfs(w, v);
dt[v][1] *= dt[w][0];
dt[v][1] += dt[v][0] * dt[w][1];
dt[v][0] *= dt[w][0];
dt[v][1] %= 1000000007, dt[v][0] %= 1000000007;
}
if (c[v] == 1)
dt[v][1] = dt[v][0];
else
dt[v][0] += dt[v][1];
}
int main() {
ios::sync_with_stdio(0);
cin >> n;
for (int i = 1, t; i < n; i++) {
cin >> t;
G[i].push_back(t), G[t].push_back(i);
}
for (int i = 0; i < n; i++) cin >> c[i];
dfs(0, 0);
cout << dt[0][1] << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long md = 1000000007;
struct node {
long long white, black;
} tree[100010];
int type[100010];
int n, fa[100010];
void output() {
printf("\n-----------------------------\n");
for (int i = 1; i <= n; ++i) {
printf("tree[%d]: white=%lld, black=%lld\n", i, tree[i].white,
tree[i].black);
}
printf("\n-----------------------------\n");
return;
}
void run() {
for (int i = n; i >= 2; --i) {
int t = fa[i];
long long bl = tree[i].black * tree[t].white % md;
bl =
(bl + ((tree[i].black + tree[i].white) % md) * tree[t].black % md) % md;
long long wh =
(((tree[i].black + tree[i].white) % md) * tree[t].white) % md;
tree[t].black = bl;
tree[t].white = wh;
}
cout << tree[1].black;
return;
}
void init() {
cin >> n;
memset(type, 0, sizeof(type));
int top = 0, a, b;
for (int i = 2; i <= n; ++i) {
scanf("%d", &fa[i]);
++fa[i];
}
for (int i = 1; i <= n; ++i) {
scanf("%d", &type[i]);
if (type[i]) {
tree[i].white = 0;
tree[i].black = 1;
} else {
tree[i].white = 1;
tree[i].black = 0;
}
}
run();
return;
}
int main() {
init();
return 0;
}
|
### Prompt
Construct a Cpp code solution to the problem outlined:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long md = 1000000007;
struct node {
long long white, black;
} tree[100010];
int type[100010];
int n, fa[100010];
void output() {
printf("\n-----------------------------\n");
for (int i = 1; i <= n; ++i) {
printf("tree[%d]: white=%lld, black=%lld\n", i, tree[i].white,
tree[i].black);
}
printf("\n-----------------------------\n");
return;
}
void run() {
for (int i = n; i >= 2; --i) {
int t = fa[i];
long long bl = tree[i].black * tree[t].white % md;
bl =
(bl + ((tree[i].black + tree[i].white) % md) * tree[t].black % md) % md;
long long wh =
(((tree[i].black + tree[i].white) % md) * tree[t].white) % md;
tree[t].black = bl;
tree[t].white = wh;
}
cout << tree[1].black;
return;
}
void init() {
cin >> n;
memset(type, 0, sizeof(type));
int top = 0, a, b;
for (int i = 2; i <= n; ++i) {
scanf("%d", &fa[i]);
++fa[i];
}
for (int i = 1; i <= n; ++i) {
scanf("%d", &type[i]);
if (type[i]) {
tree[i].white = 0;
tree[i].black = 1;
} else {
tree[i].white = 1;
tree[i].black = 0;
}
}
run();
return;
}
int main() {
init();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<int> e[100000];
long long f[100000][2];
int val[100000];
void dfs(int v) {
f[v][val[v]] = 1;
for (auto i : e[v]) {
dfs(i);
f[v][1] = (f[v][0] * f[i][1] + f[v][1] * (f[i][0] + f[i][1])) % 1000000007;
f[v][0] = (f[v][0] * f[i][1] + f[v][0] * f[i][0]) % 1000000007;
}
}
int main() {
int n;
cin >> n;
for (int i = 1; i < n; ++i) {
int c;
cin >> c;
e[c].push_back(i);
}
for (int i = 0; i < n; ++i) cin >> val[i];
dfs(0);
cout << f[0][1] << endl;
return 0;
}
|
### Prompt
Construct a cpp code solution to the problem outlined:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> e[100000];
long long f[100000][2];
int val[100000];
void dfs(int v) {
f[v][val[v]] = 1;
for (auto i : e[v]) {
dfs(i);
f[v][1] = (f[v][0] * f[i][1] + f[v][1] * (f[i][0] + f[i][1])) % 1000000007;
f[v][0] = (f[v][0] * f[i][1] + f[v][0] * f[i][0]) % 1000000007;
}
}
int main() {
int n;
cin >> n;
for (int i = 1; i < n; ++i) {
int c;
cin >> c;
e[c].push_back(i);
}
for (int i = 0; i < n; ++i) cin >> val[i];
dfs(0);
cout << f[0][1] << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n, son[1000005], cnt = 0;
long long f[1000005][2];
int read() {
int x = 0, f = 1;
char h = getchar();
for (; !isdigit(h); h = getchar())
if (h == '-') f = -1;
for (; isdigit(h); h = getchar()) x = (x << 3) + (x << 1) + h - 48;
return x * f;
}
int main() {
n = read();
memset(f, 0, sizeof(f));
for (int i = 1; i < n; i++) son[i] = read();
for (int i = 0; i < n; i++) f[i][read()] = 1;
for (int i = n - 1; i; i--)
(f[son[i]][1] = f[son[i]][0] * f[i][1] % 1000000007 +
f[son[i]][1] * (f[i][0] + f[i][1]) % 1000000007) %=
1000000007,
(f[son[i]][0] = f[son[i]][0] * (f[i][0] + f[i][1])) %= 1000000007;
cout << f[0][1];
return 0;
}
|
### Prompt
Please create a solution in CPP to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, son[1000005], cnt = 0;
long long f[1000005][2];
int read() {
int x = 0, f = 1;
char h = getchar();
for (; !isdigit(h); h = getchar())
if (h == '-') f = -1;
for (; isdigit(h); h = getchar()) x = (x << 3) + (x << 1) + h - 48;
return x * f;
}
int main() {
n = read();
memset(f, 0, sizeof(f));
for (int i = 1; i < n; i++) son[i] = read();
for (int i = 0; i < n; i++) f[i][read()] = 1;
for (int i = n - 1; i; i--)
(f[son[i]][1] = f[son[i]][0] * f[i][1] % 1000000007 +
f[son[i]][1] * (f[i][0] + f[i][1]) % 1000000007) %=
1000000007,
(f[son[i]][0] = f[son[i]][0] * (f[i][0] + f[i][1])) %= 1000000007;
cout << f[0][1];
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const double inf = 0x3f3f3f3f3f3f;
const int maxn = 1e5 + 105;
const int mod = 1e9 + 7;
int a[maxn];
std::vector<int> vv[maxn];
long long dp[maxn][2];
void dfs(int x) {
dp[x][a[x]] = 1;
dp[x][a[x] ^ 1] = 0;
for (int i = 0; i < vv[x].size(); i++) {
int v = vv[x][i];
dfs(v);
dp[x][1] =
dp[x][1] * (dp[v][0] + dp[v][1]) % mod + dp[x][0] * dp[v][1] % mod;
dp[x][1] %= mod;
dp[x][0] = dp[x][0] * (dp[v][0] + dp[v][1]) % mod;
}
}
int main() {
int n;
scanf("%d", &n);
for (int i = 2; i <= n; i++) {
int p;
scanf("%d", &p);
p++;
vv[p].push_back(i);
}
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
dfs(1);
printf("%lld\n", dp[1][1] % mod);
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const double inf = 0x3f3f3f3f3f3f;
const int maxn = 1e5 + 105;
const int mod = 1e9 + 7;
int a[maxn];
std::vector<int> vv[maxn];
long long dp[maxn][2];
void dfs(int x) {
dp[x][a[x]] = 1;
dp[x][a[x] ^ 1] = 0;
for (int i = 0; i < vv[x].size(); i++) {
int v = vv[x][i];
dfs(v);
dp[x][1] =
dp[x][1] * (dp[v][0] + dp[v][1]) % mod + dp[x][0] * dp[v][1] % mod;
dp[x][1] %= mod;
dp[x][0] = dp[x][0] * (dp[v][0] + dp[v][1]) % mod;
}
}
int main() {
int n;
scanf("%d", &n);
for (int i = 2; i <= n; i++) {
int p;
scanf("%d", &p);
p++;
vv[p].push_back(i);
}
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
dfs(1);
printf("%lld\n", dp[1][1] % mod);
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<int> q[100010];
long long dp[100010][2];
int N, color[100010], x;
void dfs(int x, int y) {
dp[x][color[x]] = 1;
for (int i = 0; i < q[x].size(); i++) {
int temp = q[x][i];
if (temp == y) continue;
dfs(temp, x);
dp[x][1] = (dp[x][1] * dp[temp][0] % 1000000007 +
dp[x][1] * dp[temp][1] % 1000000007 +
dp[x][0] * dp[temp][1] % 1000000007) %
1000000007;
dp[x][0] = (dp[x][0] * dp[temp][0] % 1000000007 +
dp[x][0] * dp[temp][1] % 1000000007) %
1000000007;
}
}
int main() {
scanf("%d", &N);
for (int i = 1; i < N; i++) {
scanf("%d", &x);
q[i].push_back(x);
q[x].push_back(i);
}
for (int i = 0; i < N; i++) scanf("%d", &color[i]);
dfs(0, 0);
printf("%I64d\n", dp[0][1]);
return 0;
}
|
### Prompt
Please create a solution in CPP to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> q[100010];
long long dp[100010][2];
int N, color[100010], x;
void dfs(int x, int y) {
dp[x][color[x]] = 1;
for (int i = 0; i < q[x].size(); i++) {
int temp = q[x][i];
if (temp == y) continue;
dfs(temp, x);
dp[x][1] = (dp[x][1] * dp[temp][0] % 1000000007 +
dp[x][1] * dp[temp][1] % 1000000007 +
dp[x][0] * dp[temp][1] % 1000000007) %
1000000007;
dp[x][0] = (dp[x][0] * dp[temp][0] % 1000000007 +
dp[x][0] * dp[temp][1] % 1000000007) %
1000000007;
}
}
int main() {
scanf("%d", &N);
for (int i = 1; i < N; i++) {
scanf("%d", &x);
q[i].push_back(x);
q[x].push_back(i);
}
for (int i = 0; i < N; i++) scanf("%d", &color[i]);
dfs(0, 0);
printf("%I64d\n", dp[0][1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)1e5 + 10;
;
const int MOD = (int)1e9 + 7;
;
vector<long long> num[maxn];
long long dpb[maxn], dpw[maxn], n;
bool isb[maxn];
long long po(long long a, long long b) {
if (!b) return 1;
long long t = po(a, b / 2);
t = (t * t) % MOD;
if (b & 1) t = (t * a) % MOD;
return t;
}
void dfs(int v, int pr) {
for (int i = 0; i < num[v].size(); i++)
if (num[v][i] != pr) dfs(num[v][i], v);
if (isb[v]) {
dpw[v] = 0;
dpb[v] = 1;
for (int i = 0; i < num[v].size(); i++)
if (num[v][i] != pr) {
dpb[v] *= dpb[num[v][i]] + dpw[num[v][i]];
dpb[v] %= MOD;
}
} else {
dpw[v] = 1;
for (int i = 0; i < num[v].size(); i++)
if (num[v][i] != pr) {
dpw[v] *= dpb[num[v][i]] + dpw[num[v][i]];
dpw[v] %= MOD;
}
dpb[v] = 0;
for (int i = 0; i < num[v].size(); i++)
if (num[v][i] != pr) {
dpb[v] += (dpw[v] * po(dpb[num[v][i]] + dpw[num[v][i]], MOD - 2)) %
MOD * dpb[num[v][i]];
dpb[v] %= MOD;
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n - 1; i++) {
long long t;
cin >> t;
num[i + 1].push_back(t);
num[t].push_back(i + 1);
}
for (int i = 0; i < n; i++) cin >> isb[i];
dfs(0, -1);
cout << dpb[0];
}
|
### Prompt
Please formulate a cpp solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)1e5 + 10;
;
const int MOD = (int)1e9 + 7;
;
vector<long long> num[maxn];
long long dpb[maxn], dpw[maxn], n;
bool isb[maxn];
long long po(long long a, long long b) {
if (!b) return 1;
long long t = po(a, b / 2);
t = (t * t) % MOD;
if (b & 1) t = (t * a) % MOD;
return t;
}
void dfs(int v, int pr) {
for (int i = 0; i < num[v].size(); i++)
if (num[v][i] != pr) dfs(num[v][i], v);
if (isb[v]) {
dpw[v] = 0;
dpb[v] = 1;
for (int i = 0; i < num[v].size(); i++)
if (num[v][i] != pr) {
dpb[v] *= dpb[num[v][i]] + dpw[num[v][i]];
dpb[v] %= MOD;
}
} else {
dpw[v] = 1;
for (int i = 0; i < num[v].size(); i++)
if (num[v][i] != pr) {
dpw[v] *= dpb[num[v][i]] + dpw[num[v][i]];
dpw[v] %= MOD;
}
dpb[v] = 0;
for (int i = 0; i < num[v].size(); i++)
if (num[v][i] != pr) {
dpb[v] += (dpw[v] * po(dpb[num[v][i]] + dpw[num[v][i]], MOD - 2)) %
MOD * dpb[num[v][i]];
dpb[v] %= MOD;
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n - 1; i++) {
long long t;
cin >> t;
num[i + 1].push_back(t);
num[t].push_back(i + 1);
}
for (int i = 0; i < n; i++) cin >> isb[i];
dfs(0, -1);
cout << dpb[0];
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7, lim = 1e6 + 10;
inline long long pwr(long long a, long long n, long long m = mod) {
long long ans(1);
while (n) {
if (n & 1) ans = ans * a % m;
n >>= 1;
a = a * a % m;
}
return ans;
}
vector<int> t[lim];
int a[lim], x[lim], y[lim], temp;
inline void bfs(int v, int par) {
bool is_leaf = true;
for (int ch : t[v])
if (ch != par) bfs(ch, v), is_leaf = false;
if (is_leaf) {
x[v] = 1;
y[v] = a[v];
return;
}
temp = 1;
for (int ch : t[v])
if (ch != par) temp = (long long)temp * x[ch] % mod;
x[v] = y[v] = temp;
if (!a[v]) {
temp = 0;
for (int ch : t[v])
if (ch != par)
temp = (temp + (long long)pwr(x[ch], mod - 2) * y[ch]) % mod;
y[v] = (long long)y[v] * temp % mod;
x[v] = (x[v] + y[v]) % mod;
}
}
int main() {
int n;
cin >> n;
for (long long i = 0, _n = (n - 1); i < _n; i++)
cin >> temp, t[temp].emplace_back(i + 1), t[i + 1].emplace_back(temp);
for (long long i = 0, _n = (n); i < _n; i++) cin >> a[i];
bfs(0, 0);
cout << y[0];
return 0;
}
|
### Prompt
Please create a solution in CPP to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7, lim = 1e6 + 10;
inline long long pwr(long long a, long long n, long long m = mod) {
long long ans(1);
while (n) {
if (n & 1) ans = ans * a % m;
n >>= 1;
a = a * a % m;
}
return ans;
}
vector<int> t[lim];
int a[lim], x[lim], y[lim], temp;
inline void bfs(int v, int par) {
bool is_leaf = true;
for (int ch : t[v])
if (ch != par) bfs(ch, v), is_leaf = false;
if (is_leaf) {
x[v] = 1;
y[v] = a[v];
return;
}
temp = 1;
for (int ch : t[v])
if (ch != par) temp = (long long)temp * x[ch] % mod;
x[v] = y[v] = temp;
if (!a[v]) {
temp = 0;
for (int ch : t[v])
if (ch != par)
temp = (temp + (long long)pwr(x[ch], mod - 2) * y[ch]) % mod;
y[v] = (long long)y[v] * temp % mod;
x[v] = (x[v] + y[v]) % mod;
}
}
int main() {
int n;
cin >> n;
for (long long i = 0, _n = (n - 1); i < _n; i++)
cin >> temp, t[temp].emplace_back(i + 1), t[i + 1].emplace_back(temp);
for (long long i = 0, _n = (n); i < _n; i++) cin >> a[i];
bfs(0, 0);
cout << y[0];
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007, M = 1e5 + 7;
long long powe(long long x, long long y) {
x = x % mod;
long long ans = 1;
while (y > 0) {
if (y & 1) {
ans = (1ll * x * ans) % mod;
}
y >>= 1;
x = (1ll * x * x) % mod;
}
return ans;
}
vector<int> edg[M];
bool col[M], vis[M];
long long s0[M], s1[M];
void dfs(int i) {
vis[i] = true;
if (col[i])
s1[i] = 1;
else
s0[i] = 1;
for (auto x : edg[i]) {
if (vis[x]) continue;
dfs(x);
s1[i] = (((s1[i] * s0[x])) % mod + (s0[i] * s1[x]) % mod +
(s1[i] * s1[x]) % mod) %
mod;
s0[i] = ((s0[i] * s0[x]) % mod + (s0[i] * s1[x]) % mod) % mod;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int n, a;
cin >> n;
for (int i = 1; i < n; i++) {
cin >> a;
edg[i].push_back(a);
edg[a].push_back(i);
}
for (int i = 0; i < n; i++) cin >> col[i];
dfs(0);
cout << s1[0];
return 0;
}
|
### Prompt
Your task is to create a cpp solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007, M = 1e5 + 7;
long long powe(long long x, long long y) {
x = x % mod;
long long ans = 1;
while (y > 0) {
if (y & 1) {
ans = (1ll * x * ans) % mod;
}
y >>= 1;
x = (1ll * x * x) % mod;
}
return ans;
}
vector<int> edg[M];
bool col[M], vis[M];
long long s0[M], s1[M];
void dfs(int i) {
vis[i] = true;
if (col[i])
s1[i] = 1;
else
s0[i] = 1;
for (auto x : edg[i]) {
if (vis[x]) continue;
dfs(x);
s1[i] = (((s1[i] * s0[x])) % mod + (s0[i] * s1[x]) % mod +
(s1[i] * s1[x]) % mod) %
mod;
s0[i] = ((s0[i] * s0[x]) % mod + (s0[i] * s1[x]) % mod) % mod;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int n, a;
cin >> n;
for (int i = 1; i < n; i++) {
cin >> a;
edg[i].push_back(a);
edg[a].push_back(i);
}
for (int i = 0; i < n; i++) cin >> col[i];
dfs(0);
cout << s1[0];
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
int n;
vector<int> g[100000];
int b[100000];
vector<pair<int, int> > t[100000];
long long P(long long a, long long b) {
long long ret = 1;
while (b) {
if (b & 1) ret = (ret * a) % MOD;
a = (a * a) % MOD;
b >>= 1;
}
return ret;
}
long long D(long long a, long long b) { return (a * P(b, MOD - 2)) % MOD; }
pair<int, int> dfs(int v) {
if (b[v]) {
long long ret = 1;
for (int to : g[v]) {
pair<int, int> tmp = dfs(to);
ret = (ret * (tmp.first + tmp.second)) % MOD;
}
return make_pair(0, (int)ret);
} else {
long long ret = 1, ret2 = 0;
for (int to : g[v]) {
pair<int, int> tmp = dfs(to);
t[v].push_back(tmp);
ret = (ret * (tmp.first + tmp.second)) % MOD;
}
for (pair<int, int> z : t[v])
ret2 = (ret2 + D(ret, z.first + z.second) * z.second) % MOD;
return make_pair((int)ret, (int)ret2);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n;
for (int i = 1; i < n; ++i) {
int x;
cin >> x;
g[x].push_back(i);
}
for (int i = 0; i < (int)(n); i++) cin >> b[i];
cout << dfs(0).second << '\n';
return 0;
}
|
### Prompt
Develop a solution in Cpp to the problem described below:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
int n;
vector<int> g[100000];
int b[100000];
vector<pair<int, int> > t[100000];
long long P(long long a, long long b) {
long long ret = 1;
while (b) {
if (b & 1) ret = (ret * a) % MOD;
a = (a * a) % MOD;
b >>= 1;
}
return ret;
}
long long D(long long a, long long b) { return (a * P(b, MOD - 2)) % MOD; }
pair<int, int> dfs(int v) {
if (b[v]) {
long long ret = 1;
for (int to : g[v]) {
pair<int, int> tmp = dfs(to);
ret = (ret * (tmp.first + tmp.second)) % MOD;
}
return make_pair(0, (int)ret);
} else {
long long ret = 1, ret2 = 0;
for (int to : g[v]) {
pair<int, int> tmp = dfs(to);
t[v].push_back(tmp);
ret = (ret * (tmp.first + tmp.second)) % MOD;
}
for (pair<int, int> z : t[v])
ret2 = (ret2 + D(ret, z.first + z.second) * z.second) % MOD;
return make_pair((int)ret, (int)ret2);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n;
for (int i = 1; i < n; ++i) {
int x;
cin >> x;
g[x].push_back(i);
}
for (int i = 0; i < (int)(n); i++) cin >> b[i];
cout << dfs(0).second << '\n';
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, P = 1000000007;
long long n;
vector<long long> adj[N];
long long c[N], l[N], r[N];
int mul(long long a, long long b) { return (a * b) % P; }
int sum(long long a, long long b) { return (a + b) % P; }
pair<long long, long long> dfs(long long u, long long p) {
vector<long long> a, b;
for (auto x : adj[u]) {
if (x != p) {
auto k = dfs(x, u);
a.push_back(k.first);
b.push_back(k.second);
} else {
a.push_back(1);
b.push_back(1);
}
}
if (a.size() == 0) {
if (c[u] == 0) {
return {0, 1};
} else {
return {1, 1};
}
}
if (c[u] == 0) {
int k = adj[u].size();
l[0] = 1;
r[k - 1] = 1;
for (int i = 1; i < k; i++) l[i] = mul(l[i - 1], b[i - 1]);
for (int i = k - 2; i >= 0; i--) r[i] = mul(r[i + 1], b[i + 1]);
long long ans = 0;
for (int i = 0; i < k; i++)
if (adj[u][i] != p) {
long long res = mul(mul(l[i], r[i]), a[i]);
ans = sum(ans, res);
}
long long ans2 = sum(ans, mul(l[k - 1], b[k - 1]));
return {ans, ans2};
} else {
long long ans = 1;
for (auto x : b) ans = mul(ans, x);
return {ans, ans};
}
}
int main() {
cin >> n;
for (int i = 0; i < n - 1; i++) {
long long a;
cin >> a;
adj[a].push_back(i + 1);
adj[i + 1].push_back(a);
}
for (int i = 0; i < n; i++) cin >> c[i];
cout << dfs(0, 0).first;
}
|
### Prompt
Please formulate a CPP solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, P = 1000000007;
long long n;
vector<long long> adj[N];
long long c[N], l[N], r[N];
int mul(long long a, long long b) { return (a * b) % P; }
int sum(long long a, long long b) { return (a + b) % P; }
pair<long long, long long> dfs(long long u, long long p) {
vector<long long> a, b;
for (auto x : adj[u]) {
if (x != p) {
auto k = dfs(x, u);
a.push_back(k.first);
b.push_back(k.second);
} else {
a.push_back(1);
b.push_back(1);
}
}
if (a.size() == 0) {
if (c[u] == 0) {
return {0, 1};
} else {
return {1, 1};
}
}
if (c[u] == 0) {
int k = adj[u].size();
l[0] = 1;
r[k - 1] = 1;
for (int i = 1; i < k; i++) l[i] = mul(l[i - 1], b[i - 1]);
for (int i = k - 2; i >= 0; i--) r[i] = mul(r[i + 1], b[i + 1]);
long long ans = 0;
for (int i = 0; i < k; i++)
if (adj[u][i] != p) {
long long res = mul(mul(l[i], r[i]), a[i]);
ans = sum(ans, res);
}
long long ans2 = sum(ans, mul(l[k - 1], b[k - 1]));
return {ans, ans2};
} else {
long long ans = 1;
for (auto x : b) ans = mul(ans, x);
return {ans, ans};
}
}
int main() {
cin >> n;
for (int i = 0; i < n - 1; i++) {
long long a;
cin >> a;
adj[a].push_back(i + 1);
adj[i + 1].push_back(a);
}
for (int i = 0; i < n; i++) cin >> c[i];
cout << dfs(0, 0).first;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 5;
const long long linf = 1e18 + 5;
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
int n, x, dp[N][2], a[N];
vector<int> v[N];
int f(int x, bool w) {
int &r = dp[x][w];
if (r != -1) return r;
if (a[x]) {
r = 1;
for (__typeof((v[x]).begin()) it = (v[x]).begin(); it != (v[x]).end(); it++)
r = ((long long)r * f(*it, 0)) % mod;
return r;
}
if (v[x].empty()) return r = !w;
r = 0;
vector<int> lp, rp;
lp.resize(v[x].size() + 1);
rp.resize(v[x].size() + 1);
lp[0] = 1;
for (int i = 0; i <= v[x].size() - 1; i++)
lp[i + 1] = ((long long)lp[i] * f(v[x][i], 0)) % mod;
rp[v[x].size()] = 1;
for (int i = v[x].size() - 1; i >= 0; i--)
rp[i] = ((long long)rp[i + 1] * f(v[x][i], 0)) % mod;
for (int i = 0; i <= v[x].size() - 1; i++)
r = (r + (long long)lp[i] * rp[i + 1] % mod * f(v[x][i], 1) % mod) % mod;
if (!w) {
int cur = 1;
for (__typeof((v[x]).begin()) it = (v[x]).begin(); it != (v[x]).end(); it++)
cur = ((long long)cur * f(*it, 0)) % mod;
r = (r + cur) % mod;
}
return r;
}
int main() {
ios ::sync_with_stdio(0);
memset(dp, -1, sizeof(dp));
cin >> n;
for (int i = 2; i <= n; i++) {
cin >> x;
v[x + 1].push_back(i);
}
for (int i = 1; i <= n; i++) cin >> a[i];
cout << f(1, 1) << '\n';
return 0;
}
|
### Prompt
Please formulate a cpp solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 5;
const long long linf = 1e18 + 5;
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
int n, x, dp[N][2], a[N];
vector<int> v[N];
int f(int x, bool w) {
int &r = dp[x][w];
if (r != -1) return r;
if (a[x]) {
r = 1;
for (__typeof((v[x]).begin()) it = (v[x]).begin(); it != (v[x]).end(); it++)
r = ((long long)r * f(*it, 0)) % mod;
return r;
}
if (v[x].empty()) return r = !w;
r = 0;
vector<int> lp, rp;
lp.resize(v[x].size() + 1);
rp.resize(v[x].size() + 1);
lp[0] = 1;
for (int i = 0; i <= v[x].size() - 1; i++)
lp[i + 1] = ((long long)lp[i] * f(v[x][i], 0)) % mod;
rp[v[x].size()] = 1;
for (int i = v[x].size() - 1; i >= 0; i--)
rp[i] = ((long long)rp[i + 1] * f(v[x][i], 0)) % mod;
for (int i = 0; i <= v[x].size() - 1; i++)
r = (r + (long long)lp[i] * rp[i + 1] % mod * f(v[x][i], 1) % mod) % mod;
if (!w) {
int cur = 1;
for (__typeof((v[x]).begin()) it = (v[x]).begin(); it != (v[x]).end(); it++)
cur = ((long long)cur * f(*it, 0)) % mod;
r = (r + cur) % mod;
}
return r;
}
int main() {
ios ::sync_with_stdio(0);
memset(dp, -1, sizeof(dp));
cin >> n;
for (int i = 2; i <= n; i++) {
cin >> x;
v[x + 1].push_back(i);
}
for (int i = 1; i <= n; i++) cin >> a[i];
cout << f(1, 1) << '\n';
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 5, MOD = 1e9 + 7;
int n, c[MAX], f[MAX], g[MAX], pref[MAX], suff[MAX];
vector<int> tmp, gt[MAX];
void dfs(int x, int p) {
if (x > 0 && gt[x].size() == 1) {
f[x] = !c[x];
g[x] = c[x];
return;
}
if (c[x] == 0) {
f[x] = 1;
g[x] = 0;
for (int i = 0; i < gt[x].size(); i++) {
int y = gt[x][i];
if (y == p) continue;
dfs(y, x);
f[x] = (f[x] * 1LL * (f[y] + g[y])) % MOD;
}
} else {
f[x] = 0;
g[x] = 1;
for (int i = 0; i < gt[x].size(); i++) {
int y = gt[x][i];
if (y == p) continue;
dfs(y, x);
g[x] = (g[x] * 1LL * (f[y] + g[y])) % MOD;
}
for (int i = 0; i < gt[x].size(); i++) {
int y = gt[x][i];
if (y == p) continue;
tmp.push_back(y);
}
for (int i = 0; i < tmp.size(); i++) {
int y = tmp[i];
pref[i] = (f[y] + g[y]) % MOD;
if (i > 0) pref[i] = (pref[i] * 1LL * pref[i - 1]) % MOD;
}
for (int i = tmp.size() - 1; i >= 0; i--) {
int y = tmp[i];
suff[i] = (f[y] + g[y]) % MOD;
if (i < tmp.size() - 1) suff[i] = (suff[i] * 1LL * suff[i + 1]) % MOD;
int ans = f[y];
if (i > 0) ans = (ans * 1LL * pref[i - 1]) % MOD;
if (i < tmp.size() - 1) ans = (ans * 1LL * suff[i + 1]) % MOD;
f[x] = (f[x] + ans) % MOD;
}
for (int i = 0; i < tmp.size(); i++) {
int y = tmp[i];
pref[y] = suff[y] = 0;
}
tmp.clear();
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int x;
scanf("%d", &x);
gt[x].push_back(i);
gt[i].push_back(x);
}
for (int i = 0; i < n; i++) {
scanf("%d", &c[i]);
c[i] = !c[i];
}
dfs(0, -1);
printf("%d\n", f[0]);
return 0;
}
|
### Prompt
Generate a Cpp solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 5, MOD = 1e9 + 7;
int n, c[MAX], f[MAX], g[MAX], pref[MAX], suff[MAX];
vector<int> tmp, gt[MAX];
void dfs(int x, int p) {
if (x > 0 && gt[x].size() == 1) {
f[x] = !c[x];
g[x] = c[x];
return;
}
if (c[x] == 0) {
f[x] = 1;
g[x] = 0;
for (int i = 0; i < gt[x].size(); i++) {
int y = gt[x][i];
if (y == p) continue;
dfs(y, x);
f[x] = (f[x] * 1LL * (f[y] + g[y])) % MOD;
}
} else {
f[x] = 0;
g[x] = 1;
for (int i = 0; i < gt[x].size(); i++) {
int y = gt[x][i];
if (y == p) continue;
dfs(y, x);
g[x] = (g[x] * 1LL * (f[y] + g[y])) % MOD;
}
for (int i = 0; i < gt[x].size(); i++) {
int y = gt[x][i];
if (y == p) continue;
tmp.push_back(y);
}
for (int i = 0; i < tmp.size(); i++) {
int y = tmp[i];
pref[i] = (f[y] + g[y]) % MOD;
if (i > 0) pref[i] = (pref[i] * 1LL * pref[i - 1]) % MOD;
}
for (int i = tmp.size() - 1; i >= 0; i--) {
int y = tmp[i];
suff[i] = (f[y] + g[y]) % MOD;
if (i < tmp.size() - 1) suff[i] = (suff[i] * 1LL * suff[i + 1]) % MOD;
int ans = f[y];
if (i > 0) ans = (ans * 1LL * pref[i - 1]) % MOD;
if (i < tmp.size() - 1) ans = (ans * 1LL * suff[i + 1]) % MOD;
f[x] = (f[x] + ans) % MOD;
}
for (int i = 0; i < tmp.size(); i++) {
int y = tmp[i];
pref[y] = suff[y] = 0;
}
tmp.clear();
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int x;
scanf("%d", &x);
gt[x].push_back(i);
gt[i].push_back(x);
}
for (int i = 0; i < n; i++) {
scanf("%d", &c[i]);
c[i] = !c[i];
}
dfs(0, -1);
printf("%d\n", f[0]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int countBits(long long mask) {
int res = 0;
while (mask) mask &= (mask - 1), ++res;
return res;
}
string toString(long long n) {
stringstream ss;
ss << n;
return ss.str();
}
long long toNumber(string s) {
stringstream ss;
long long n;
ss << s;
ss >> n;
return n;
}
long long dp[100010][2];
bool black[100010];
vector<vector<int> > adjList;
long long mod_inverse(long long n, long long p) {
if (!p) return 1;
if (p == 1) return n;
long long res = mod_inverse(n, p / 2);
res = (res * res) % 1000000007;
if (p & 1) res = (res * n) % 1000000007;
return res;
}
long long rec(int node, bool has_black_ancestor, int parent) {
if (adjList[node].size() == 1 && parent != -1)
return black[node] || has_black_ancestor;
long long &res = dp[node][has_black_ancestor];
if (res != -1) return res;
res = 0;
long long all_ok = 1;
for (int i = (int)(0), _m = (int)(adjList[node].size()); i < _m; ++i) {
int child = adjList[node][i];
if (child == parent) continue;
all_ok = (all_ok * rec(child, 1, node)) % 1000000007;
}
if (black[node] || has_black_ancestor) res = all_ok;
if (!black[node]) {
for (int i = (int)(0), _m = (int)(adjList[node].size()); i < _m; ++i) {
int child = adjList[node][i];
if (child == parent) continue;
long long all_but_this =
(all_ok * mod_inverse(rec(child, 1, node), 1000000007 - 2)) %
1000000007;
res += (all_but_this * rec(child, 0, node)) % 1000000007;
res %= 1000000007;
}
}
return res;
}
int main() {
int n, p;
while (cin >> n) {
adjList.clear();
adjList.resize(n);
for (int i = (int)(1), _m = (int)(n); i < _m; ++i) {
cin >> p;
adjList[p].push_back(i);
adjList[i].push_back(p);
}
for (int i = (int)(0), _m = (int)(n); i < _m; ++i) cin >> black[i];
memset(dp, -1, sizeof dp);
cout << rec(0, 0, -1) << endl;
}
}
|
### Prompt
Please formulate a cpp solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int countBits(long long mask) {
int res = 0;
while (mask) mask &= (mask - 1), ++res;
return res;
}
string toString(long long n) {
stringstream ss;
ss << n;
return ss.str();
}
long long toNumber(string s) {
stringstream ss;
long long n;
ss << s;
ss >> n;
return n;
}
long long dp[100010][2];
bool black[100010];
vector<vector<int> > adjList;
long long mod_inverse(long long n, long long p) {
if (!p) return 1;
if (p == 1) return n;
long long res = mod_inverse(n, p / 2);
res = (res * res) % 1000000007;
if (p & 1) res = (res * n) % 1000000007;
return res;
}
long long rec(int node, bool has_black_ancestor, int parent) {
if (adjList[node].size() == 1 && parent != -1)
return black[node] || has_black_ancestor;
long long &res = dp[node][has_black_ancestor];
if (res != -1) return res;
res = 0;
long long all_ok = 1;
for (int i = (int)(0), _m = (int)(adjList[node].size()); i < _m; ++i) {
int child = adjList[node][i];
if (child == parent) continue;
all_ok = (all_ok * rec(child, 1, node)) % 1000000007;
}
if (black[node] || has_black_ancestor) res = all_ok;
if (!black[node]) {
for (int i = (int)(0), _m = (int)(adjList[node].size()); i < _m; ++i) {
int child = adjList[node][i];
if (child == parent) continue;
long long all_but_this =
(all_ok * mod_inverse(rec(child, 1, node), 1000000007 - 2)) %
1000000007;
res += (all_but_this * rec(child, 0, node)) % 1000000007;
res %= 1000000007;
}
}
return res;
}
int main() {
int n, p;
while (cin >> n) {
adjList.clear();
adjList.resize(n);
for (int i = (int)(1), _m = (int)(n); i < _m; ++i) {
cin >> p;
adjList[p].push_back(i);
adjList[i].push_back(p);
}
for (int i = (int)(0), _m = (int)(n); i < _m; ++i) cin >> black[i];
memset(dp, -1, sizeof dp);
cout << rec(0, 0, -1) << endl;
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
class disjointSet {
int *parent;
int *rank;
int cs, ms;
public:
disjointSet(int ms = int(1e5 + 5)) {
this->ms = ms;
cs = 0;
parent = new int[ms];
rank = new int[ms];
}
void MakeSet(int x) {
if (cs == ms) return;
parent[x] = x;
rank[x] = 0;
cs++;
}
int Find(int x) {
if (parent[x] != x) parent[x] = Find(parent[x]);
return parent[x];
}
void Union(int x, int y) {
int px = Find(x);
int py = Find(y);
if (px == py) return;
if (rank[px] > rank[py])
parent[py] = px;
else {
parent[px] = py;
if (rank[px] == rank[py]) rank[py]++;
}
}
};
int n;
vector<vector<int> > g;
bitset<int(int(1e5 + 5))> color;
long long dp[int(1e5 + 5)][2];
void dfs(int u) {
dp[u][0] = 1;
dp[u][1] = 0;
for (int v : g[u]) {
dfs(v);
dp[u][1] = ((dp[u][1] * dp[v][0]) % int(1e9 + 7) +
(dp[u][0] * dp[v][1]) % int(1e9 + 7)) %
int(1e9 + 7);
dp[u][0] = (dp[u][0] * dp[v][0]) % int(1e9 + 7);
}
if (color[u])
dp[u][1] = dp[u][0];
else
dp[u][0] = (dp[u][0] + dp[u][1]) % int(1e9 + 7);
}
void solve() {
dfs(0);
cout << dp[0][1] << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
int u;
g.assign(n, vector<int>());
for (int v = 1; v < n; v++) {
cin >> u;
g[u].push_back(v);
}
for (int u = 0; u < n; u++) {
int c;
cin >> c;
color[u] = c;
}
solve();
}
|
### Prompt
Your challenge is to write a cpp solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
class disjointSet {
int *parent;
int *rank;
int cs, ms;
public:
disjointSet(int ms = int(1e5 + 5)) {
this->ms = ms;
cs = 0;
parent = new int[ms];
rank = new int[ms];
}
void MakeSet(int x) {
if (cs == ms) return;
parent[x] = x;
rank[x] = 0;
cs++;
}
int Find(int x) {
if (parent[x] != x) parent[x] = Find(parent[x]);
return parent[x];
}
void Union(int x, int y) {
int px = Find(x);
int py = Find(y);
if (px == py) return;
if (rank[px] > rank[py])
parent[py] = px;
else {
parent[px] = py;
if (rank[px] == rank[py]) rank[py]++;
}
}
};
int n;
vector<vector<int> > g;
bitset<int(int(1e5 + 5))> color;
long long dp[int(1e5 + 5)][2];
void dfs(int u) {
dp[u][0] = 1;
dp[u][1] = 0;
for (int v : g[u]) {
dfs(v);
dp[u][1] = ((dp[u][1] * dp[v][0]) % int(1e9 + 7) +
(dp[u][0] * dp[v][1]) % int(1e9 + 7)) %
int(1e9 + 7);
dp[u][0] = (dp[u][0] * dp[v][0]) % int(1e9 + 7);
}
if (color[u])
dp[u][1] = dp[u][0];
else
dp[u][0] = (dp[u][0] + dp[u][1]) % int(1e9 + 7);
}
void solve() {
dfs(0);
cout << dp[0][1] << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
int u;
g.assign(n, vector<int>());
for (int v = 1; v < n; v++) {
cin >> u;
g[u].push_back(v);
}
for (int u = 0; u < n; u++) {
int c;
cin >> c;
color[u] = c;
}
solve();
}
```
|
#include <bits/stdc++.h>
const int MOD = 1e9 + 7, MAXN = 1e5 + 50;
using namespace std;
int p[MAXN], x[MAXN];
vector<int> adj[MAXN];
long long dp[MAXN][2];
void dfs(int u) {
dp[u][0] = 1;
dp[u][1] = 0;
for (int v : adj[u]) {
dfs(v);
dp[u][1] = (dp[u][1] * dp[v][0]) % MOD;
dp[u][1] = (dp[u][1] + dp[u][0] * dp[v][1]) % MOD;
dp[u][0] = (dp[u][0] * dp[v][0]) % MOD;
}
if (x[u])
dp[u][1] = dp[u][0];
else
dp[u][0] += dp[u][1];
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
scanf("%d", p + i);
adj[p[i]].push_back(i);
}
p[0] = -1;
for (int i = 0; i < n; i++) scanf("%d", x + i);
dfs(0);
printf("%I64d\n", dp[0][1] % MOD);
return 0;
}
|
### Prompt
Construct a cpp code solution to the problem outlined:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
const int MOD = 1e9 + 7, MAXN = 1e5 + 50;
using namespace std;
int p[MAXN], x[MAXN];
vector<int> adj[MAXN];
long long dp[MAXN][2];
void dfs(int u) {
dp[u][0] = 1;
dp[u][1] = 0;
for (int v : adj[u]) {
dfs(v);
dp[u][1] = (dp[u][1] * dp[v][0]) % MOD;
dp[u][1] = (dp[u][1] + dp[u][0] * dp[v][1]) % MOD;
dp[u][0] = (dp[u][0] * dp[v][0]) % MOD;
}
if (x[u])
dp[u][1] = dp[u][0];
else
dp[u][0] += dp[u][1];
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
scanf("%d", p + i);
adj[p[i]].push_back(i);
}
p[0] = -1;
for (int i = 0; i < n; i++) scanf("%d", x + i);
dfs(0);
printf("%I64d\n", dp[0][1] % MOD);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n, kq = 0;
vector<int> a[100100];
int x[100100], dd[100100];
long long g[100100], first[100100];
int d[100100][3];
void nhap() {
cin >> n;
int c;
for (int i = 0; i <= n - 2; i++) {
scanf("%d", &c);
a[c].push_back(i + 1);
a[i + 1].push_back(c);
}
for (int i = 0; i <= n - 1; i++) scanf("%d", &x[i]);
}
void chuanbi() {
memset(g, 0, sizeof(g));
memset(first, 0, sizeof(first));
memset(dd, 0, sizeof(dd));
}
void dfs(int v) {
dd[v] = 1;
if (!x[v])
first[v] = 1;
else
g[v] = 1;
for (int i = 0; i < a[v].size(); i++) {
int u = a[v][i];
if (!dd[u]) {
dfs(u);
g[v] = (g[v] * (first[u] + g[u]) % 1000000007 +
first[v] * g[u] % 1000000007) %
1000000007;
first[v] = first[v] * (first[u] + g[u]) % 1000000007;
}
}
}
void xuli() {
dfs(1);
cout << g[1];
}
void ghikq() {}
int main() {
nhap();
chuanbi();
xuli();
ghikq();
}
|
### Prompt
Your task is to create a Cpp solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, kq = 0;
vector<int> a[100100];
int x[100100], dd[100100];
long long g[100100], first[100100];
int d[100100][3];
void nhap() {
cin >> n;
int c;
for (int i = 0; i <= n - 2; i++) {
scanf("%d", &c);
a[c].push_back(i + 1);
a[i + 1].push_back(c);
}
for (int i = 0; i <= n - 1; i++) scanf("%d", &x[i]);
}
void chuanbi() {
memset(g, 0, sizeof(g));
memset(first, 0, sizeof(first));
memset(dd, 0, sizeof(dd));
}
void dfs(int v) {
dd[v] = 1;
if (!x[v])
first[v] = 1;
else
g[v] = 1;
for (int i = 0; i < a[v].size(); i++) {
int u = a[v][i];
if (!dd[u]) {
dfs(u);
g[v] = (g[v] * (first[u] + g[u]) % 1000000007 +
first[v] * g[u] % 1000000007) %
1000000007;
first[v] = first[v] * (first[u] + g[u]) % 1000000007;
}
}
}
void xuli() {
dfs(1);
cout << g[1];
}
void ghikq() {}
int main() {
nhap();
chuanbi();
xuli();
ghikq();
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<int> g[100005];
long long n, mod = 1e9 + 7, dp[100005], a[100005], b[100005];
void dfs(int x) {
dp[x] = b[x];
a[x] = 1 - dp[x];
for (int i = 0; i < g[x].size(); i++) {
int v = g[x][i];
dfs(v);
dp[x] =
((dp[x] * a[v]) % mod + (dp[v] * a[x]) % mod + (dp[x] * dp[v]) % mod) %
mod;
a[x] = ((a[x] * dp[v]) % mod + (a[x] * a[v]) % mod) % mod;
}
return;
}
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i < n; i++) {
int x;
cin >> x;
g[x].push_back(i);
}
for (int i = 0; i < n; i++) cin >> b[i];
dfs(0);
cout << dp[0];
return 0;
}
|
### Prompt
Please create a solution in Cpp to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> g[100005];
long long n, mod = 1e9 + 7, dp[100005], a[100005], b[100005];
void dfs(int x) {
dp[x] = b[x];
a[x] = 1 - dp[x];
for (int i = 0; i < g[x].size(); i++) {
int v = g[x][i];
dfs(v);
dp[x] =
((dp[x] * a[v]) % mod + (dp[v] * a[x]) % mod + (dp[x] * dp[v]) % mod) %
mod;
a[x] = ((a[x] * dp[v]) % mod + (a[x] * a[v]) % mod) % mod;
}
return;
}
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i < n; i++) {
int x;
cin >> x;
g[x].push_back(i);
}
for (int i = 0; i < n; i++) cin >> b[i];
dfs(0);
cout << dp[0];
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int Max = 100005;
const long long mod = 1e9 + 7;
vector<int> edge[Max];
int is[Max];
long long dp[Max][2];
long long inv(long long x) {
long long res = 1;
for (int k = mod - 2; k; k >>= 1) {
if (k & 1) res = res * x % mod;
x = x * x % mod;
}
return res;
}
void dfs(int u, int f) {
int v, m = edge[u].size();
long long tot = 1;
long long tmp;
for (int i = 0; i < m; i++) {
v = edge[u][i];
if (v == f) continue;
dfs(v, u);
tot = tot * (dp[v][0] + dp[v][1]) % mod;
}
if (is[u])
dp[u][0] = 0;
else
dp[u][0] = tot;
if (is[u])
dp[u][1] = tot;
else {
dp[u][1] = 0;
for (int i = 0; i < m; i++) {
v = edge[u][i];
if (v == f) continue;
tmp = (tot * dp[v][1] % mod) * inv(dp[v][0] + dp[v][1]) % mod;
dp[u][1] = (dp[u][1] + tmp) % mod;
}
}
}
int main() {
int n, p;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
scanf("%d", &p);
edge[i].push_back(p);
edge[p].push_back(i);
}
for (int i = 0; i < n; i++) {
scanf("%d", &is[i]);
}
dfs(0, -1);
cout << dp[0][1] << endl;
return 0;
}
|
### Prompt
In cpp, your task is to solve the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int Max = 100005;
const long long mod = 1e9 + 7;
vector<int> edge[Max];
int is[Max];
long long dp[Max][2];
long long inv(long long x) {
long long res = 1;
for (int k = mod - 2; k; k >>= 1) {
if (k & 1) res = res * x % mod;
x = x * x % mod;
}
return res;
}
void dfs(int u, int f) {
int v, m = edge[u].size();
long long tot = 1;
long long tmp;
for (int i = 0; i < m; i++) {
v = edge[u][i];
if (v == f) continue;
dfs(v, u);
tot = tot * (dp[v][0] + dp[v][1]) % mod;
}
if (is[u])
dp[u][0] = 0;
else
dp[u][0] = tot;
if (is[u])
dp[u][1] = tot;
else {
dp[u][1] = 0;
for (int i = 0; i < m; i++) {
v = edge[u][i];
if (v == f) continue;
tmp = (tot * dp[v][1] % mod) * inv(dp[v][0] + dp[v][1]) % mod;
dp[u][1] = (dp[u][1] + tmp) % mod;
}
}
}
int main() {
int n, p;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
scanf("%d", &p);
edge[i].push_back(p);
edge[p].push_back(i);
}
for (int i = 0; i < n; i++) {
scanf("%d", &is[i]);
}
dfs(0, -1);
cout << dp[0][1] << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
queue<int> tree[100000];
int colour[100000];
long long int DP[2][100000];
void generateAnswer(int root) {
if (tree[root].size() == 0) {
if (colour[root] == 1) {
DP[1][root] = DP[0][root];
}
return;
}
while (!tree[root].empty()) {
int curr = tree[root].front();
generateAnswer(curr);
tree[root].pop();
DP[1][root] = (DP[1][root] * DP[0][curr]) % 1000000007;
DP[1][root] += (DP[0][root] * DP[1][curr]) % 1000000007;
DP[0][root] = (DP[0][root] * DP[0][curr]) % 1000000007;
}
if (colour[root] == 1) {
DP[1][root] = DP[0][root];
} else {
DP[0][root] = (DP[0][root] + DP[1][root]) % 1000000007;
}
return;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < 100000; i++) {
colour[i] = -1;
DP[0][i] = 1;
DP[1][i] = 0;
}
for (int i = 0; i < n - 1; i++) {
int from;
cin >> from;
tree[from].push(i + 1);
}
for (int i = 0; i < n; i++) {
scanf("%d", &colour[i]);
}
generateAnswer(0);
printf("%lld", DP[1][0] % 1000000007);
return 0;
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
queue<int> tree[100000];
int colour[100000];
long long int DP[2][100000];
void generateAnswer(int root) {
if (tree[root].size() == 0) {
if (colour[root] == 1) {
DP[1][root] = DP[0][root];
}
return;
}
while (!tree[root].empty()) {
int curr = tree[root].front();
generateAnswer(curr);
tree[root].pop();
DP[1][root] = (DP[1][root] * DP[0][curr]) % 1000000007;
DP[1][root] += (DP[0][root] * DP[1][curr]) % 1000000007;
DP[0][root] = (DP[0][root] * DP[0][curr]) % 1000000007;
}
if (colour[root] == 1) {
DP[1][root] = DP[0][root];
} else {
DP[0][root] = (DP[0][root] + DP[1][root]) % 1000000007;
}
return;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < 100000; i++) {
colour[i] = -1;
DP[0][i] = 1;
DP[1][i] = 0;
}
for (int i = 0; i < n - 1; i++) {
int from;
cin >> from;
tree[from].push(i + 1);
}
for (int i = 0; i < n; i++) {
scanf("%d", &colour[i]);
}
generateAnswer(0);
printf("%lld", DP[1][0] % 1000000007);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
struct Mint {
long long v;
explicit operator long long() const { return v; }
Mint() { v = 0; }
Mint(long long _v) {
v = (-mod < _v && _v < mod) ? _v : _v % mod;
if (v < 0) v += mod;
}
friend bool operator==(const Mint& a, const Mint& b) { return a.v == b.v; }
friend bool operator!=(const Mint& a, const Mint& b) { return !(a == b); }
friend bool operator<(const Mint& a, const Mint& b) { return a.v < b.v; }
Mint& operator+=(const Mint& m) {
if ((v += m.v) >= mod) v -= mod;
return *this;
}
Mint& operator-=(const Mint& m) {
if ((v -= m.v) < 0) v += mod;
return *this;
}
Mint& operator*=(const Mint& m) {
v = v * m.v % mod;
return *this;
}
Mint& operator/=(const Mint& m) { return (*this) *= inv(m); }
friend Mint pow(Mint a, long long p) {
Mint ans = 1;
assert(p >= 0);
for (; p; p /= 2, a *= a)
if (p & 1) ans *= a;
return ans;
}
friend Mint inv(const Mint& a) {
assert(a.v != 0);
return pow(a, mod - 2);
}
Mint operator-() const { return Mint(-v); }
Mint& operator++() { return *this += 1; }
Mint& operator--() { return *this -= 1; }
Mint operator++(int) {
Mint temp;
temp.v = v++;
return temp;
}
Mint operator--(int) {
Mint temp;
temp.v = v--;
return temp;
}
friend Mint operator+(Mint a, const Mint& b) { return a += b; }
friend Mint operator-(Mint a, const Mint& b) { return a -= b; }
friend Mint operator*(Mint a, const Mint& b) { return a *= b; }
friend Mint operator/(Mint a, const Mint& b) { return a /= b; }
friend ostream& operator<<(ostream& os, const Mint& m) {
os << m.v;
return os;
}
friend istream& operator>>(istream& is, Mint& m) {
long long x;
is >> x;
m.v = x;
return is;
}
};
void solve() {
int n;
cin >> n;
vector<vector<int>> adj(n);
for (int i = 1; i < n; i++) {
int p;
cin >> p;
adj[i].push_back(p);
adj[p].push_back(i);
}
vector<int> col(n);
for (int i = 0; i < n; i++) cin >> col[i];
vector<array<Mint, 2>> dp(n);
function<void(int, int)> dfs = [&](int node, int par) {
dp[node][col[node]] = 1;
for (int nxt : adj[node]) {
if (nxt != par) {
dfs(nxt, node);
array<Mint, 2> ndp;
ndp[0] += dp[node][0] * dp[nxt][0];
ndp[1] += dp[node][1] * dp[nxt][0] + dp[node][0] * dp[nxt][1];
ndp[0] += dp[node][0] * dp[nxt][1];
ndp[1] += dp[node][1] * dp[nxt][1];
dp[node] = ndp;
}
}
};
dfs(0, 0);
cout << dp[0][1] << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
cout << fixed << setprecision(20);
;
int t = 1;
for (int i = 1; i <= t; i++) {
solve();
}
}
|
### Prompt
Construct a Cpp code solution to the problem outlined:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
struct Mint {
long long v;
explicit operator long long() const { return v; }
Mint() { v = 0; }
Mint(long long _v) {
v = (-mod < _v && _v < mod) ? _v : _v % mod;
if (v < 0) v += mod;
}
friend bool operator==(const Mint& a, const Mint& b) { return a.v == b.v; }
friend bool operator!=(const Mint& a, const Mint& b) { return !(a == b); }
friend bool operator<(const Mint& a, const Mint& b) { return a.v < b.v; }
Mint& operator+=(const Mint& m) {
if ((v += m.v) >= mod) v -= mod;
return *this;
}
Mint& operator-=(const Mint& m) {
if ((v -= m.v) < 0) v += mod;
return *this;
}
Mint& operator*=(const Mint& m) {
v = v * m.v % mod;
return *this;
}
Mint& operator/=(const Mint& m) { return (*this) *= inv(m); }
friend Mint pow(Mint a, long long p) {
Mint ans = 1;
assert(p >= 0);
for (; p; p /= 2, a *= a)
if (p & 1) ans *= a;
return ans;
}
friend Mint inv(const Mint& a) {
assert(a.v != 0);
return pow(a, mod - 2);
}
Mint operator-() const { return Mint(-v); }
Mint& operator++() { return *this += 1; }
Mint& operator--() { return *this -= 1; }
Mint operator++(int) {
Mint temp;
temp.v = v++;
return temp;
}
Mint operator--(int) {
Mint temp;
temp.v = v--;
return temp;
}
friend Mint operator+(Mint a, const Mint& b) { return a += b; }
friend Mint operator-(Mint a, const Mint& b) { return a -= b; }
friend Mint operator*(Mint a, const Mint& b) { return a *= b; }
friend Mint operator/(Mint a, const Mint& b) { return a /= b; }
friend ostream& operator<<(ostream& os, const Mint& m) {
os << m.v;
return os;
}
friend istream& operator>>(istream& is, Mint& m) {
long long x;
is >> x;
m.v = x;
return is;
}
};
void solve() {
int n;
cin >> n;
vector<vector<int>> adj(n);
for (int i = 1; i < n; i++) {
int p;
cin >> p;
adj[i].push_back(p);
adj[p].push_back(i);
}
vector<int> col(n);
for (int i = 0; i < n; i++) cin >> col[i];
vector<array<Mint, 2>> dp(n);
function<void(int, int)> dfs = [&](int node, int par) {
dp[node][col[node]] = 1;
for (int nxt : adj[node]) {
if (nxt != par) {
dfs(nxt, node);
array<Mint, 2> ndp;
ndp[0] += dp[node][0] * dp[nxt][0];
ndp[1] += dp[node][1] * dp[nxt][0] + dp[node][0] * dp[nxt][1];
ndp[0] += dp[node][0] * dp[nxt][1];
ndp[1] += dp[node][1] * dp[nxt][1];
dp[node] = ndp;
}
}
};
dfs(0, 0);
cout << dp[0][1] << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
cout << fixed << setprecision(20);
;
int t = 1;
for (int i = 1; i <= t; i++) {
solve();
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int N = 1e5;
vector<int> g[N];
int dp[N][2], col[N], l[N], r[N];
void dfs(int v) {
int sz = g[v].size();
for (int i = 0; i < sz; i++) dfs(g[v][i]);
if (col[v]) {
int add = 1;
for (int i = 0; i < sz; i++)
add = (add * 1ll * (dp[g[v][i]][0] + dp[g[v][i]][1])) % MOD;
dp[v][1] = (dp[v][1] + add) % MOD;
} else {
int add = 1;
for (int i = 0; i < sz; i++)
add = (add * 1ll * (dp[g[v][i]][0] + dp[g[v][i]][1])) % MOD;
dp[v][0] = (dp[v][0] + add) % MOD;
if (sz) {
l[0] = (dp[g[v][0]][0] + dp[g[v][0]][1]) % MOD;
r[sz - 1] = (dp[g[v][sz - 1]][0] + dp[g[v][sz - 1]][1]) % MOD;
}
for (int i = 1; i < sz; i++)
l[i] = (l[i - 1] * 1ll * (dp[g[v][i]][0] + dp[g[v][i]][1])) % MOD;
for (int i = sz - 2; i >= 0; i--)
r[i] = (r[i + 1] * 1ll * (dp[g[v][i]][0] + dp[g[v][i]][1])) % MOD;
for (int i = 0; i < sz; i++) {
add = 1;
add =
(add * 1ll * (i ? l[i - 1] : 1) * (i != sz - 1 ? r[i + 1] : 1)) % MOD;
add = (add * 1ll * dp[g[v][i]][1]) % MOD;
dp[v][1] = (dp[v][1] + add) % MOD;
}
}
}
int main() {
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int p;
cin >> p;
g[p].push_back(i);
}
for (int i = 0; i < n; i++) cin >> col[i];
dfs(0);
cout << dp[0][1] << endl;
return 0;
}
|
### Prompt
Please create a solution in cpp to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int N = 1e5;
vector<int> g[N];
int dp[N][2], col[N], l[N], r[N];
void dfs(int v) {
int sz = g[v].size();
for (int i = 0; i < sz; i++) dfs(g[v][i]);
if (col[v]) {
int add = 1;
for (int i = 0; i < sz; i++)
add = (add * 1ll * (dp[g[v][i]][0] + dp[g[v][i]][1])) % MOD;
dp[v][1] = (dp[v][1] + add) % MOD;
} else {
int add = 1;
for (int i = 0; i < sz; i++)
add = (add * 1ll * (dp[g[v][i]][0] + dp[g[v][i]][1])) % MOD;
dp[v][0] = (dp[v][0] + add) % MOD;
if (sz) {
l[0] = (dp[g[v][0]][0] + dp[g[v][0]][1]) % MOD;
r[sz - 1] = (dp[g[v][sz - 1]][0] + dp[g[v][sz - 1]][1]) % MOD;
}
for (int i = 1; i < sz; i++)
l[i] = (l[i - 1] * 1ll * (dp[g[v][i]][0] + dp[g[v][i]][1])) % MOD;
for (int i = sz - 2; i >= 0; i--)
r[i] = (r[i + 1] * 1ll * (dp[g[v][i]][0] + dp[g[v][i]][1])) % MOD;
for (int i = 0; i < sz; i++) {
add = 1;
add =
(add * 1ll * (i ? l[i - 1] : 1) * (i != sz - 1 ? r[i + 1] : 1)) % MOD;
add = (add * 1ll * dp[g[v][i]][1]) % MOD;
dp[v][1] = (dp[v][1] + add) % MOD;
}
}
}
int main() {
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int p;
cin >> p;
g[p].push_back(i);
}
for (int i = 0; i < n; i++) cin >> col[i];
dfs(0);
cout << dp[0][1] << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int mod_inv(int a) {
int pw = 1000000007 - 2;
int res = 1;
while (pw) {
if (pw & 1) res = 1ll * res * a % 1000000007;
a = 1ll * a * a % 1000000007;
pw >>= 1;
}
return res;
}
void solver(vector<int> *arr, int n, int id, int prev, int *districts,
long long **dp) {
if (districts[id] == 1) {
dp[id][0] = 0;
long long prod = 1;
for (auto &i : arr[id]) {
if (i != prev) {
solver(arr, n, i, id, districts, dp);
prod =
((prod % 1000000007) *
((dp[i][1] % 1000000007) + (dp[i][0] % 1000000007)) % 1000000007) %
1000000007;
}
}
dp[id][1] = prod;
return;
}
long long sum = 0;
long long prod = 1;
for (auto &i : arr[id]) {
if (i != prev) {
solver(arr, n, i, id, districts, dp);
prod =
((prod % 1000000007) *
((dp[i][0] % 1000000007) + (dp[i][1] % 1000000007)) % 1000000007) %
1000000007;
}
}
dp[id][0] = prod;
for (auto &i : arr[id]) {
if (i != prev) {
sum = (sum +
(1ll * dp[i][1] *
(1ll * dp[id][0] * mod_inv(dp[i][0] + dp[i][1]) % 1000000007) %
1000000007)) %
1000000007;
}
}
dp[id][1] = sum;
return;
}
int main() {
int n;
cin >> n;
vector<int> *arr = new vector<int>[n];
for (int i = 0; i < n - 1; i++) {
int p;
cin >> p;
arr[i + 1].push_back(p);
arr[p].push_back(i + 1);
}
int *districts = new int[n];
int count = 0;
for (int i = 0; i < n; i++) {
int b;
cin >> b;
count += b;
districts[i] = b;
}
if (count == 0) {
cout << 0 << endl;
return 0;
}
long long **dp = new long long *[n];
for (int i = 0; i < n; i++) {
dp[i] = new long long[2];
}
solver(arr, n, 0, -1, districts, dp);
cout << (dp[0][1] % 1000000007) << endl;
for (int i = 0; i < n; i++) {
delete[] dp[i];
}
delete[] dp;
delete[] districts;
delete[] arr;
return 0;
}
|
### Prompt
Your task is to create a Cpp solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int mod_inv(int a) {
int pw = 1000000007 - 2;
int res = 1;
while (pw) {
if (pw & 1) res = 1ll * res * a % 1000000007;
a = 1ll * a * a % 1000000007;
pw >>= 1;
}
return res;
}
void solver(vector<int> *arr, int n, int id, int prev, int *districts,
long long **dp) {
if (districts[id] == 1) {
dp[id][0] = 0;
long long prod = 1;
for (auto &i : arr[id]) {
if (i != prev) {
solver(arr, n, i, id, districts, dp);
prod =
((prod % 1000000007) *
((dp[i][1] % 1000000007) + (dp[i][0] % 1000000007)) % 1000000007) %
1000000007;
}
}
dp[id][1] = prod;
return;
}
long long sum = 0;
long long prod = 1;
for (auto &i : arr[id]) {
if (i != prev) {
solver(arr, n, i, id, districts, dp);
prod =
((prod % 1000000007) *
((dp[i][0] % 1000000007) + (dp[i][1] % 1000000007)) % 1000000007) %
1000000007;
}
}
dp[id][0] = prod;
for (auto &i : arr[id]) {
if (i != prev) {
sum = (sum +
(1ll * dp[i][1] *
(1ll * dp[id][0] * mod_inv(dp[i][0] + dp[i][1]) % 1000000007) %
1000000007)) %
1000000007;
}
}
dp[id][1] = sum;
return;
}
int main() {
int n;
cin >> n;
vector<int> *arr = new vector<int>[n];
for (int i = 0; i < n - 1; i++) {
int p;
cin >> p;
arr[i + 1].push_back(p);
arr[p].push_back(i + 1);
}
int *districts = new int[n];
int count = 0;
for (int i = 0; i < n; i++) {
int b;
cin >> b;
count += b;
districts[i] = b;
}
if (count == 0) {
cout << 0 << endl;
return 0;
}
long long **dp = new long long *[n];
for (int i = 0; i < n; i++) {
dp[i] = new long long[2];
}
solver(arr, n, 0, -1, districts, dp);
cout << (dp[0][1] % 1000000007) << endl;
for (int i = 0; i < n; i++) {
delete[] dp[i];
}
delete[] dp;
delete[] districts;
delete[] arr;
return 0;
}
```
|
#include <bits/stdc++.h>
const double EPS = 1e-24;
const long long int MOD = 1000000007ll;
const double PI = 3.14159265359;
int INF = 2147483645;
long long int INFINF = 9223372036854775807;
template <class T>
T Max2(T a, T b) {
return a < b ? b : a;
}
template <class T>
T Min2(T a, T b) {
return a < b ? a : b;
}
template <class T>
T Max3(T a, T b, T c) {
return Max2(Max2(a, b), c);
}
template <class T>
T Min3(T a, T b, T c) {
return Min2(Min2(a, b), c);
}
template <class T>
T Max4(T a, T b, T c, T d) {
return Max2(Max2(a, b), Max2(c, d));
}
template <class T>
T Min4(T a, T b, T c, T d) {
return Min2(Min2(a, b), Max2(c, d));
}
using namespace std;
long long int N;
vector<long long int> G[100010];
long long int dp[100010][2];
bool vis[100010];
long long int color[100010];
void dfs(long long int u) {
vis[u] = true;
if (color[u]) {
dp[u][0] = 0;
dp[u][1] = 1;
} else {
dp[u][0] = 1;
dp[u][1] = 0;
}
for (long long int v : G[u]) {
if (!vis[v]) {
dfs(v);
long long int temp[2];
temp[0] = dp[u][0];
temp[1] = dp[u][0];
temp[1] = (dp[u][1] * dp[v][1]) % MOD;
temp[1] = (temp[1] + dp[u][0] * dp[v][1]) % MOD;
temp[1] = (temp[1] + dp[u][1] * dp[v][0]) % MOD;
temp[0] = (dp[u][0] * dp[v][1]) % MOD;
temp[0] = (temp[0] + dp[u][0] * dp[v][0]) % MOD;
dp[u][0] = temp[0];
dp[u][1] = temp[1];
}
}
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> N;
for (int i = 1; i <= N - 1; i++) {
long long int x;
cin >> x;
G[x].push_back(i);
G[i].push_back(x);
}
for (int i = 0; i < N; i++) cin >> color[i];
dfs(0);
cout << dp[0][1] << "\n";
return 0;
}
|
### Prompt
Please provide a Cpp coded solution to the problem described below:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
const double EPS = 1e-24;
const long long int MOD = 1000000007ll;
const double PI = 3.14159265359;
int INF = 2147483645;
long long int INFINF = 9223372036854775807;
template <class T>
T Max2(T a, T b) {
return a < b ? b : a;
}
template <class T>
T Min2(T a, T b) {
return a < b ? a : b;
}
template <class T>
T Max3(T a, T b, T c) {
return Max2(Max2(a, b), c);
}
template <class T>
T Min3(T a, T b, T c) {
return Min2(Min2(a, b), c);
}
template <class T>
T Max4(T a, T b, T c, T d) {
return Max2(Max2(a, b), Max2(c, d));
}
template <class T>
T Min4(T a, T b, T c, T d) {
return Min2(Min2(a, b), Max2(c, d));
}
using namespace std;
long long int N;
vector<long long int> G[100010];
long long int dp[100010][2];
bool vis[100010];
long long int color[100010];
void dfs(long long int u) {
vis[u] = true;
if (color[u]) {
dp[u][0] = 0;
dp[u][1] = 1;
} else {
dp[u][0] = 1;
dp[u][1] = 0;
}
for (long long int v : G[u]) {
if (!vis[v]) {
dfs(v);
long long int temp[2];
temp[0] = dp[u][0];
temp[1] = dp[u][0];
temp[1] = (dp[u][1] * dp[v][1]) % MOD;
temp[1] = (temp[1] + dp[u][0] * dp[v][1]) % MOD;
temp[1] = (temp[1] + dp[u][1] * dp[v][0]) % MOD;
temp[0] = (dp[u][0] * dp[v][1]) % MOD;
temp[0] = (temp[0] + dp[u][0] * dp[v][0]) % MOD;
dp[u][0] = temp[0];
dp[u][1] = temp[1];
}
}
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> N;
for (int i = 1; i <= N - 1; i++) {
long long int x;
cin >> x;
G[x].push_back(i);
G[i].push_back(x);
}
for (int i = 0; i < N; i++) cin >> color[i];
dfs(0);
cout << dp[0][1] << "\n";
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const int maxn = 110000;
vector<int> tree[maxn];
bool vis[maxn];
int color[maxn];
long long f[maxn][2];
long long t1, t2;
void dfs(int root) {
vis[root] = true;
int siz = tree[root].size();
if (color[root] == 1) {
f[root][1] = 1;
f[root][0] = 0;
} else {
f[root][0] = 1;
f[root][1] = 0;
}
for (int i = 0; i < siz; ++i) {
int v = tree[root][i];
if (!vis[tree[root][i]]) {
dfs(tree[root][i]);
if (color[root] == 1) {
f[root][1] = f[root][1] * (f[v][0] + f[v][1]) % mod;
} else {
t1 = f[root][0] * (f[v][0] + f[v][1]);
t2 = f[root][1] * (f[v][0] + f[v][1]) + f[root][0] * f[v][1];
f[root][1] = t2 % mod;
f[root][0] = t1 % mod;
}
}
}
}
int main(int argc, char* argv[]) {
int n, p;
scanf("%d", &n);
for (int i = 1; i < n; ++i) {
scanf("%d", &p);
tree[i].push_back(p);
tree[p].push_back(i);
}
for (int i = 0; i < n; ++i) scanf("%d", &color[i]);
dfs(0);
printf("%I64d\n", f[0][1]);
return 0;
}
|
### Prompt
Develop a solution in CPP to the problem described below:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const int maxn = 110000;
vector<int> tree[maxn];
bool vis[maxn];
int color[maxn];
long long f[maxn][2];
long long t1, t2;
void dfs(int root) {
vis[root] = true;
int siz = tree[root].size();
if (color[root] == 1) {
f[root][1] = 1;
f[root][0] = 0;
} else {
f[root][0] = 1;
f[root][1] = 0;
}
for (int i = 0; i < siz; ++i) {
int v = tree[root][i];
if (!vis[tree[root][i]]) {
dfs(tree[root][i]);
if (color[root] == 1) {
f[root][1] = f[root][1] * (f[v][0] + f[v][1]) % mod;
} else {
t1 = f[root][0] * (f[v][0] + f[v][1]);
t2 = f[root][1] * (f[v][0] + f[v][1]) + f[root][0] * f[v][1];
f[root][1] = t2 % mod;
f[root][0] = t1 % mod;
}
}
}
}
int main(int argc, char* argv[]) {
int n, p;
scanf("%d", &n);
for (int i = 1; i < n; ++i) {
scanf("%d", &p);
tree[i].push_back(p);
tree[p].push_back(i);
}
for (int i = 0; i < n; ++i) scanf("%d", &color[i]);
dfs(0);
printf("%I64d\n", f[0][1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n, x, kk, dp[2000005][2], col[2000005], head[2000005];
struct Tree {
int nxt, to;
} e[2000005];
inline void link(int x, int y) {
e[++kk].nxt = head[x];
e[kk].to = y;
head[x] = kk;
}
void dfs1(int u, int fa) {
dp[u][col[u]] = 1;
dp[u][col[u] ^ 1] = 0;
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (v == fa) continue;
dfs1(v, u);
dp[u][1] = 1ll *
(1ll * dp[u][1] * (dp[v][0] + dp[v][1]) % 1000000007 +
1ll * dp[u][0] * dp[v][1] % 1000000007) %
1000000007;
dp[u][0] = 1ll *
(1ll * dp[u][0] * dp[v][0] % 1000000007 +
1ll * dp[u][0] * dp[v][1] % 1000000007) %
1000000007;
}
}
int main() {
scanf("%d", &n);
for (int i = 2; i <= n; i++) {
scanf("%d", &x);
x++;
link(x, i);
}
for (int i = 1; i <= n; i++) scanf("%d", &col[i]);
dfs1(1, -1);
printf("%d\n", (dp[1][1]) % 1000000007);
return 0;
}
|
### Prompt
Develop a solution in Cpp to the problem described below:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, x, kk, dp[2000005][2], col[2000005], head[2000005];
struct Tree {
int nxt, to;
} e[2000005];
inline void link(int x, int y) {
e[++kk].nxt = head[x];
e[kk].to = y;
head[x] = kk;
}
void dfs1(int u, int fa) {
dp[u][col[u]] = 1;
dp[u][col[u] ^ 1] = 0;
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (v == fa) continue;
dfs1(v, u);
dp[u][1] = 1ll *
(1ll * dp[u][1] * (dp[v][0] + dp[v][1]) % 1000000007 +
1ll * dp[u][0] * dp[v][1] % 1000000007) %
1000000007;
dp[u][0] = 1ll *
(1ll * dp[u][0] * dp[v][0] % 1000000007 +
1ll * dp[u][0] * dp[v][1] % 1000000007) %
1000000007;
}
}
int main() {
scanf("%d", &n);
for (int i = 2; i <= n; i++) {
scanf("%d", &x);
x++;
link(x, i);
}
for (int i = 1; i <= n; i++) scanf("%d", &col[i]);
dfs1(1, -1);
printf("%d\n", (dp[1][1]) % 1000000007);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 9;
const int mod = 1e9 + 7;
int col[N];
vector<int> ed[N];
long long dp[N][2];
long long rev(long long n) {
long long r = 1;
int b = mod - 2;
while (b > 0) {
if (b & 1) {
r = (r * n) % mod;
}
n = (n * n) % mod;
b >>= 1;
}
return r;
}
void dfs(int cur, int p) {
bool leaf = true;
long long zero = 1;
for (vector<int>::iterator vit = ed[cur].begin(); vit != ed[cur].end();
++vit) {
if ((*vit) != p) {
leaf = false;
dfs(*vit, cur);
zero = (zero * (dp[*vit][0] + dp[*vit][1])) % mod;
}
}
if (leaf) {
dp[cur][col[cur]] = 1;
return;
}
if (!col[cur]) {
dp[cur][0] = zero;
for (vector<int>::iterator vit = ed[cur].begin(); vit != ed[cur].end();
++vit) {
if ((*vit) != p) {
dp[cur][1] =
(dp[cur][1] +
((zero * rev(dp[*vit][0] + dp[*vit][1])) % mod) * dp[*vit][1]) %
mod;
dp[cur][1] %= mod;
}
}
} else {
dp[cur][1] = zero;
}
}
int main() {
int i, j, n, k;
while (cin >> n) {
for (i = 0; i < n; ++i) {
ed[i].clear();
}
for (i = 1; i < n; ++i) {
cin >> k;
ed[i].push_back(k);
ed[k].push_back(i);
}
for (i = 0; i < n; ++i) {
cin >> col[i];
}
memset(dp, 0, sizeof(dp));
dfs(0, -1);
cout << dp[0][1] << endl;
}
return 0;
}
|
### Prompt
Create a solution in Cpp for the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 9;
const int mod = 1e9 + 7;
int col[N];
vector<int> ed[N];
long long dp[N][2];
long long rev(long long n) {
long long r = 1;
int b = mod - 2;
while (b > 0) {
if (b & 1) {
r = (r * n) % mod;
}
n = (n * n) % mod;
b >>= 1;
}
return r;
}
void dfs(int cur, int p) {
bool leaf = true;
long long zero = 1;
for (vector<int>::iterator vit = ed[cur].begin(); vit != ed[cur].end();
++vit) {
if ((*vit) != p) {
leaf = false;
dfs(*vit, cur);
zero = (zero * (dp[*vit][0] + dp[*vit][1])) % mod;
}
}
if (leaf) {
dp[cur][col[cur]] = 1;
return;
}
if (!col[cur]) {
dp[cur][0] = zero;
for (vector<int>::iterator vit = ed[cur].begin(); vit != ed[cur].end();
++vit) {
if ((*vit) != p) {
dp[cur][1] =
(dp[cur][1] +
((zero * rev(dp[*vit][0] + dp[*vit][1])) % mod) * dp[*vit][1]) %
mod;
dp[cur][1] %= mod;
}
}
} else {
dp[cur][1] = zero;
}
}
int main() {
int i, j, n, k;
while (cin >> n) {
for (i = 0; i < n; ++i) {
ed[i].clear();
}
for (i = 1; i < n; ++i) {
cin >> k;
ed[i].push_back(k);
ed[k].push_back(i);
}
for (i = 0; i < n; ++i) {
cin >> col[i];
}
memset(dp, 0, sizeof(dp));
dfs(0, -1);
cout << dp[0][1] << endl;
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 100;
const int Mod = 1e9 + 7;
int Color[N];
long long Dp[N][2];
vector<int> Tree[N];
void DFS(int u) {
Dp[u][Color[u]] = 1;
for (auto v : Tree[u]) {
DFS(v);
Dp[u][1] = (Dp[u][1] * Dp[v][1] % Mod + Dp[u][1] * Dp[v][0] % Mod +
Dp[u][0] * Dp[v][1] % Mod) %
Mod;
Dp[u][0] = (Dp[u][0] * Dp[v][0] % Mod + Dp[u][0] * Dp[v][1] % Mod) % Mod;
}
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int p;
scanf("%d", &p);
Tree[p].push_back(i);
}
for (int i = 0; i < n; i++) scanf("%d", Color + i);
DFS(0);
printf("%I64d\n", Dp[0][1]);
return 0;
}
|
### Prompt
Your challenge is to write a CPP solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 100;
const int Mod = 1e9 + 7;
int Color[N];
long long Dp[N][2];
vector<int> Tree[N];
void DFS(int u) {
Dp[u][Color[u]] = 1;
for (auto v : Tree[u]) {
DFS(v);
Dp[u][1] = (Dp[u][1] * Dp[v][1] % Mod + Dp[u][1] * Dp[v][0] % Mod +
Dp[u][0] * Dp[v][1] % Mod) %
Mod;
Dp[u][0] = (Dp[u][0] * Dp[v][0] % Mod + Dp[u][0] * Dp[v][1] % Mod) % Mod;
}
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int p;
scanf("%d", &p);
Tree[p].push_back(i);
}
for (int i = 0; i < n; i++) scanf("%d", Color + i);
DFS(0);
printf("%I64d\n", Dp[0][1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 7;
int n, i, x;
int dp[N][2], A[N];
vector<int> V[N];
int f(int x, int k) {
int &r = dp[x][k], i;
if (r != -1) return r;
if (A[x]) {
r = 1;
for (i = 0; i < V[x].size(); i++) r = (long long)r * f(V[x][i], 0) % mod;
return r;
}
if (!V[x].size()) return r = !k;
vector<int> pre, suf;
pre.resize(V[x].size() + 5);
suf.resize(V[x].size() + 5);
pre[0] = suf[V[x].size()] = 1;
for (i = 0; i < V[x].size(); i++)
pre[i + 1] = (long long)pre[i] * f(V[x][i], 0) % mod;
for (i = V[x].size() - 1; i >= 0; i--)
suf[i] = (long long)suf[i + 1] * f(V[x][i], 0) % mod;
r = 0;
for (i = 0; i < V[x].size(); i++)
r = (r + (long long)f(V[x][i], 1) * pre[i] % mod * suf[i + 1] % mod) % mod;
if (!k) r = ((long long)r + suf[0]) % mod;
return r;
}
int main() {
scanf("%d", &n);
for (i = 2; i <= n; i++) {
scanf("%d", &x);
V[x + 1].push_back(i);
}
for (i = 1; i <= n; i++) {
scanf("%d", &x);
A[i] = x;
}
memset(dp, -1, sizeof dp);
cout << f(1, 1) << endl;
}
|
### Prompt
Generate a cpp solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 7;
int n, i, x;
int dp[N][2], A[N];
vector<int> V[N];
int f(int x, int k) {
int &r = dp[x][k], i;
if (r != -1) return r;
if (A[x]) {
r = 1;
for (i = 0; i < V[x].size(); i++) r = (long long)r * f(V[x][i], 0) % mod;
return r;
}
if (!V[x].size()) return r = !k;
vector<int> pre, suf;
pre.resize(V[x].size() + 5);
suf.resize(V[x].size() + 5);
pre[0] = suf[V[x].size()] = 1;
for (i = 0; i < V[x].size(); i++)
pre[i + 1] = (long long)pre[i] * f(V[x][i], 0) % mod;
for (i = V[x].size() - 1; i >= 0; i--)
suf[i] = (long long)suf[i + 1] * f(V[x][i], 0) % mod;
r = 0;
for (i = 0; i < V[x].size(); i++)
r = (r + (long long)f(V[x][i], 1) * pre[i] % mod * suf[i + 1] % mod) % mod;
if (!k) r = ((long long)r + suf[0]) % mod;
return r;
}
int main() {
scanf("%d", &n);
for (i = 2; i <= n; i++) {
scanf("%d", &x);
V[x + 1].push_back(i);
}
for (i = 1; i <= n; i++) {
scanf("%d", &x);
A[i] = x;
}
memset(dp, -1, sizeof dp);
cout << f(1, 1) << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const int MN = 100111;
long long f[2][MN];
vector<int> ke[MN];
int n, col[MN];
void init() {
for (int i = (1), _b = (n); i <= _b; i++) ke[i].clear();
memset(f, 0, sizeof f);
}
void dfs(int u) {
if (ke[u].size() == 0) {
if (col[u] == 0) {
f[0][u] = 1;
f[1][u] = 0;
} else {
f[0][u] = 0;
f[1][u] = 1;
}
return;
}
vector<long long> f0;
vector<long long> f1;
vector<long long> left, right;
int k = ke[u].size();
f0.resize(k);
f1.resize(k);
left.resize(k);
right.resize(k);
for (int i = 0, _a = (k); i < _a; i++) {
int v = ke[u][i];
dfs(v);
f0[i] = f[0][v];
f1[i] = f[1][v];
}
for (int i = 0, _a = (k); i < _a; i++) {
if (i == 0) {
left[i] = (f0[i] + f1[i]) % MOD;
} else {
left[i] = (left[i - 1] * f0[i] + left[i - 1] * f1[i]) % MOD;
}
}
for (int i = (k - 1), _b = (0); i >= _b; i--) {
if (i == k - 1) {
right[i] = (f0[i] + f1[i]) % MOD;
} else {
right[i] = (right[i + 1] * (f0[i] + f1[i])) % MOD;
}
}
if (col[u] == 0) {
f[0][u] = left[k - 1];
for (int i = 0, _a = (k); i < _a; i++) {
f[1][u] = (f[1][u] + f1[i] * ((i > 0) ? left[i - 1] : 1) % MOD *
((i < k - 1) ? right[i + 1] : 1)) %
MOD;
}
} else {
f[0][u] = 0;
f[1][u] = left[k - 1];
}
}
int main() {
ios ::sync_with_stdio(false);
while (cin >> n) {
init();
for (int i = (2), _b = (n); i <= _b; i++) {
int fi;
cin >> fi;
++fi;
ke[fi].push_back(i);
}
for (int i = (1), _b = (n); i <= _b; i++) cin >> col[i];
dfs(1);
cout << f[1][1] << endl;
}
return 0;
}
|
### Prompt
Your task is to create a CPP solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const int MN = 100111;
long long f[2][MN];
vector<int> ke[MN];
int n, col[MN];
void init() {
for (int i = (1), _b = (n); i <= _b; i++) ke[i].clear();
memset(f, 0, sizeof f);
}
void dfs(int u) {
if (ke[u].size() == 0) {
if (col[u] == 0) {
f[0][u] = 1;
f[1][u] = 0;
} else {
f[0][u] = 0;
f[1][u] = 1;
}
return;
}
vector<long long> f0;
vector<long long> f1;
vector<long long> left, right;
int k = ke[u].size();
f0.resize(k);
f1.resize(k);
left.resize(k);
right.resize(k);
for (int i = 0, _a = (k); i < _a; i++) {
int v = ke[u][i];
dfs(v);
f0[i] = f[0][v];
f1[i] = f[1][v];
}
for (int i = 0, _a = (k); i < _a; i++) {
if (i == 0) {
left[i] = (f0[i] + f1[i]) % MOD;
} else {
left[i] = (left[i - 1] * f0[i] + left[i - 1] * f1[i]) % MOD;
}
}
for (int i = (k - 1), _b = (0); i >= _b; i--) {
if (i == k - 1) {
right[i] = (f0[i] + f1[i]) % MOD;
} else {
right[i] = (right[i + 1] * (f0[i] + f1[i])) % MOD;
}
}
if (col[u] == 0) {
f[0][u] = left[k - 1];
for (int i = 0, _a = (k); i < _a; i++) {
f[1][u] = (f[1][u] + f1[i] * ((i > 0) ? left[i - 1] : 1) % MOD *
((i < k - 1) ? right[i + 1] : 1)) %
MOD;
}
} else {
f[0][u] = 0;
f[1][u] = left[k - 1];
}
}
int main() {
ios ::sync_with_stdio(false);
while (cin >> n) {
init();
for (int i = (2), _b = (n); i <= _b; i++) {
int fi;
cin >> fi;
++fi;
ke[fi].push_back(i);
}
for (int i = (1), _b = (n); i <= _b; i++) cin >> col[i];
dfs(1);
cout << f[1][1] << endl;
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long int power(long long int a, long long int b, long long int m) {
long long int ans = 1;
while (b) {
if (b & 1) ans = (ans * a) % m;
b /= 2;
a = (a * a) % m;
}
return ans;
}
long long int modInverse(long long int num) {
num = power(num, 1000000007 - 2, 1000000007);
return num;
}
long long int dp[300009][2];
long long int c[300009];
vector<long long int> v[300009];
void dfs(long long int num, long long int par) {
long long int tot = 1;
for (auto it : v[num]) {
if (it == par) continue;
dfs(it, num);
tot *= (dp[it][0] + dp[it][1]);
tot %= 1000000007;
}
if (c[num]) {
dp[num][1] = tot;
dp[num][0] = 0;
} else {
dp[num][0] = tot;
long long int ans = 0;
for (auto it : v[num]) {
if (it == par) continue;
ans += ((dp[it][1] * (modInverse(dp[it][0] + dp[it][1]))) % 1000000007 *
tot) %
1000000007;
ans %= 1000000007;
}
dp[num][1] = ans;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int n;
cin >> n;
for (long long int i = 0; i < n - 1; i++) {
long long int x;
cin >> x;
v[i + 1].push_back(x);
v[x].push_back(i + 1);
}
for (long long int i = 0; i < n; i++) cin >> c[i];
dfs(0, -1);
cout << dp[0][1] << endl;
}
|
### Prompt
Your challenge is to write a CPP solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long int power(long long int a, long long int b, long long int m) {
long long int ans = 1;
while (b) {
if (b & 1) ans = (ans * a) % m;
b /= 2;
a = (a * a) % m;
}
return ans;
}
long long int modInverse(long long int num) {
num = power(num, 1000000007 - 2, 1000000007);
return num;
}
long long int dp[300009][2];
long long int c[300009];
vector<long long int> v[300009];
void dfs(long long int num, long long int par) {
long long int tot = 1;
for (auto it : v[num]) {
if (it == par) continue;
dfs(it, num);
tot *= (dp[it][0] + dp[it][1]);
tot %= 1000000007;
}
if (c[num]) {
dp[num][1] = tot;
dp[num][0] = 0;
} else {
dp[num][0] = tot;
long long int ans = 0;
for (auto it : v[num]) {
if (it == par) continue;
ans += ((dp[it][1] * (modInverse(dp[it][0] + dp[it][1]))) % 1000000007 *
tot) %
1000000007;
ans %= 1000000007;
}
dp[num][1] = ans;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int n;
cin >> n;
for (long long int i = 0; i < n - 1; i++) {
long long int x;
cin >> x;
v[i + 1].push_back(x);
v[x].push_back(i + 1);
}
for (long long int i = 0; i < n; i++) cin >> c[i];
dfs(0, -1);
cout << dp[0][1] << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
vector<int> colour;
vector<vector<int>> g;
vector<long long> dp0, dp1;
void dfs(int v) {
dp0[v] = 1;
dp1[v] = 0;
for (int u : g[v]) {
dfs(u);
dp1[v] = dp1[v] * dp0[u] % MOD;
dp1[v] = (dp1[v] + dp0[v] * dp1[u]) % MOD;
dp0[v] = (dp0[v] * dp0[u]) % MOD;
}
if (colour[v])
dp1[v] = dp0[v];
else
dp0[v] = (dp1[v] + dp0[v]) % MOD;
}
int main() {
int n;
cin >> n;
g.resize(n);
for (int v = 1; v < n; v++) {
int u;
cin >> u;
g[u].push_back(v);
}
colour.resize(n);
for (int v = 0; v < n; v++) cin >> colour[v];
dp0.resize(n), dp1.resize(n);
dfs(0);
cout << dp1[0] << '\n';
return 0;
}
|
### Prompt
In Cpp, your task is to solve the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
vector<int> colour;
vector<vector<int>> g;
vector<long long> dp0, dp1;
void dfs(int v) {
dp0[v] = 1;
dp1[v] = 0;
for (int u : g[v]) {
dfs(u);
dp1[v] = dp1[v] * dp0[u] % MOD;
dp1[v] = (dp1[v] + dp0[v] * dp1[u]) % MOD;
dp0[v] = (dp0[v] * dp0[u]) % MOD;
}
if (colour[v])
dp1[v] = dp0[v];
else
dp0[v] = (dp1[v] + dp0[v]) % MOD;
}
int main() {
int n;
cin >> n;
g.resize(n);
for (int v = 1; v < n; v++) {
int u;
cin >> u;
g[u].push_back(v);
}
colour.resize(n);
for (int v = 0; v < n; v++) cin >> colour[v];
dp0.resize(n), dp1.resize(n);
dfs(0);
cout << dp1[0] << '\n';
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
using ii = pair<int, int>;
using ll = long long;
constexpr int MAXN = 5 + 200000;
constexpr int MOD = 7 + 1e9;
int c[MAXN], nodes, cnt[MAXN];
vector<int> g[MAXN], newt[MAXN];
int add(int a, int b) {
int ans = a + b;
if (ans >= MOD) ans -= MOD;
return ans;
}
int mul(int a, int b) { return (int)((1LL * a * b) % MOD); }
void dfs(int u, int p) {
if (p != -1) {
g[u].erase(find(g[u].begin(), g[u].end(), p));
}
for (int v : g[u]) {
dfs(v, u);
}
}
int n;
int dfs2(int u) {
cnt[u] = (u < n && c[u] == 1);
for (int v : newt[u]) {
cnt[u] += dfs2(v);
}
return cnt[u];
}
void mbin(int u, vector<int>::iterator s, vector<int>::iterator e) {
int dist = distance(s, e);
if (dist <= 2) {
while (s != e) {
newt[u].push_back(*s);
mbin(*s, g[*s].begin(), g[*s].end());
s++;
}
} else {
newt[u].push_back(*s);
mbin(*s, g[*s].begin(), g[*s].end());
newt[u].push_back(nodes++);
mbin(newt[u].back(), 1 + s, e);
}
}
int memo[MAXN][2];
int dp(int u, int f) {
int& ans = memo[u][f];
if (ans != -1) return ans;
int l = -1, r = -1;
bool leaf = newt[u].empty();
if (!leaf) l = newt[u][0], r = newt[u][1];
assert(leaf || (int)newt[u].size() == 2);
ans = 0;
if (cnt[u] == 0 && f == 0) {
return ans = 1;
}
if (u < n) {
if (c[u] == 1) {
if (f) {
ans = dp(u, 0);
} else {
if (!leaf) {
ans = mul(dp(l, cnt[l] >= 1), dp(r, cnt[r] >= 1));
} else {
ans = 1;
}
}
} else {
if (f) {
assert(!leaf);
ans = dp(u, 0);
int m = 1;
if (cnt[l] >= 1) m = mul(m, dp(l, 1));
if (cnt[r] >= 1) m = mul(m, dp(r, 1));
ans = add(ans, m);
} else {
if (!leaf) {
if (cnt[l] >= 1 && cnt[r] >= 1) {
ans = mul(dp(l, 0), dp(r, 1));
ans = add(ans, mul(dp(l, 1), dp(r, 0)));
} else if (cnt[l] >= 1) {
ans = dp(l, 0);
} else if (cnt[r] >= 1) {
ans = dp(r, 0);
} else {
assert(false);
}
}
}
}
} else {
assert(!leaf);
if (f) {
if (cnt[l] >= 1 && cnt[r] >= 1) {
ans = mul(dp(l, 1), dp(r, 1));
} else if (cnt[l] >= 1) {
ans = dp(l, 1);
} else if (cnt[r] >= 1) {
ans = dp(r, 1);
} else
assert(false);
} else {
if (cnt[l] >= 1 && cnt[r] >= 1) {
ans = mul(dp(l, 0), dp(r, 1));
ans = add(ans, mul(dp(l, 1), dp(r, 0)));
} else if (cnt[l] >= 1) {
ans = dp(l, 0);
} else if (cnt[r] >= 1) {
ans = dp(r, 0);
}
}
}
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n;
for (int i = 1; i < n; ++i) {
int v;
cin >> v;
g[i].push_back(v);
g[v].push_back(i);
}
for (int i = 0; i < n; ++i) {
cin >> c[i];
}
dfs(0, -1);
nodes = n;
mbin(0, g[0].begin(), g[0].end());
dfs2(0);
for (int i = 0; i < nodes; ++i) {
if (!newt[i].empty()) {
while ((int)newt[i].size() < 2) {
newt[i].push_back(MAXN - 1);
}
}
}
memset(memo, -1, sizeof memo);
cout << dp(0, 0) << '\n';
return 0;
}
|
### Prompt
Please provide a CPP coded solution to the problem described below:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using ii = pair<int, int>;
using ll = long long;
constexpr int MAXN = 5 + 200000;
constexpr int MOD = 7 + 1e9;
int c[MAXN], nodes, cnt[MAXN];
vector<int> g[MAXN], newt[MAXN];
int add(int a, int b) {
int ans = a + b;
if (ans >= MOD) ans -= MOD;
return ans;
}
int mul(int a, int b) { return (int)((1LL * a * b) % MOD); }
void dfs(int u, int p) {
if (p != -1) {
g[u].erase(find(g[u].begin(), g[u].end(), p));
}
for (int v : g[u]) {
dfs(v, u);
}
}
int n;
int dfs2(int u) {
cnt[u] = (u < n && c[u] == 1);
for (int v : newt[u]) {
cnt[u] += dfs2(v);
}
return cnt[u];
}
void mbin(int u, vector<int>::iterator s, vector<int>::iterator e) {
int dist = distance(s, e);
if (dist <= 2) {
while (s != e) {
newt[u].push_back(*s);
mbin(*s, g[*s].begin(), g[*s].end());
s++;
}
} else {
newt[u].push_back(*s);
mbin(*s, g[*s].begin(), g[*s].end());
newt[u].push_back(nodes++);
mbin(newt[u].back(), 1 + s, e);
}
}
int memo[MAXN][2];
int dp(int u, int f) {
int& ans = memo[u][f];
if (ans != -1) return ans;
int l = -1, r = -1;
bool leaf = newt[u].empty();
if (!leaf) l = newt[u][0], r = newt[u][1];
assert(leaf || (int)newt[u].size() == 2);
ans = 0;
if (cnt[u] == 0 && f == 0) {
return ans = 1;
}
if (u < n) {
if (c[u] == 1) {
if (f) {
ans = dp(u, 0);
} else {
if (!leaf) {
ans = mul(dp(l, cnt[l] >= 1), dp(r, cnt[r] >= 1));
} else {
ans = 1;
}
}
} else {
if (f) {
assert(!leaf);
ans = dp(u, 0);
int m = 1;
if (cnt[l] >= 1) m = mul(m, dp(l, 1));
if (cnt[r] >= 1) m = mul(m, dp(r, 1));
ans = add(ans, m);
} else {
if (!leaf) {
if (cnt[l] >= 1 && cnt[r] >= 1) {
ans = mul(dp(l, 0), dp(r, 1));
ans = add(ans, mul(dp(l, 1), dp(r, 0)));
} else if (cnt[l] >= 1) {
ans = dp(l, 0);
} else if (cnt[r] >= 1) {
ans = dp(r, 0);
} else {
assert(false);
}
}
}
}
} else {
assert(!leaf);
if (f) {
if (cnt[l] >= 1 && cnt[r] >= 1) {
ans = mul(dp(l, 1), dp(r, 1));
} else if (cnt[l] >= 1) {
ans = dp(l, 1);
} else if (cnt[r] >= 1) {
ans = dp(r, 1);
} else
assert(false);
} else {
if (cnt[l] >= 1 && cnt[r] >= 1) {
ans = mul(dp(l, 0), dp(r, 1));
ans = add(ans, mul(dp(l, 1), dp(r, 0)));
} else if (cnt[l] >= 1) {
ans = dp(l, 0);
} else if (cnt[r] >= 1) {
ans = dp(r, 0);
}
}
}
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n;
for (int i = 1; i < n; ++i) {
int v;
cin >> v;
g[i].push_back(v);
g[v].push_back(i);
}
for (int i = 0; i < n; ++i) {
cin >> c[i];
}
dfs(0, -1);
nodes = n;
mbin(0, g[0].begin(), g[0].end());
dfs2(0);
for (int i = 0; i < nodes; ++i) {
if (!newt[i].empty()) {
while ((int)newt[i].size() < 2) {
newt[i].push_back(MAXN - 1);
}
}
}
memset(memo, -1, sizeof memo);
cout << dp(0, 0) << '\n';
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int mod = 1000000007;
vector<int> g[100005];
int color[100005];
int dp[2][100005];
int mPow(int a, int x) {
int ret = 1;
while (x > 0) {
if (x & 1) ret = 1LL * ret * a % mod;
x >>= 1;
a = 1LL * a * a % mod;
}
return ret;
}
int ml[100005], mr[100005];
int solve(int v, int has, int par = -1) {
if (dp[has][v] != -1) return dp[has][v];
int ret = 1;
for (int u : g[v]) {
if (u == par) continue;
solve(u, 0, v);
solve(u, 1, v);
}
ml[0] = 1;
for (int i = 0; i < g[v].size(); ++i) {
int u = g[v][i];
if (u == par) {
ml[i + 1] = ml[i];
} else {
ml[i + 1] = 1LL * ml[i] * (dp[0][u] + dp[1][u]) % mod;
}
}
mr[g[v].size() + 1] = 1;
for (int i = (int)g[v].size() - 1; i >= 0; --i) {
int u = g[v][i];
if (u == par) {
mr[i + 1] = mr[i + 2];
} else {
mr[i + 1] = 1LL * mr[i + 2] * (dp[0][u] + dp[1][u]) % mod;
}
}
ret = mr[1];
if (has == 0) {
if (color[v] == 1) return dp[has][v] = 0;
return dp[has][v] = ret;
}
int ans = 0;
if (color[v] == 1) {
ans = ret;
} else {
for (int i = 0; i < g[v].size(); ++i) {
int u = g[v][i];
if (u == par) continue;
ans += 1LL * ml[i] * mr[i + 2] % mod * dp[1][u] % mod;
if (ans >= mod) ans -= mod;
}
}
return dp[has][v] = ans;
}
int main() {
int n, p;
scanf("%d", &n);
for (int i = 1; i < n; ++i) {
scanf("%d", &p);
g[i].push_back(p);
g[p].push_back(i);
}
for (int i = 0; i < n; ++i) {
scanf("%d", &color[i]);
}
memset(dp, -1, sizeof(dp));
for (int i = 0; i < n; ++i)
if (color[i] == 1) {
printf("%d\n", solve(i, 1));
break;
}
return 0;
}
|
### Prompt
Construct a Cpp code solution to the problem outlined:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int mod = 1000000007;
vector<int> g[100005];
int color[100005];
int dp[2][100005];
int mPow(int a, int x) {
int ret = 1;
while (x > 0) {
if (x & 1) ret = 1LL * ret * a % mod;
x >>= 1;
a = 1LL * a * a % mod;
}
return ret;
}
int ml[100005], mr[100005];
int solve(int v, int has, int par = -1) {
if (dp[has][v] != -1) return dp[has][v];
int ret = 1;
for (int u : g[v]) {
if (u == par) continue;
solve(u, 0, v);
solve(u, 1, v);
}
ml[0] = 1;
for (int i = 0; i < g[v].size(); ++i) {
int u = g[v][i];
if (u == par) {
ml[i + 1] = ml[i];
} else {
ml[i + 1] = 1LL * ml[i] * (dp[0][u] + dp[1][u]) % mod;
}
}
mr[g[v].size() + 1] = 1;
for (int i = (int)g[v].size() - 1; i >= 0; --i) {
int u = g[v][i];
if (u == par) {
mr[i + 1] = mr[i + 2];
} else {
mr[i + 1] = 1LL * mr[i + 2] * (dp[0][u] + dp[1][u]) % mod;
}
}
ret = mr[1];
if (has == 0) {
if (color[v] == 1) return dp[has][v] = 0;
return dp[has][v] = ret;
}
int ans = 0;
if (color[v] == 1) {
ans = ret;
} else {
for (int i = 0; i < g[v].size(); ++i) {
int u = g[v][i];
if (u == par) continue;
ans += 1LL * ml[i] * mr[i + 2] % mod * dp[1][u] % mod;
if (ans >= mod) ans -= mod;
}
}
return dp[has][v] = ans;
}
int main() {
int n, p;
scanf("%d", &n);
for (int i = 1; i < n; ++i) {
scanf("%d", &p);
g[i].push_back(p);
g[p].push_back(i);
}
for (int i = 0; i < n; ++i) {
scanf("%d", &color[i]);
}
memset(dp, -1, sizeof(dp));
for (int i = 0; i < n; ++i)
if (color[i] == 1) {
printf("%d\n", solve(i, 1));
break;
}
return 0;
}
```
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.