output
stringlengths 52
181k
| instruction
stringlengths 296
182k
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e5 + 5;
const long long mod = 1e9 + 7;
long long n;
vector<long long> adj[maxn];
long long f[maxn][2];
void dfs(long long u) {
for (long long i = 0; i < adj[u].size(); i++) {
long long v = adj[u][i];
dfs(v);
f[u][1] = (f[u][1] * f[v][0] % mod + f[u][0] * f[v][1] % mod +
f[u][1] * f[v][1] % mod) %
mod;
f[u][1] %= mod;
f[u][0] = f[u][0] * ((f[v][0] + f[v][1]) % mod) % mod;
f[u][0] %= mod;
}
}
signed main() {
cin >> n;
for (long long i = 1; i < n; i++) {
long long x;
cin >> x;
adj[x].push_back(i);
}
for (long long i = 0; i < n; i++) {
long long colour;
cin >> colour;
f[i][colour] = 1;
}
dfs(0);
cout << f[0][1] % mod;
}
|
### 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;
const long long mod = 1e9 + 7;
long long n;
vector<long long> adj[maxn];
long long f[maxn][2];
void dfs(long long u) {
for (long long i = 0; i < adj[u].size(); i++) {
long long v = adj[u][i];
dfs(v);
f[u][1] = (f[u][1] * f[v][0] % mod + f[u][0] * f[v][1] % mod +
f[u][1] * f[v][1] % mod) %
mod;
f[u][1] %= mod;
f[u][0] = f[u][0] * ((f[v][0] + f[v][1]) % mod) % mod;
f[u][0] %= mod;
}
}
signed main() {
cin >> n;
for (long long i = 1; i < n; i++) {
long long x;
cin >> x;
adj[x].push_back(i);
}
for (long long i = 0; i < n; i++) {
long long colour;
cin >> colour;
f[i][colour] = 1;
}
dfs(0);
cout << f[0][1] % mod;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long int inv(long long int a) {
int n = 1000000005;
int p = 1000000007;
long long int ret = 1;
while (n != 0) {
if (n % 2 == 1) ret = (ret * a) % p;
n /= 2;
a = a * a % p;
}
return ret;
}
int main() {
int n;
cin >> n;
long long int dp[n][2];
int i, j;
for (i = 0; i < n; i++) {
dp[i][0] = 0;
dp[i][1] = 0;
}
vector<int> child[n];
long long int temp;
for (i = 1; i < n; i++) {
cin >> temp;
child[temp].push_back(i);
}
int wb[n];
for (i = 0; i < n; i++) {
cin >> wb[i];
}
for (i = n - 1; i >= 0; i--) {
if (child[i].size() == 0) {
dp[i][wb[i]] = 1;
} else {
temp = 1;
for (j = 0; j < child[i].size(); j++) {
temp *= ((dp[child[i][j]][0] + dp[child[i][j]][1]) % 1000000007);
temp %= 1000000007;
}
if (wb[i] == 1) {
dp[i][1] = temp;
dp[i][0] = 0;
} else {
dp[i][0] = temp;
dp[i][1] = 0;
for (j = 0; j < child[i].size(); j++) {
dp[i][1] +=
temp * dp[child[i][j]][1] % 1000000007 *
inv((dp[child[i][j]][0] + dp[child[i][j]][1]) % 1000000007) %
1000000007;
dp[i][1] %= 1000000007;
}
}
}
}
cout << dp[0][1] << "\n";
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;
long long int inv(long long int a) {
int n = 1000000005;
int p = 1000000007;
long long int ret = 1;
while (n != 0) {
if (n % 2 == 1) ret = (ret * a) % p;
n /= 2;
a = a * a % p;
}
return ret;
}
int main() {
int n;
cin >> n;
long long int dp[n][2];
int i, j;
for (i = 0; i < n; i++) {
dp[i][0] = 0;
dp[i][1] = 0;
}
vector<int> child[n];
long long int temp;
for (i = 1; i < n; i++) {
cin >> temp;
child[temp].push_back(i);
}
int wb[n];
for (i = 0; i < n; i++) {
cin >> wb[i];
}
for (i = n - 1; i >= 0; i--) {
if (child[i].size() == 0) {
dp[i][wb[i]] = 1;
} else {
temp = 1;
for (j = 0; j < child[i].size(); j++) {
temp *= ((dp[child[i][j]][0] + dp[child[i][j]][1]) % 1000000007);
temp %= 1000000007;
}
if (wb[i] == 1) {
dp[i][1] = temp;
dp[i][0] = 0;
} else {
dp[i][0] = temp;
dp[i][1] = 0;
for (j = 0; j < child[i].size(); j++) {
dp[i][1] +=
temp * dp[child[i][j]][1] % 1000000007 *
inv((dp[child[i][j]][0] + dp[child[i][j]][1]) % 1000000007) %
1000000007;
dp[i][1] %= 1000000007;
}
}
}
}
cout << dp[0][1] << "\n";
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
int n;
long long dp[100005][2];
int c[100005];
vector<int> edge[100005];
int main() {
cin >> n;
for (int i = 0; i < n - 1; i++) {
int x;
cin >> x;
edge[x].push_back(i + 1);
}
for (int i = 0; i < n; i++) cin >> c[i];
for (int i = n - 1; i >= 0; i--) {
long long a0 = 0, a1 = 0;
if (c[i] == 0)
a0++;
else
a1++;
for (int j = 0; j < edge[i].size(); j++) {
int x = edge[i][j];
long long a01 = (a0 * dp[x][0] + a0 * dp[x][1]) % MOD;
long long a11 = (a1 * dp[x][0] + a1 * dp[x][1] + a0 * dp[x][1]) % MOD;
a0 = a01;
a1 = a11;
}
dp[i][0] = a0;
dp[i][1] = a1;
}
cout << 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 long long MOD = 1000000007;
int n;
long long dp[100005][2];
int c[100005];
vector<int> edge[100005];
int main() {
cin >> n;
for (int i = 0; i < n - 1; i++) {
int x;
cin >> x;
edge[x].push_back(i + 1);
}
for (int i = 0; i < n; i++) cin >> c[i];
for (int i = n - 1; i >= 0; i--) {
long long a0 = 0, a1 = 0;
if (c[i] == 0)
a0++;
else
a1++;
for (int j = 0; j < edge[i].size(); j++) {
int x = edge[i][j];
long long a01 = (a0 * dp[x][0] + a0 * dp[x][1]) % MOD;
long long a11 = (a1 * dp[x][0] + a1 * dp[x][1] + a0 * dp[x][1]) % MOD;
a0 = a01;
a1 = a11;
}
dp[i][0] = a0;
dp[i][1] = a1;
}
cout << dp[0][1];
return 0;
}
```
|
#include <bits/stdc++.h>
const double EPS = 1e-24;
const long long mod = 1000000007ll;
const long long mod1 = 1000000009ll;
const long long mod2 = 1100000009ll;
const double PI = 3.14159265359;
int INF = 2147483645;
long long 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 bit_count(long long _x) {
long long _ret = 0;
while (_x) {
if (_x % 2 == 1) _ret++;
_x /= 2;
}
return _ret;
}
long long bit(long long _mask, long long _i) {
return (_mask & (1 << _i)) == 0 ? 0 : 1;
}
long long powermod(long long _a, long long _b, long long _m) {
long long _r = 1;
while (_b) {
if (_b % 2 == 1) _r = (_r * _a) % _m;
_b /= 2;
_a = (_a * _a) % _m;
}
return _r;
}
long long add(long long a, long long b, long long m) {
long long first = a + b;
while (first >= m) first -= m;
return first;
}
long long sub(long long a, long long b, long long m) {
long long first = a - b;
while (first < 0) first += m;
return first;
}
long long mul(long long a, long long b, long long m) {
long long first = a * b;
first %= m;
return first;
}
long long operator*(pair<int, int> A, pair<int, int> B) {
return A.first * B.second - A.second * B.first;
}
vector<vector<long long> > dp(100000, vector<long long>(2, 0));
vector<int> visit(100000);
vector<vector<int> > gr(100000);
vector<int> color(100000);
void dfs(int i) {
visit[i] = 1;
vector<int>::iterator it;
dp[i][color[i]]++;
for (it = gr[i].begin(); it < gr[i].end(); it++) {
if (!visit[*it]) {
dfs(*it);
if (color[i] == 1) {
dp[i][1] = (dp[i][1] * ((dp[*it][0] + dp[*it][1]) % mod)) % mod;
dp[i][0] = 0;
} else {
dp[i][1] = (dp[i][1] * (dp[*it][0] + dp[*it][1])) % mod +
dp[i][0] * dp[*it][1] % mod;
dp[i][1] %= mod;
dp[i][0] = (dp[i][0] * (dp[*it][0] + dp[*it][1])) % mod;
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int n, a, b;
cin >> n;
for (int i = 0; i < int(n - 1); i++) {
cin >> a;
gr[i + 1].push_back(a);
gr[a].push_back(i + 1);
}
for (int i = 0; i < int(n); i++) {
cin >> color[i];
}
dfs(0);
cout << dp[0][1];
}
|
### 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 EPS = 1e-24;
const long long mod = 1000000007ll;
const long long mod1 = 1000000009ll;
const long long mod2 = 1100000009ll;
const double PI = 3.14159265359;
int INF = 2147483645;
long long 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 bit_count(long long _x) {
long long _ret = 0;
while (_x) {
if (_x % 2 == 1) _ret++;
_x /= 2;
}
return _ret;
}
long long bit(long long _mask, long long _i) {
return (_mask & (1 << _i)) == 0 ? 0 : 1;
}
long long powermod(long long _a, long long _b, long long _m) {
long long _r = 1;
while (_b) {
if (_b % 2 == 1) _r = (_r * _a) % _m;
_b /= 2;
_a = (_a * _a) % _m;
}
return _r;
}
long long add(long long a, long long b, long long m) {
long long first = a + b;
while (first >= m) first -= m;
return first;
}
long long sub(long long a, long long b, long long m) {
long long first = a - b;
while (first < 0) first += m;
return first;
}
long long mul(long long a, long long b, long long m) {
long long first = a * b;
first %= m;
return first;
}
long long operator*(pair<int, int> A, pair<int, int> B) {
return A.first * B.second - A.second * B.first;
}
vector<vector<long long> > dp(100000, vector<long long>(2, 0));
vector<int> visit(100000);
vector<vector<int> > gr(100000);
vector<int> color(100000);
void dfs(int i) {
visit[i] = 1;
vector<int>::iterator it;
dp[i][color[i]]++;
for (it = gr[i].begin(); it < gr[i].end(); it++) {
if (!visit[*it]) {
dfs(*it);
if (color[i] == 1) {
dp[i][1] = (dp[i][1] * ((dp[*it][0] + dp[*it][1]) % mod)) % mod;
dp[i][0] = 0;
} else {
dp[i][1] = (dp[i][1] * (dp[*it][0] + dp[*it][1])) % mod +
dp[i][0] * dp[*it][1] % mod;
dp[i][1] %= mod;
dp[i][0] = (dp[i][0] * (dp[*it][0] + dp[*it][1])) % mod;
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int n, a, b;
cin >> n;
for (int i = 0; i < int(n - 1); i++) {
cin >> a;
gr[i + 1].push_back(a);
gr[a].push_back(i + 1);
}
for (int i = 0; i < int(n); i++) {
cin >> color[i];
}
dfs(0);
cout << dp[0][1];
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<vector<long long> > adj;
vector<long long> arr;
pair<long long, long long> dfs(long long v, long long p = -1) {
pair<long long, long long> res = {0, 1};
for (long long x : adj[v]) {
if (x == p) continue;
pair<long long, long long> temp = dfs(x, v);
res.first =
((((temp.first + temp.second) % 1000000007) * res.first % 1000000007) +
(res.second * temp.first % 1000000007)) %
1000000007;
res.second =
res.second * ((temp.first + temp.second) % 1000000007) % 1000000007;
}
if (arr[v] == 1) {
res.first = res.second;
res.second = 0;
}
return res;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long n;
cin >> n;
adj.resize(n);
arr.resize(n);
for (long long i = 1; i < n; i++) {
long long x;
cin >> x;
adj[i].push_back(x);
adj[x].push_back(i);
}
for (long long &x : arr) cin >> x;
pair<long long, long long> ans = dfs(0);
cout << ans.first << "\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;
vector<vector<long long> > adj;
vector<long long> arr;
pair<long long, long long> dfs(long long v, long long p = -1) {
pair<long long, long long> res = {0, 1};
for (long long x : adj[v]) {
if (x == p) continue;
pair<long long, long long> temp = dfs(x, v);
res.first =
((((temp.first + temp.second) % 1000000007) * res.first % 1000000007) +
(res.second * temp.first % 1000000007)) %
1000000007;
res.second =
res.second * ((temp.first + temp.second) % 1000000007) % 1000000007;
}
if (arr[v] == 1) {
res.first = res.second;
res.second = 0;
}
return res;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long n;
cin >> n;
adj.resize(n);
arr.resize(n);
for (long long i = 1; i < n; i++) {
long long x;
cin >> x;
adj[i].push_back(x);
adj[x].push_back(i);
}
for (long long &x : arr) cin >> x;
pair<long long, long long> ans = dfs(0);
cout << ans.first << "\n";
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
vector<int> e[100000];
long long dp[100000][2];
int color[100000];
void dfs(int u) {
dp[u][1] = 0;
dp[u][0] = 1;
for (int v : e[u]) {
dfs(v);
dp[u][1] *= dp[v][0];
dp[u][1] %= MOD;
dp[u][1] += dp[u][0] * dp[v][1] % MOD;
dp[u][1] %= MOD;
dp[u][0] *= dp[v][0];
dp[u][0] %= MOD;
}
if (color[u]) {
dp[u][1] = dp[u][0];
} else {
dp[u][0] += dp[u][1];
dp[u][0] %= MOD;
}
}
int main() {
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int u;
cin >> u;
e[u].push_back(i);
}
for (int i = 0; i < n; i++) cin >> color[i];
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 long long MOD = 1000000007;
vector<int> e[100000];
long long dp[100000][2];
int color[100000];
void dfs(int u) {
dp[u][1] = 0;
dp[u][0] = 1;
for (int v : e[u]) {
dfs(v);
dp[u][1] *= dp[v][0];
dp[u][1] %= MOD;
dp[u][1] += dp[u][0] * dp[v][1] % MOD;
dp[u][1] %= MOD;
dp[u][0] *= dp[v][0];
dp[u][0] %= MOD;
}
if (color[u]) {
dp[u][1] = dp[u][0];
} else {
dp[u][0] += dp[u][1];
dp[u][0] %= MOD;
}
}
int main() {
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int u;
cin >> u;
e[u].push_back(i);
}
for (int i = 0; i < n; i++) cin >> color[i];
dfs(0);
cout << dp[0][1] << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(0) * 2;
const double EPS = 1e-8;
const long long MOD = 1e9 + 7;
const int MAXN = 1e5 + 5;
const int oo = 1e9;
const double foo = 1e30;
template <class T>
int getbit(T s, int i) {
return (s >> i) & 1;
}
template <class T>
T onbit(T s, int i) {
return s | (T(1) << i);
}
template <class T>
T offbit(T s, int i) {
return s & (~(T(1) << i));
}
template <class T>
int cntbit(T s) {
return __builtin_popcounll(s);
}
int n, pa[100100];
long long a[100100][2];
int main() {
cin >> n;
for (int i = 1; i < n; i++) {
cin >> pa[i];
}
for (int i = 0; i < n; i++) {
int x;
cin >> x;
a[i][x] = 1;
}
for (int i = n - 1; i >= 1; i--) {
a[pa[i]][1] =
(a[pa[i]][0] * a[i][1] + a[pa[i]][1] * (a[i][0] + a[i][1])) % MOD;
a[pa[i]][0] = (a[pa[i]][0] * (a[i][0] + a[i][1])) % MOD;
}
cout << a[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;
const double PI = acos(0) * 2;
const double EPS = 1e-8;
const long long MOD = 1e9 + 7;
const int MAXN = 1e5 + 5;
const int oo = 1e9;
const double foo = 1e30;
template <class T>
int getbit(T s, int i) {
return (s >> i) & 1;
}
template <class T>
T onbit(T s, int i) {
return s | (T(1) << i);
}
template <class T>
T offbit(T s, int i) {
return s & (~(T(1) << i));
}
template <class T>
int cntbit(T s) {
return __builtin_popcounll(s);
}
int n, pa[100100];
long long a[100100][2];
int main() {
cin >> n;
for (int i = 1; i < n; i++) {
cin >> pa[i];
}
for (int i = 0; i < n; i++) {
int x;
cin >> x;
a[i][x] = 1;
}
for (int i = n - 1; i >= 1; i--) {
a[pa[i]][1] =
(a[pa[i]][0] * a[i][1] + a[pa[i]][1] * (a[i][0] + a[i][1])) % MOD;
a[pa[i]][0] = (a[pa[i]][0] * (a[i][0] + a[i][1])) % MOD;
}
cout << a[0][1] << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
int N;
vector<int> adj[100100];
bool black[100100];
ll dp[100100][2];
void r(int node, int par) {
for (int i : adj[node])
if (i != par) r(i, node);
ll first = 1;
for (int i : adj[node])
if (i != par) {
first = (first * (dp[i][0] + dp[i][1])) % (ll)(1e9 + 7);
}
if (black[node]) {
;
dp[node][1] = first;
return;
}
dp[node][0] = first;
ll temp[2][2];
temp[1][0] = 1, temp[1][1] = 0;
bool childind = 0;
for (int i : adj[node])
if (i != par) {
temp[childind][0] =
(temp[!childind][0] * (dp[i][0] + dp[i][1])) % (ll)(1e9 + 7);
temp[childind][1] = (temp[!childind][0] * dp[i][1] +
temp[!childind][1] * (dp[i][0] + dp[i][1])) %
(ll)(1e9 + 7);
childind = !childind;
}
childind = !childind;
assert(temp[childind][0] == first);
dp[node][1] = temp[childind][1];
}
int main() {
scanf("%d", &N);
for (int i = 1; i < N; i++) {
int A;
scanf("%d", &A);
adj[A].push_back(i);
adj[i].push_back(A);
}
for (int i = 0; i < N; i++) {
int A;
scanf("%d", &A);
black[i] = A;
}
r(0, -1);
printf("%d\n", dp[0][1]);
for (int i = 0; i < N; i++) {
;
}
}
|
### 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;
using ll = long long;
using ld = long double;
int N;
vector<int> adj[100100];
bool black[100100];
ll dp[100100][2];
void r(int node, int par) {
for (int i : adj[node])
if (i != par) r(i, node);
ll first = 1;
for (int i : adj[node])
if (i != par) {
first = (first * (dp[i][0] + dp[i][1])) % (ll)(1e9 + 7);
}
if (black[node]) {
;
dp[node][1] = first;
return;
}
dp[node][0] = first;
ll temp[2][2];
temp[1][0] = 1, temp[1][1] = 0;
bool childind = 0;
for (int i : adj[node])
if (i != par) {
temp[childind][0] =
(temp[!childind][0] * (dp[i][0] + dp[i][1])) % (ll)(1e9 + 7);
temp[childind][1] = (temp[!childind][0] * dp[i][1] +
temp[!childind][1] * (dp[i][0] + dp[i][1])) %
(ll)(1e9 + 7);
childind = !childind;
}
childind = !childind;
assert(temp[childind][0] == first);
dp[node][1] = temp[childind][1];
}
int main() {
scanf("%d", &N);
for (int i = 1; i < N; i++) {
int A;
scanf("%d", &A);
adj[A].push_back(i);
adj[i].push_back(A);
}
for (int i = 0; i < N; i++) {
int A;
scanf("%d", &A);
black[i] = A;
}
r(0, -1);
printf("%d\n", dp[0][1]);
for (int i = 0; i < N; i++) {
;
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long sz = 2 * 1e5 + 5, mod = 1e9 + 7;
vector<long long> adj[sz];
long long dp[sz][2], x[sz];
void dfs(long long v, long long p) {
dp[v][x[v]] = 1;
for (auto u : adj[v]) {
if (u == p) continue;
dfs(u, v);
dp[v][1] =
(dp[v][1] * dp[u][1] + dp[v][1] * dp[u][0] + dp[v][0] * dp[u][1]) % mod;
dp[v][0] = (dp[v][0] * dp[u][1] + dp[v][0] * dp[u][0]) % mod;
}
}
int main() {
long long n, u, i;
cin >> n;
for (i = 1; i < n; i++) {
cin >> u;
adj[u].push_back(i);
}
for (i = 0; i < n; i++) cin >> x[i];
dfs(0, -1);
cout << dp[0][1] << endl;
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 long long sz = 2 * 1e5 + 5, mod = 1e9 + 7;
vector<long long> adj[sz];
long long dp[sz][2], x[sz];
void dfs(long long v, long long p) {
dp[v][x[v]] = 1;
for (auto u : adj[v]) {
if (u == p) continue;
dfs(u, v);
dp[v][1] =
(dp[v][1] * dp[u][1] + dp[v][1] * dp[u][0] + dp[v][0] * dp[u][1]) % mod;
dp[v][0] = (dp[v][0] * dp[u][1] + dp[v][0] * dp[u][0]) % mod;
}
}
int main() {
long long n, u, i;
cin >> n;
for (i = 1; i < n; i++) {
cin >> u;
adj[u].push_back(i);
}
for (i = 0; i < n; i++) cin >> x[i];
dfs(0, -1);
cout << dp[0][1] << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
const long long mod = 1000000007;
long long dp[maxn][2];
int color[maxn];
vector<int> G[maxn];
bool vis[maxn];
void dfs(int s) {
dp[s][0] = 1;
dp[s][1] = 0;
vis[s] = true;
for (int i = 0; i < G[s].size(); i++) {
int t = G[s][i];
if (vis[t]) continue;
dfs(t);
dp[s][1] = (dp[s][0] * dp[t][1] + dp[s][1] * dp[t][0]) % mod;
dp[s][0] = dp[s][0] * dp[t][0] % mod;
}
if (color[s] == 1) {
dp[s][1] = dp[s][0];
} else
dp[s][0] += dp[s][1];
dp[s][1] %= mod;
dp[s][0] %= mod;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int temp;
scanf("%d", &temp);
G[temp].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
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 = 100005;
const long long mod = 1000000007;
long long dp[maxn][2];
int color[maxn];
vector<int> G[maxn];
bool vis[maxn];
void dfs(int s) {
dp[s][0] = 1;
dp[s][1] = 0;
vis[s] = true;
for (int i = 0; i < G[s].size(); i++) {
int t = G[s][i];
if (vis[t]) continue;
dfs(t);
dp[s][1] = (dp[s][0] * dp[t][1] + dp[s][1] * dp[t][0]) % mod;
dp[s][0] = dp[s][0] * dp[t][0] % mod;
}
if (color[s] == 1) {
dp[s][1] = dp[s][0];
} else
dp[s][0] += dp[s][1];
dp[s][1] %= mod;
dp[s][0] %= mod;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int temp;
scanf("%d", &temp);
G[temp].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;
bool vis[100000] = {false};
int dp[100000][2] = {0};
int ans = 1;
void dfs(vector<int> *v, int root, bool *vis, int *col) {
vis[root] = 1;
dp[root][0] = 1 - col[root];
dp[root][1] = col[root];
for (int i = 0; i < v[root].size(); i++) {
int a = v[root][i];
if (vis[a] != 1) {
dfs(v, a, vis, col);
int x, y;
x = dp[root][0];
y = dp[root][1];
dp[root][0] = dp[root][1] = 0;
dp[root][0] =
(dp[root][0] + ((long long)x * dp[a][0]) % 1000000007) % 1000000007;
dp[root][0] =
(dp[root][0] + ((long long)x * dp[a][1]) % 1000000007) % 1000000007;
dp[root][1] =
(dp[root][1] + ((long long)y * dp[a][0]) % 1000000007) % 1000000007;
dp[root][1] =
(dp[root][1] + ((long long)x * dp[a][1]) % 1000000007) % 1000000007;
dp[root][1] =
(dp[root][1] + ((long long)y * dp[a][1]) % 1000000007) % 1000000007;
}
}
}
const int dir[4][2] = {0, -1, 0, 1, -1, 0, 1, 0};
int main() {
int i, n;
cin >> n;
vector<int> v[100000];
for (i = 0; i < n - 1; i++) {
int a;
cin >> a;
v[i + 1].push_back(a);
v[a].push_back(i + 1);
}
int col[100000], k = 0;
for (i = 0; i < n; i++) {
int a;
cin >> a;
col[i] = a;
}
dfs(v, 0, vis, col);
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;
bool vis[100000] = {false};
int dp[100000][2] = {0};
int ans = 1;
void dfs(vector<int> *v, int root, bool *vis, int *col) {
vis[root] = 1;
dp[root][0] = 1 - col[root];
dp[root][1] = col[root];
for (int i = 0; i < v[root].size(); i++) {
int a = v[root][i];
if (vis[a] != 1) {
dfs(v, a, vis, col);
int x, y;
x = dp[root][0];
y = dp[root][1];
dp[root][0] = dp[root][1] = 0;
dp[root][0] =
(dp[root][0] + ((long long)x * dp[a][0]) % 1000000007) % 1000000007;
dp[root][0] =
(dp[root][0] + ((long long)x * dp[a][1]) % 1000000007) % 1000000007;
dp[root][1] =
(dp[root][1] + ((long long)y * dp[a][0]) % 1000000007) % 1000000007;
dp[root][1] =
(dp[root][1] + ((long long)x * dp[a][1]) % 1000000007) % 1000000007;
dp[root][1] =
(dp[root][1] + ((long long)y * dp[a][1]) % 1000000007) % 1000000007;
}
}
}
const int dir[4][2] = {0, -1, 0, 1, -1, 0, 1, 0};
int main() {
int i, n;
cin >> n;
vector<int> v[100000];
for (i = 0; i < n - 1; i++) {
int a;
cin >> a;
v[i + 1].push_back(a);
v[a].push_back(i + 1);
}
int col[100000], k = 0;
for (i = 0; i < n; i++) {
int a;
cin >> a;
col[i] = a;
}
dfs(v, 0, vis, col);
cout << dp[0][1] << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e5 + 5;
const long long MOD = (long long)1e9 + 7;
vector<int> adj[N];
int n, x[N];
long long dp[N][2];
void dfs(int u) {
dp[u][0] = 1LL;
dp[u][1] = 0LL;
for (int i = 0; i < adj[u].size(); i++) {
int v = adj[u][i];
dfs(v);
dp[u][1] = (dp[u][1] % MOD * dp[v][0] % MOD) % MOD;
dp[u][1] =
(dp[u][1] % MOD + ((dp[u][0] % MOD * dp[v][1] % MOD) % MOD) % MOD) %
MOD;
dp[u][0] = (dp[u][0] % MOD * dp[v][0] % MOD) % MOD;
}
if (x[u] == 1)
dp[u][1] = dp[u][0];
else
dp[u][0] = (dp[u][0] % MOD + dp[u][1] % MOD) % MOD;
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int p;
scanf("%d", &p);
adj[p].push_back(i);
}
for (int i = 0; i < n; i++) scanf("%d", &x[i]);
dfs(0);
printf("%lld\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>
using namespace std;
const int N = (int)1e5 + 5;
const long long MOD = (long long)1e9 + 7;
vector<int> adj[N];
int n, x[N];
long long dp[N][2];
void dfs(int u) {
dp[u][0] = 1LL;
dp[u][1] = 0LL;
for (int i = 0; i < adj[u].size(); i++) {
int v = adj[u][i];
dfs(v);
dp[u][1] = (dp[u][1] % MOD * dp[v][0] % MOD) % MOD;
dp[u][1] =
(dp[u][1] % MOD + ((dp[u][0] % MOD * dp[v][1] % MOD) % MOD) % MOD) %
MOD;
dp[u][0] = (dp[u][0] % MOD * dp[v][0] % MOD) % MOD;
}
if (x[u] == 1)
dp[u][1] = dp[u][0];
else
dp[u][0] = (dp[u][0] % MOD + dp[u][1] % MOD) % MOD;
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int p;
scanf("%d", &p);
adj[p].push_back(i);
}
for (int i = 0; i < n; i++) scanf("%d", &x[i]);
dfs(0);
printf("%lld\n", dp[0][1] % MOD);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n, i, p[100010], a, fa;
bool x[100010];
long long ans[100010][2], an1, an0;
const long long kkk = 1000000007;
priority_queue<long long> q1;
int main() {
scanf("%d", &n);
for (i = 1; i < n; i++) scanf("%d", &p[i]);
for (i = 0; i < n; i++) {
scanf("%d", &a);
if (a) {
x[i] = 1;
ans[i][1] = 1;
} else {
x[i] = 0;
ans[i][0] = 1;
}
}
for (i = n - 1; i > 0; i--) {
fa = p[i];
an0 = (ans[fa][0] * ans[i][0] % kkk + ans[fa][0] * ans[i][1] % kkk) % kkk;
an1 = (ans[fa][0] * ans[i][1] % kkk + ans[fa][1] * ans[i][1] % kkk +
ans[fa][1] * ans[i][0] % kkk) %
kkk;
ans[fa][0] = an0;
ans[fa][1] = an1;
}
printf("%I64d\n", ans[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;
int n, i, p[100010], a, fa;
bool x[100010];
long long ans[100010][2], an1, an0;
const long long kkk = 1000000007;
priority_queue<long long> q1;
int main() {
scanf("%d", &n);
for (i = 1; i < n; i++) scanf("%d", &p[i]);
for (i = 0; i < n; i++) {
scanf("%d", &a);
if (a) {
x[i] = 1;
ans[i][1] = 1;
} else {
x[i] = 0;
ans[i][0] = 1;
}
}
for (i = n - 1; i > 0; i--) {
fa = p[i];
an0 = (ans[fa][0] * ans[i][0] % kkk + ans[fa][0] * ans[i][1] % kkk) % kkk;
an1 = (ans[fa][0] * ans[i][1] % kkk + ans[fa][1] * ans[i][1] % kkk +
ans[fa][1] * ans[i][0] % kkk) %
kkk;
ans[fa][0] = an0;
ans[fa][1] = an1;
}
printf("%I64d\n", ans[0][1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MO = 1000000007;
int n, dp[200000][2], t;
vector<int> e[200000];
void dfs(int x) {
for (int i = 0; i < e[x].size(); i++) {
int t = e[x][i];
dfs(t);
dp[x][1] =
((long long)dp[x][0] * dp[t][1] + (long long)dp[x][1] * dp[t][0] +
(long long)dp[x][1] * dp[t][1]) %
MO;
dp[x][0] =
((long long)dp[x][0] * dp[t][1] + (long long)dp[x][0] * dp[t][0]) % MO;
}
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n - 1; i++) {
scanf("%d", &t);
e[t].push_back(i + 1);
}
dfs(0);
for (int i = 0; i < n; i++) {
scanf("%d", &t);
if (t) {
dp[i][0] = 0;
dp[i][1] = 1;
} else {
dp[i][0] = 1;
dp[i][1] = 0;
}
}
dfs(0);
printf("%d\n", 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 int MO = 1000000007;
int n, dp[200000][2], t;
vector<int> e[200000];
void dfs(int x) {
for (int i = 0; i < e[x].size(); i++) {
int t = e[x][i];
dfs(t);
dp[x][1] =
((long long)dp[x][0] * dp[t][1] + (long long)dp[x][1] * dp[t][0] +
(long long)dp[x][1] * dp[t][1]) %
MO;
dp[x][0] =
((long long)dp[x][0] * dp[t][1] + (long long)dp[x][0] * dp[t][0]) % MO;
}
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n - 1; i++) {
scanf("%d", &t);
e[t].push_back(i + 1);
}
dfs(0);
for (int i = 0; i < n; i++) {
scanf("%d", &t);
if (t) {
dp[i][0] = 0;
dp[i][1] = 1;
} else {
dp[i][0] = 1;
dp[i][1] = 0;
}
}
dfs(0);
printf("%d\n", dp[0][1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
const int maxm = 1000100;
const int INF = 0x3f3f3f3f;
const int mod = 1000000007;
int n, m;
int sum[maxn];
int a[maxn];
vector<int> g[maxn];
long long dp[maxn][2];
int dfs(int x, int fa) {
int i, j;
sum[x] = a[x];
for (i = 0; i < g[x].size(); i++) {
int temp = g[x][i];
sum[x] += dfs(temp, x);
}
return sum[x];
}
void Dp(int x, int fa) {
int i, j;
if (a[x] == 1)
dp[x][1] = 1;
else
dp[x][0] = 1;
for (i = 0; i < g[x].size(); i++) {
int v = g[x][i];
if (v == fa) continue;
Dp(v, x);
dp[x][1] = (dp[x][1] * (dp[v][0] + dp[v][1]) + dp[x][0] * dp[v][1]) % mod;
dp[x][0] = (dp[x][0] * (dp[v][0] + dp[v][1])) % mod;
}
return;
}
int main() {
int i, j;
int k;
scanf("%d", &n);
for (i = 1; i < n; i++) {
int x;
scanf("%d", &x);
g[x].push_back(i);
g[i].push_back(x);
}
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
memset(dp, 0, sizeof(dp));
Dp(0, -1);
printf("%d\n", 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 int maxn = 100010;
const int maxm = 1000100;
const int INF = 0x3f3f3f3f;
const int mod = 1000000007;
int n, m;
int sum[maxn];
int a[maxn];
vector<int> g[maxn];
long long dp[maxn][2];
int dfs(int x, int fa) {
int i, j;
sum[x] = a[x];
for (i = 0; i < g[x].size(); i++) {
int temp = g[x][i];
sum[x] += dfs(temp, x);
}
return sum[x];
}
void Dp(int x, int fa) {
int i, j;
if (a[x] == 1)
dp[x][1] = 1;
else
dp[x][0] = 1;
for (i = 0; i < g[x].size(); i++) {
int v = g[x][i];
if (v == fa) continue;
Dp(v, x);
dp[x][1] = (dp[x][1] * (dp[v][0] + dp[v][1]) + dp[x][0] * dp[v][1]) % mod;
dp[x][0] = (dp[x][0] * (dp[v][0] + dp[v][1])) % mod;
}
return;
}
int main() {
int i, j;
int k;
scanf("%d", &n);
for (i = 1; i < n; i++) {
int x;
scanf("%d", &x);
g[x].push_back(i);
g[i].push_back(x);
}
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
memset(dp, 0, sizeof(dp));
Dp(0, -1);
printf("%d\n", dp[0][1]);
return 0;
}
```
|
#include <bits/stdc++.h>
long long shouldendl = 1;
long long INF = (long long)(1e9 + 7);
long long MOD = (long long)(1e9 + 7);
using namespace std;
long long modInverse(long long a) {
long long m = MOD, m0 = m, y = 0, x = 1;
if (m == 1) return 0;
while (a > 1) {
long long q = a / m, t = m;
m = a % m, a = t, t = y;
y = x - q * y, x = t;
}
if (x < 0) x += m0;
return x;
}
long long add(long long a, long long b) {
return ((a % MOD) + (b % MOD)) % MOD;
}
long long mul(long long a, long long b) {
return ((a % MOD) * (b % MOD)) % MOD;
}
long long sub(long long a, long long b) {
long long ans = ((a % MOD - b % MOD)) % MOD;
if (ans < 0) ans += MOD;
return ans;
}
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
void MAIN(long long i, long long t);
int get(int f = 2) {
if (f != 2) return 1;
long long t;
cin >> t;
;
return t;
}
template <class T>
int logd(T name) {
cout << name << endl;
return 0;
}
void test(int t) {
for (long long i = 1; i <= t; i++) {
MAIN(i, t);
if (shouldendl) cout << "\n";
}
}
long long power(long long x, long long y) {
if (y == 0) return 1;
long long res = 1;
x = x % MOD;
if (x == 0) return 0;
while (y > 0) {
if (y & 1) res = (res * x) % MOD;
y = y >> 1;
x = (x * x) % MOD;
}
return res;
}
template <typename T, typename U>
std::pair<T, U> operator+(const std::pair<T, U> &l, const std::pair<T, U> &r) {
return {l.first + r.first, l.second + r.second};
}
long long popcnt(long long x) { return __builtin_popcountll(x); }
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
template <class T>
bool input(vector<T> &v, long long n = -1) {
if (n == -1) assert(v.size() != 0);
if (n == -1) n = v.size();
for (auto &i : v) cin >> i;
return 0;
}
template <class T>
bool print(vector<T> v, long long l = 0, long long r = -1) {
if (r == -1) r = v.size();
for (int i = l; i < r; i++) cout << v[i] << " ";
return 0;
}
vector<int> edges[200005];
vector<int> a(200005);
int dp[200005][2];
map<int, bool> visited;
long long n;
void dfs(long long u) {
visited[u] = true;
dp[u][0] = 1 - a[u];
dp[u][1] = a[u];
for (auto v : edges[u]) {
if (visited[v]) continue;
dfs(v);
int zero = dp[u][0];
int one = dp[u][1];
dp[u][0] = mul(zero, dp[v][1]);
dp[u][1] = mul(one, dp[v][1]);
dp[u][0] += mul(zero, dp[v][0]);
dp[u][1] += add(mul(zero, dp[v][1]), mul(one, dp[v][0]));
dp[u][0] %= MOD;
dp[u][1] %= MOD;
}
}
void MAIN(long long current_test_case, long long total_test_cases) {
cin >> n;
for (int i = 0; i < n - 1; i++) {
int u = i + 1, v;
cin >> v;
edges[u].push_back(v);
edges[v].push_back(u);
}
for (int i = 0; i < n; i++) cin >> a[i];
dfs(0);
cout << dp[0][1];
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
shouldendl = 1;
test(get(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>
long long shouldendl = 1;
long long INF = (long long)(1e9 + 7);
long long MOD = (long long)(1e9 + 7);
using namespace std;
long long modInverse(long long a) {
long long m = MOD, m0 = m, y = 0, x = 1;
if (m == 1) return 0;
while (a > 1) {
long long q = a / m, t = m;
m = a % m, a = t, t = y;
y = x - q * y, x = t;
}
if (x < 0) x += m0;
return x;
}
long long add(long long a, long long b) {
return ((a % MOD) + (b % MOD)) % MOD;
}
long long mul(long long a, long long b) {
return ((a % MOD) * (b % MOD)) % MOD;
}
long long sub(long long a, long long b) {
long long ans = ((a % MOD - b % MOD)) % MOD;
if (ans < 0) ans += MOD;
return ans;
}
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
void MAIN(long long i, long long t);
int get(int f = 2) {
if (f != 2) return 1;
long long t;
cin >> t;
;
return t;
}
template <class T>
int logd(T name) {
cout << name << endl;
return 0;
}
void test(int t) {
for (long long i = 1; i <= t; i++) {
MAIN(i, t);
if (shouldendl) cout << "\n";
}
}
long long power(long long x, long long y) {
if (y == 0) return 1;
long long res = 1;
x = x % MOD;
if (x == 0) return 0;
while (y > 0) {
if (y & 1) res = (res * x) % MOD;
y = y >> 1;
x = (x * x) % MOD;
}
return res;
}
template <typename T, typename U>
std::pair<T, U> operator+(const std::pair<T, U> &l, const std::pair<T, U> &r) {
return {l.first + r.first, l.second + r.second};
}
long long popcnt(long long x) { return __builtin_popcountll(x); }
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
template <class T>
bool input(vector<T> &v, long long n = -1) {
if (n == -1) assert(v.size() != 0);
if (n == -1) n = v.size();
for (auto &i : v) cin >> i;
return 0;
}
template <class T>
bool print(vector<T> v, long long l = 0, long long r = -1) {
if (r == -1) r = v.size();
for (int i = l; i < r; i++) cout << v[i] << " ";
return 0;
}
vector<int> edges[200005];
vector<int> a(200005);
int dp[200005][2];
map<int, bool> visited;
long long n;
void dfs(long long u) {
visited[u] = true;
dp[u][0] = 1 - a[u];
dp[u][1] = a[u];
for (auto v : edges[u]) {
if (visited[v]) continue;
dfs(v);
int zero = dp[u][0];
int one = dp[u][1];
dp[u][0] = mul(zero, dp[v][1]);
dp[u][1] = mul(one, dp[v][1]);
dp[u][0] += mul(zero, dp[v][0]);
dp[u][1] += add(mul(zero, dp[v][1]), mul(one, dp[v][0]));
dp[u][0] %= MOD;
dp[u][1] %= MOD;
}
}
void MAIN(long long current_test_case, long long total_test_cases) {
cin >> n;
for (int i = 0; i < n - 1; i++) {
int u = i + 1, v;
cin >> v;
edges[u].push_back(v);
edges[v].push_back(u);
}
for (int i = 0; i < n; i++) cin >> a[i];
dfs(0);
cout << dp[0][1];
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
shouldendl = 1;
test(get(1));
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int start[n];
int to[n - 1];
int next[n - 1];
memset(start, -1, n * sizeof(int));
for (int i = 0; i <= n - 2; i++) {
int p;
cin >> p;
to[i] = i + 1;
next[i] = start[p];
start[p] = i;
}
bool isBlack[n];
for (int i = 0; i <= n; i++) {
cin >> isBlack[i];
}
long long dp[n][2];
memset(dp, 0, sizeof(long long) * n * 2);
for (int i = n - 1; i >= 0; i--) {
dp[i][isBlack[i]] = 1;
for (int e = start[i]; e != -1; e = next[e]) {
int child = to[e];
dp[i][1] =
dp[i][1] * (dp[child][0] + dp[child][1]) + dp[i][0] * dp[child][1];
dp[i][1] %= 1000 * 1000 * 1000 + 7;
dp[i][0] = dp[i][0] * (dp[child][0] + dp[child][1]);
dp[i][0] %= 1000 * 1000 * 1000 + 7;
}
}
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;
int main() {
int n;
cin >> n;
int start[n];
int to[n - 1];
int next[n - 1];
memset(start, -1, n * sizeof(int));
for (int i = 0; i <= n - 2; i++) {
int p;
cin >> p;
to[i] = i + 1;
next[i] = start[p];
start[p] = i;
}
bool isBlack[n];
for (int i = 0; i <= n; i++) {
cin >> isBlack[i];
}
long long dp[n][2];
memset(dp, 0, sizeof(long long) * n * 2);
for (int i = n - 1; i >= 0; i--) {
dp[i][isBlack[i]] = 1;
for (int e = start[i]; e != -1; e = next[e]) {
int child = to[e];
dp[i][1] =
dp[i][1] * (dp[child][0] + dp[child][1]) + dp[i][0] * dp[child][1];
dp[i][1] %= 1000 * 1000 * 1000 + 7;
dp[i][0] = dp[i][0] * (dp[child][0] + dp[child][1]);
dp[i][0] %= 1000 * 1000 * 1000 + 7;
}
}
cout << dp[0][1] << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long dp[100007][2];
int c[100007], n;
vector<int> e[100007];
const long long mod = 1000000007;
void clr() {
for (int i = 0; i < 100007; i++) {
e[i].clear();
}
memset(dp, 0, sizeof(dp));
}
void add(int u, int v) {
e[u].push_back(v);
e[v].push_back(u);
}
void dfs(int u, int p) {
dp[u][c[u]] = 1;
for (int i = 0; i < e[u].size(); i++) {
int v = e[u][i];
if (v == p) continue;
dfs(v, u);
long long o[] = {dp[u][0], dp[u][1]};
dp[u][1] =
o[1] * dp[v][0] % mod + o[0] * dp[v][1] % mod + o[1] * dp[v][1] % mod;
dp[u][1] %= mod;
dp[u][0] = o[0] * dp[v][0] % mod + o[0] * dp[v][1] % mod;
dp[u][0] %= mod;
}
}
int main() {
while (cin >> n) {
clr();
for (int i = 1; i < n; i++) {
int x;
cin >> x;
add(i, x);
}
for (int i = 0; i < n; i++) {
cin >> c[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;
long long dp[100007][2];
int c[100007], n;
vector<int> e[100007];
const long long mod = 1000000007;
void clr() {
for (int i = 0; i < 100007; i++) {
e[i].clear();
}
memset(dp, 0, sizeof(dp));
}
void add(int u, int v) {
e[u].push_back(v);
e[v].push_back(u);
}
void dfs(int u, int p) {
dp[u][c[u]] = 1;
for (int i = 0; i < e[u].size(); i++) {
int v = e[u][i];
if (v == p) continue;
dfs(v, u);
long long o[] = {dp[u][0], dp[u][1]};
dp[u][1] =
o[1] * dp[v][0] % mod + o[0] * dp[v][1] % mod + o[1] * dp[v][1] % mod;
dp[u][1] %= mod;
dp[u][0] = o[0] * dp[v][0] % mod + o[0] * dp[v][1] % mod;
dp[u][0] %= mod;
}
}
int main() {
while (cin >> n) {
clr();
for (int i = 1; i < n; i++) {
int x;
cin >> x;
add(i, x);
}
for (int i = 0; i < n; i++) {
cin >> c[i];
}
dfs(0, -1);
cout << dp[0][1];
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 1;
long long mod = 1e9 + 7;
vector<long long> adj[N];
long long col[N];
long long dp[N][2];
void dfs(long long src, long long par) {
dp[src][0] = !col[src];
dp[src][1] = col[src];
for (auto &x : adj[src]) {
if (x == par) continue;
dfs(x, src);
dp[src][1] =
dp[src][1] * dp[x][1] + dp[src][1] * dp[x][0] + dp[src][0] * dp[x][1];
dp[src][1] %= mod;
dp[src][0] = dp[src][0] * dp[x][0] + dp[src][0] * dp[x][1];
dp[src][0] %= mod;
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long n;
cin >> n;
for (long long i = 2; i <= n; i++) {
long long p;
cin >> p;
p++;
adj[i].push_back(p);
adj[p].push_back(i);
}
for (long long i = 1; i <= n; i++) {
cin >> col[i];
}
dfs(1, -1);
cout << dp[1][1];
}
|
### 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 long long N = 1e5 + 1;
long long mod = 1e9 + 7;
vector<long long> adj[N];
long long col[N];
long long dp[N][2];
void dfs(long long src, long long par) {
dp[src][0] = !col[src];
dp[src][1] = col[src];
for (auto &x : adj[src]) {
if (x == par) continue;
dfs(x, src);
dp[src][1] =
dp[src][1] * dp[x][1] + dp[src][1] * dp[x][0] + dp[src][0] * dp[x][1];
dp[src][1] %= mod;
dp[src][0] = dp[src][0] * dp[x][0] + dp[src][0] * dp[x][1];
dp[src][0] %= mod;
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long n;
cin >> n;
for (long long i = 2; i <= n; i++) {
long long p;
cin >> p;
p++;
adj[i].push_back(p);
adj[p].push_back(i);
}
for (long long i = 1; i <= n; i++) {
cin >> col[i];
}
dfs(1, -1);
cout << dp[1][1];
}
```
|
#include <bits/stdc++.h>
using namespace std;
inline long long in() {
int32_t x;
scanf("%d", &x);
return x;
}
inline string getStr() {
char ch[1000000];
scanf("%s", ch);
return ch;
}
inline char getCh() {
char ch;
scanf(" %c", &ch);
return ch;
}
const long long INF = 1e18;
const long long MOD = 1e9 + 7;
const long long MAX_N = 1e5 + 5;
struct edge {
long long v, u, w;
edge() {}
edge(long long v, long long u, long long w) : v(v), u(u), w(w) {}
};
edge e[MAX_N];
bool b[MAX_N];
vector<long long> graph[MAX_N];
long long dp[2][MAX_N];
inline void dfs(long long node, long long pr) {
dp[1][node] = b[node], dp[0][node] = b[node] ^ 1;
for (long long i = 0; i < (long long)graph[node].size(); i++) {
long long D0 = dp[0][node], D1 = dp[1][node];
long long u = graph[node][i];
if (u == pr) continue;
dp[0][node] = dp[1][node] = 0;
dfs(u, node);
dp[0][node] += (1LL * dp[0][u] * D0 + 1LL * dp[1][u] * D0) % MOD;
dp[1][node] +=
(1LL * dp[1][u] * D0 + 1LL * dp[1][u] * D1 + 1LL * dp[0][u] * D1) % MOD;
}
}
int32_t main() {
long long n = in();
for (long long i = 0; i < n - 1; i++) {
e[i].v = in(), e[i].u = i + 1;
graph[e[i].v].push_back(e[i].u);
graph[e[i].u].push_back(e[i].v);
}
for (long long i = 0; i < n; i++) {
b[i] = in();
}
dfs(1, -1);
cout << dp[1][1] << 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;
inline long long in() {
int32_t x;
scanf("%d", &x);
return x;
}
inline string getStr() {
char ch[1000000];
scanf("%s", ch);
return ch;
}
inline char getCh() {
char ch;
scanf(" %c", &ch);
return ch;
}
const long long INF = 1e18;
const long long MOD = 1e9 + 7;
const long long MAX_N = 1e5 + 5;
struct edge {
long long v, u, w;
edge() {}
edge(long long v, long long u, long long w) : v(v), u(u), w(w) {}
};
edge e[MAX_N];
bool b[MAX_N];
vector<long long> graph[MAX_N];
long long dp[2][MAX_N];
inline void dfs(long long node, long long pr) {
dp[1][node] = b[node], dp[0][node] = b[node] ^ 1;
for (long long i = 0; i < (long long)graph[node].size(); i++) {
long long D0 = dp[0][node], D1 = dp[1][node];
long long u = graph[node][i];
if (u == pr) continue;
dp[0][node] = dp[1][node] = 0;
dfs(u, node);
dp[0][node] += (1LL * dp[0][u] * D0 + 1LL * dp[1][u] * D0) % MOD;
dp[1][node] +=
(1LL * dp[1][u] * D0 + 1LL * dp[1][u] * D1 + 1LL * dp[0][u] * D1) % MOD;
}
}
int32_t main() {
long long n = in();
for (long long i = 0; i < n - 1; i++) {
e[i].v = in(), e[i].u = i + 1;
graph[e[i].v].push_back(e[i].u);
graph[e[i].u].push_back(e[i].v);
}
for (long long i = 0; i < n; i++) {
b[i] = in();
}
dfs(1, -1);
cout << dp[1][1] << endl;
}
```
|
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("unroll-loops")
using namespace std;
const long double PI = acos(-1);
const long long MOD = 1000000007;
const long long FMOD = 998244353;
const long double eps = 1e-9;
mt19937 RNG(chrono::steady_clock::now().time_since_epoch().count());
vector<long long> v[100005];
long long c[100005];
long long dp[2][100005];
void dfs(long long src, long long par) {
long long zero = 1 - c[src];
long long one = c[src];
for (auto x : v[src]) {
if (x != par) {
dfs(x, src);
one = (one * dp[0][x] + one * dp[1][x] + zero * dp[1][x]) % MOD;
zero = (zero * dp[0][x] + zero * dp[1][x]) % MOD;
}
}
dp[0][src] = zero;
dp[1][src] = one;
}
signed main() {
long long n;
cin >> n;
for (long long i = 0; i < n - 1; i++) {
long long a;
cin >> a;
v[a].push_back(i + 1);
v[i + 1].push_back(a);
}
for (long long i = 0; i < n; i++) cin >> c[i];
dfs(0, -1);
cout << dp[1][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>
#pragma GCC optimize("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("unroll-loops")
using namespace std;
const long double PI = acos(-1);
const long long MOD = 1000000007;
const long long FMOD = 998244353;
const long double eps = 1e-9;
mt19937 RNG(chrono::steady_clock::now().time_since_epoch().count());
vector<long long> v[100005];
long long c[100005];
long long dp[2][100005];
void dfs(long long src, long long par) {
long long zero = 1 - c[src];
long long one = c[src];
for (auto x : v[src]) {
if (x != par) {
dfs(x, src);
one = (one * dp[0][x] + one * dp[1][x] + zero * dp[1][x]) % MOD;
zero = (zero * dp[0][x] + zero * dp[1][x]) % MOD;
}
}
dp[0][src] = zero;
dp[1][src] = one;
}
signed main() {
long long n;
cin >> n;
for (long long i = 0; i < n - 1; i++) {
long long a;
cin >> a;
v[a].push_back(i + 1);
v[i + 1].push_back(a);
}
for (long long i = 0; i < n; i++) cin >> c[i];
dfs(0, -1);
cout << dp[1][0];
}
```
|
#include <bits/stdc++.h>
enum { M = 1000000007 };
int main() {
int n;
scanf("%d", &n);
std::vector<std::vector<int>> G(n);
std::vector<int> C(n);
std::vector<long long> S(n), B(n);
for (int i = (1); i <= (n - 1); ++i) {
int a;
scanf("%d", &a);
G[a].push_back(i);
}
for (int &c : C) scanf("%d", &c);
long long res = 1;
for (int i = (n - 1); i >= (0); --i) {
if (C[i]) {
for (int j : G[i]) res = res * S[j] % M;
S[i] = 1;
B[i] = 1;
} else {
int d = G[i].size();
std::vector<long long> a(d + 1), b(d + 1);
a[0] = b[0] = 1;
for (int j = 0; j < (d); ++j) {
a[j + 1] = a[j] * S[G[i][j]] % M;
b[j + 1] = b[j] * S[G[i][d - j - 1]] % M;
}
for (int j = 0; j < (d); ++j)
B[i] += a[j] * B[G[i][j]] % M * b[d - j - 1] % M;
B[i] %= M;
S[i] = (a.back() + B[i]) % M;
}
}
res = res * B[0] % M;
printf("%d\n", int(res));
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>
enum { M = 1000000007 };
int main() {
int n;
scanf("%d", &n);
std::vector<std::vector<int>> G(n);
std::vector<int> C(n);
std::vector<long long> S(n), B(n);
for (int i = (1); i <= (n - 1); ++i) {
int a;
scanf("%d", &a);
G[a].push_back(i);
}
for (int &c : C) scanf("%d", &c);
long long res = 1;
for (int i = (n - 1); i >= (0); --i) {
if (C[i]) {
for (int j : G[i]) res = res * S[j] % M;
S[i] = 1;
B[i] = 1;
} else {
int d = G[i].size();
std::vector<long long> a(d + 1), b(d + 1);
a[0] = b[0] = 1;
for (int j = 0; j < (d); ++j) {
a[j + 1] = a[j] * S[G[i][j]] % M;
b[j + 1] = b[j] * S[G[i][d - j - 1]] % M;
}
for (int j = 0; j < (d); ++j)
B[i] += a[j] * B[G[i][j]] % M * b[d - j - 1] % M;
B[i] %= M;
S[i] = (a.back() + B[i]) % M;
}
}
res = res * B[0] % M;
printf("%d\n", int(res));
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int sz = 100005;
int n;
vector<int> T[sz];
vector<int> col;
const int MOD = 1000000007;
long long P[sz][2];
void dfs(int u, int prev) {
if (col[u] == 1)
P[u][1] = 1, P[u][0] = 0;
else
P[u][1] = 0, P[u][0] = 1;
for (int i = 0; i < T[u].size(); i++) {
int v = T[u][i];
if (v == prev) continue;
dfs(v, u);
P[u][1] *= (P[v][0] + P[v][1]);
P[u][1] %= MOD;
P[u][1] += P[u][0] * P[v][1];
P[u][1] %= MOD;
P[u][0] = P[u][0] * (P[v][0] + P[v][1]);
P[u][0] %= MOD;
}
}
int main() {
memset(P, 0, sizeof P);
scanf("%d", &n);
for (int i = 0; i < n - 1; i++) {
int x;
scanf("%d", &x);
T[i + 1].push_back(x);
T[x].push_back(i + 1);
}
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
col.push_back(x);
}
dfs(0, -1);
cout << P[0][1] << "\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;
const int sz = 100005;
int n;
vector<int> T[sz];
vector<int> col;
const int MOD = 1000000007;
long long P[sz][2];
void dfs(int u, int prev) {
if (col[u] == 1)
P[u][1] = 1, P[u][0] = 0;
else
P[u][1] = 0, P[u][0] = 1;
for (int i = 0; i < T[u].size(); i++) {
int v = T[u][i];
if (v == prev) continue;
dfs(v, u);
P[u][1] *= (P[v][0] + P[v][1]);
P[u][1] %= MOD;
P[u][1] += P[u][0] * P[v][1];
P[u][1] %= MOD;
P[u][0] = P[u][0] * (P[v][0] + P[v][1]);
P[u][0] %= MOD;
}
}
int main() {
memset(P, 0, sizeof P);
scanf("%d", &n);
for (int i = 0; i < n - 1; i++) {
int x;
scanf("%d", &x);
T[i + 1].push_back(x);
T[x].push_back(i + 1);
}
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
col.push_back(x);
}
dfs(0, -1);
cout << P[0][1] << "\n";
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long M = 1e9 + 7;
const long long N = 1e5 + 7;
long long n;
vector<long long> kid[N];
bool col[N];
deque<long long> muls[N], mulr[N];
long long dp[N][2];
void solve(long long v) {
if (!kid[v].size()) {
if (col[v])
dp[v][1] = 1;
else
dp[v][0] = 1;
return;
}
for (auto u : kid[v]) solve(u);
for (long long i = 0; i < kid[v].size(); i++) {
long long u = kid[v][i];
long long s = dp[u][0] + dp[u][1];
if (i == 0)
muls[v].push_back(s);
else
muls[v].push_back((s * muls[v].back()) % M);
}
for (long long i = kid[v].size() - 1; i >= 0; i--) {
long long u = kid[v][i];
long long s = dp[u][0] + dp[u][1];
if (i == kid[v].size() - 1)
mulr[v].push_front(s);
else
mulr[v].push_front((s * mulr[v][0]) % M);
}
if (col[v]) {
dp[v][1] = mulr[v][0];
return;
}
dp[v][0] = mulr[v][0];
for (long long i = 0; i < kid[v].size(); i++) {
long long u = kid[v][i];
long long s = dp[u][1];
if (i) s = (s * muls[v][i - 1]) % M;
if (i + 1 < kid[v].size()) s = (s * mulr[v][i + 1]) % M;
dp[v][1] = (dp[v][1] + s) % M;
}
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (long long i = 2; i <= n; i++) {
long long p;
cin >> p;
p++;
kid[p].push_back(i);
}
for (long long i = 1; i <= n; i++) cin >> col[i];
solve(1);
cout << dp[1][1];
}
|
### 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;
long long M = 1e9 + 7;
const long long N = 1e5 + 7;
long long n;
vector<long long> kid[N];
bool col[N];
deque<long long> muls[N], mulr[N];
long long dp[N][2];
void solve(long long v) {
if (!kid[v].size()) {
if (col[v])
dp[v][1] = 1;
else
dp[v][0] = 1;
return;
}
for (auto u : kid[v]) solve(u);
for (long long i = 0; i < kid[v].size(); i++) {
long long u = kid[v][i];
long long s = dp[u][0] + dp[u][1];
if (i == 0)
muls[v].push_back(s);
else
muls[v].push_back((s * muls[v].back()) % M);
}
for (long long i = kid[v].size() - 1; i >= 0; i--) {
long long u = kid[v][i];
long long s = dp[u][0] + dp[u][1];
if (i == kid[v].size() - 1)
mulr[v].push_front(s);
else
mulr[v].push_front((s * mulr[v][0]) % M);
}
if (col[v]) {
dp[v][1] = mulr[v][0];
return;
}
dp[v][0] = mulr[v][0];
for (long long i = 0; i < kid[v].size(); i++) {
long long u = kid[v][i];
long long s = dp[u][1];
if (i) s = (s * muls[v][i - 1]) % M;
if (i + 1 < kid[v].size()) s = (s * mulr[v][i + 1]) % M;
dp[v][1] = (dp[v][1] + s) % M;
}
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (long long i = 2; i <= n; i++) {
long long p;
cin >> p;
p++;
kid[p].push_back(i);
}
for (long long i = 1; i <= n; i++) cin >> col[i];
solve(1);
cout << dp[1][1];
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000 * 100 + 5;
const int MOD = 1000 * 1000 * 1000 + 7;
int n;
vector<int> adj[MAXN];
bool mark[MAXN];
bool black[MAXN];
long long dp[MAXN][2];
void dfs(int v) {
mark[v] = true;
vector<long long> a, b;
for (int i = 0; i < adj[v].size(); i++) {
int u = adj[v][i];
if (mark[u]) continue;
dfs(u);
a.push_back(dp[u][0]);
b.push_back(dp[u][1]);
}
if (a.size() == 0) {
if (black[v]) {
dp[v][1] = 1;
dp[v][0] = 1;
} else {
dp[v][1] = 0;
dp[v][0] = 1;
}
return;
}
vector<long long> t1, t2;
t1 = a;
for (int i = 1; i < t1.size(); i++) t1[i] = (t1[i] * t1[i - 1]) % MOD;
t2 = a;
for (int i = (int)t2.size() - 2; i >= 0; i--)
t2[i] = (t2[i] * t2[i + 1]) % MOD;
if (black[v]) {
dp[v][1] = t1[t1.size() - 1];
dp[v][0] = dp[v][1];
} else {
dp[v][0] = t1[t1.size() - 1];
for (int i = 0; i < a.size(); i++) {
long long add = 1;
if (i > 0) add = (add * t1[i - 1]) % MOD;
if (i < a.size() - 1) add = (add * t2[i + 1]) % MOD;
add = (add * b[i]) % MOD;
dp[v][1] = (dp[v][1] + add) % MOD;
}
dp[v][0] += dp[v][1];
}
}
int main() {
cin >> n;
for (int i = 1; i <= n - 1; i++) {
int x;
cin >> x;
adj[x].push_back(i);
adj[i].push_back(x);
}
for (int i = 0; i < n; i++) cin >> black[i];
dfs(0);
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 MAXN = 1000 * 100 + 5;
const int MOD = 1000 * 1000 * 1000 + 7;
int n;
vector<int> adj[MAXN];
bool mark[MAXN];
bool black[MAXN];
long long dp[MAXN][2];
void dfs(int v) {
mark[v] = true;
vector<long long> a, b;
for (int i = 0; i < adj[v].size(); i++) {
int u = adj[v][i];
if (mark[u]) continue;
dfs(u);
a.push_back(dp[u][0]);
b.push_back(dp[u][1]);
}
if (a.size() == 0) {
if (black[v]) {
dp[v][1] = 1;
dp[v][0] = 1;
} else {
dp[v][1] = 0;
dp[v][0] = 1;
}
return;
}
vector<long long> t1, t2;
t1 = a;
for (int i = 1; i < t1.size(); i++) t1[i] = (t1[i] * t1[i - 1]) % MOD;
t2 = a;
for (int i = (int)t2.size() - 2; i >= 0; i--)
t2[i] = (t2[i] * t2[i + 1]) % MOD;
if (black[v]) {
dp[v][1] = t1[t1.size() - 1];
dp[v][0] = dp[v][1];
} else {
dp[v][0] = t1[t1.size() - 1];
for (int i = 0; i < a.size(); i++) {
long long add = 1;
if (i > 0) add = (add * t1[i - 1]) % MOD;
if (i < a.size() - 1) add = (add * t2[i + 1]) % MOD;
add = (add * b[i]) % MOD;
dp[v][1] = (dp[v][1] + add) % MOD;
}
dp[v][0] += dp[v][1];
}
}
int main() {
cin >> n;
for (int i = 1; i <= n - 1; i++) {
int x;
cin >> x;
adj[x].push_back(i);
adj[i].push_back(x);
}
for (int i = 0; i < n; i++) cin >> black[i];
dfs(0);
cout << dp[0][1] << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int mod = (int)1e9 + 7;
void dfs(int v, int p, list<int>* adj, int* c, long long** dp) {
if (c[v] == 1) {
dp[0][v] = 0;
dp[1][v] = 1;
} else {
dp[0][v] = 1;
dp[1][v] = 0;
}
for (auto i : adj[v]) {
if (i == p) continue;
dfs(i, v, adj, c, dp);
long long zero = dp[0][v], one = dp[1][v];
dp[0][v] = zero * dp[0][i];
dp[0][v] %= mod;
dp[1][v] = (one * dp[0][i]) % mod + (zero * dp[1][i]) % mod;
dp[1][v] %= mod;
dp[0][v] += zero * dp[1][i];
dp[0][v] %= mod;
dp[1][v] += one * dp[1][i];
dp[1][v] %= mod;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
list<int>* adj = new list<int>[n];
for (int i = 1; i < n; i++) {
int a;
cin >> a;
adj[i].push_back(a);
adj[a].push_back(i);
}
int* c = new int[n];
for (int i = 0; i < n; i++) {
cin >> c[i];
}
long long** dp = new long long*[2];
dp[0] = new long long[n]();
dp[1] = new long long[n]();
dfs(0, -1, adj, c, dp);
cout << dp[1][0] << 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;
int mod = (int)1e9 + 7;
void dfs(int v, int p, list<int>* adj, int* c, long long** dp) {
if (c[v] == 1) {
dp[0][v] = 0;
dp[1][v] = 1;
} else {
dp[0][v] = 1;
dp[1][v] = 0;
}
for (auto i : adj[v]) {
if (i == p) continue;
dfs(i, v, adj, c, dp);
long long zero = dp[0][v], one = dp[1][v];
dp[0][v] = zero * dp[0][i];
dp[0][v] %= mod;
dp[1][v] = (one * dp[0][i]) % mod + (zero * dp[1][i]) % mod;
dp[1][v] %= mod;
dp[0][v] += zero * dp[1][i];
dp[0][v] %= mod;
dp[1][v] += one * dp[1][i];
dp[1][v] %= mod;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
list<int>* adj = new list<int>[n];
for (int i = 1; i < n; i++) {
int a;
cin >> a;
adj[i].push_back(a);
adj[a].push_back(i);
}
int* c = new int[n];
for (int i = 0; i < n; i++) {
cin >> c[i];
}
long long** dp = new long long*[2];
dp[0] = new long long[n]();
dp[1] = new long long[n]();
dfs(0, -1, adj, c, dp);
cout << dp[1][0] << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int pr[1000007];
bool isnotprime(long long x) {
if (x == 1) return 1;
if (x == 2) return 0;
if (x % 2 == 0) return 1;
return (pr[x / 64] & (1 << ((x >> 1) & 31)));
}
void sieve(long long n) {
for (long long i = 3; i * i <= n; i += 2) {
if (!isnotprime(i))
for (long long j = i * i, k = i << 1; j < n; j += k)
pr[j / 64] |= (1 << ((j >> 1) & 31));
}
}
long long fpow(long long x, long long y) {
x = x % 1000000007;
long long res = 1;
while (y) {
if (y & 1) res = res * x;
res %= 1000000007;
y = y >> 1;
x = x * x;
x %= 1000000007;
}
return res;
}
long long inv(long long a, long long m) {
long long c = m;
long long y = 0, x = 1;
if (m == 1) return 0;
while (a > 1) {
long long q = a / m;
long long t = m;
m = a % m, a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0) x += c;
return x;
}
bool visited[100005];
vector<long long> adj[100005];
long long col[100005];
long long dp[100005][2];
void dfs(long long v) {
visited[v] = true;
dp[v][1] = col[v];
dp[v][0] = 1 - col[v];
for (auto u : adj[v]) {
if (visited[u])
continue;
else {
dfs(u);
long long zero = dp[v][0];
long long one = dp[v][1];
dp[v][0] = 0;
dp[v][1] = 0;
dp[v][1] +=
((zero * dp[u][1]) % 1000000007 + (one * dp[u][0]) % 1000000007) %
1000000007;
dp[v][1] %= 1000000007;
dp[v][0] += (zero * dp[u][0]) % 1000000007;
dp[v][0] %= 1000000007;
dp[v][0] += (zero * dp[u][1]) % 1000000007;
dp[v][0] %= 1000000007;
dp[v][1] += (one * dp[u][1]) % 1000000007;
dp[v][1] %= 1000000007;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
for (int i = 0; i < n - 1; i++) {
long long x;
cin >> x;
adj[i + 1].push_back(x);
adj[x].push_back(i + 1);
}
for (int i = 0; i < n; i++) cin >> col[i];
dfs(0);
cout << dp[0][1];
}
|
### 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 pr[1000007];
bool isnotprime(long long x) {
if (x == 1) return 1;
if (x == 2) return 0;
if (x % 2 == 0) return 1;
return (pr[x / 64] & (1 << ((x >> 1) & 31)));
}
void sieve(long long n) {
for (long long i = 3; i * i <= n; i += 2) {
if (!isnotprime(i))
for (long long j = i * i, k = i << 1; j < n; j += k)
pr[j / 64] |= (1 << ((j >> 1) & 31));
}
}
long long fpow(long long x, long long y) {
x = x % 1000000007;
long long res = 1;
while (y) {
if (y & 1) res = res * x;
res %= 1000000007;
y = y >> 1;
x = x * x;
x %= 1000000007;
}
return res;
}
long long inv(long long a, long long m) {
long long c = m;
long long y = 0, x = 1;
if (m == 1) return 0;
while (a > 1) {
long long q = a / m;
long long t = m;
m = a % m, a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0) x += c;
return x;
}
bool visited[100005];
vector<long long> adj[100005];
long long col[100005];
long long dp[100005][2];
void dfs(long long v) {
visited[v] = true;
dp[v][1] = col[v];
dp[v][0] = 1 - col[v];
for (auto u : adj[v]) {
if (visited[u])
continue;
else {
dfs(u);
long long zero = dp[v][0];
long long one = dp[v][1];
dp[v][0] = 0;
dp[v][1] = 0;
dp[v][1] +=
((zero * dp[u][1]) % 1000000007 + (one * dp[u][0]) % 1000000007) %
1000000007;
dp[v][1] %= 1000000007;
dp[v][0] += (zero * dp[u][0]) % 1000000007;
dp[v][0] %= 1000000007;
dp[v][0] += (zero * dp[u][1]) % 1000000007;
dp[v][0] %= 1000000007;
dp[v][1] += (one * dp[u][1]) % 1000000007;
dp[v][1] %= 1000000007;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
for (int i = 0; i < n - 1; i++) {
long long x;
cin >> x;
adj[i + 1].push_back(x);
adj[x].push_back(i + 1);
}
for (int i = 0; i < n; i++) cin >> col[i];
dfs(0);
cout << dp[0][1];
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > al;
long long dp[100011][4];
int x[100011];
long long p = 1000 * 1000 * 1000 + 7;
void DFS(int v) {
dp[v][0] = 1;
dp[v][1] = 0;
for (int i = 0; i < al[v].size(); i++) {
int u = al[v][i];
DFS(u);
dp[v][1] = (dp[v][1] * dp[u][0]) % p;
dp[v][1] = (dp[v][1] + (dp[v][0] * dp[u][1]) % p) % p;
dp[v][0] = (dp[v][0] * dp[u][0]) % p;
}
if (x[v] == 1)
dp[v][1] = dp[v][0];
else
dp[v][0] = (dp[v][0] + dp[v][1]) % p;
}
int main() {
memset(dp, -1, sizeof dp);
int n;
scanf("%d", &n);
al.assign(n + 5, vector<int>());
for (int i = 0; i < n - 1; i++) {
int t;
scanf("%d", &t);
al[t].push_back(i + 1);
}
for (int i = 0; i < n; i++) scanf("%d", &x[i]);
DFS(0);
printf("%d\n", dp[0][1]);
}
|
### 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<vector<int> > al;
long long dp[100011][4];
int x[100011];
long long p = 1000 * 1000 * 1000 + 7;
void DFS(int v) {
dp[v][0] = 1;
dp[v][1] = 0;
for (int i = 0; i < al[v].size(); i++) {
int u = al[v][i];
DFS(u);
dp[v][1] = (dp[v][1] * dp[u][0]) % p;
dp[v][1] = (dp[v][1] + (dp[v][0] * dp[u][1]) % p) % p;
dp[v][0] = (dp[v][0] * dp[u][0]) % p;
}
if (x[v] == 1)
dp[v][1] = dp[v][0];
else
dp[v][0] = (dp[v][0] + dp[v][1]) % p;
}
int main() {
memset(dp, -1, sizeof dp);
int n;
scanf("%d", &n);
al.assign(n + 5, vector<int>());
for (int i = 0; i < n - 1; i++) {
int t;
scanf("%d", &t);
al[t].push_back(i + 1);
}
for (int i = 0; i < n; i++) scanf("%d", &x[i]);
DFS(0);
printf("%d\n", dp[0][1]);
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long int maxN = 1e5;
long long int dp1[maxN], dp2[maxN], d[maxN], n, mod = 1e9 + 7;
vector<long long int> g[maxN];
void dfs(long long int v = 1, long long int p = 0) {
dp1[v] = 1, dp2[v] = 0;
for (long long int i = 0; i < g[v].size(); i++) {
long long int u = g[v][i];
if (p - u) {
dfs(u, v);
dp2[v] = dp2[v] * dp1[u] % mod;
dp2[v] = (dp2[v] + dp1[v] * dp2[u]) % mod;
dp1[v] = dp1[v] * dp1[u];
dp1[v] %= mod;
}
}
if (d[v])
dp2[v] = dp1[v];
else
dp1[v] += dp2[v];
}
int main() {
cin >> n;
for (long long int i = 2; i <= n; i++) {
long long int x;
cin >> x;
g[++x].push_back(i);
g[i].push_back(x);
}
for (long long int i = 1; i <= n; i++) cin >> d[i];
dfs();
cout << dp2[1] << 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 long long int maxN = 1e5;
long long int dp1[maxN], dp2[maxN], d[maxN], n, mod = 1e9 + 7;
vector<long long int> g[maxN];
void dfs(long long int v = 1, long long int p = 0) {
dp1[v] = 1, dp2[v] = 0;
for (long long int i = 0; i < g[v].size(); i++) {
long long int u = g[v][i];
if (p - u) {
dfs(u, v);
dp2[v] = dp2[v] * dp1[u] % mod;
dp2[v] = (dp2[v] + dp1[v] * dp2[u]) % mod;
dp1[v] = dp1[v] * dp1[u];
dp1[v] %= mod;
}
}
if (d[v])
dp2[v] = dp1[v];
else
dp1[v] += dp2[v];
}
int main() {
cin >> n;
for (long long int i = 2; i <= n; i++) {
long long int x;
cin >> x;
g[++x].push_back(i);
g[i].push_back(x);
}
for (long long int i = 1; i <= n; i++) cin >> d[i];
dfs();
cout << dp2[1] << endl;
;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int LIM = 1e+5 + 10;
const int MOD = 1e+9 + 7;
int color[LIM];
long long cnt_1[LIM];
long long cnt_0[LIM];
int used[LIM];
vector<vector<int> > g(LIM, vector<int>());
void dfs(int v) {
used[v] = true;
if (color[v] == 1) {
cnt_1[v] = 1;
cnt_0[v] = 0;
} else {
cnt_1[v] = 0;
cnt_0[v] = 1;
}
for (int i = 0; i < (int)g[v].size(); i++) {
int to = g[v][i];
if (used[to]) continue;
dfs(to);
long long t1 = 0;
long long t0 = 0;
t1 += cnt_1[v] * cnt_1[to];
t0 += cnt_0[v] * cnt_1[to];
t1 += cnt_1[v] * cnt_0[to];
t1 += cnt_0[v] * cnt_1[to];
t0 += cnt_0[v] * cnt_0[to];
cnt_0[v] = t0 % MOD;
cnt_1[v] = t1 % MOD;
}
}
int main() {
int n;
cin >> n;
int v = 0;
for (int u = 1; u < n; u++) {
cin >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 0; i < n; i++) cin >> color[i];
for (int i = 0; i < n; i++)
if (!used[i]) {
dfs(i);
}
cout << cnt_1[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 int LIM = 1e+5 + 10;
const int MOD = 1e+9 + 7;
int color[LIM];
long long cnt_1[LIM];
long long cnt_0[LIM];
int used[LIM];
vector<vector<int> > g(LIM, vector<int>());
void dfs(int v) {
used[v] = true;
if (color[v] == 1) {
cnt_1[v] = 1;
cnt_0[v] = 0;
} else {
cnt_1[v] = 0;
cnt_0[v] = 1;
}
for (int i = 0; i < (int)g[v].size(); i++) {
int to = g[v][i];
if (used[to]) continue;
dfs(to);
long long t1 = 0;
long long t0 = 0;
t1 += cnt_1[v] * cnt_1[to];
t0 += cnt_0[v] * cnt_1[to];
t1 += cnt_1[v] * cnt_0[to];
t1 += cnt_0[v] * cnt_1[to];
t0 += cnt_0[v] * cnt_0[to];
cnt_0[v] = t0 % MOD;
cnt_1[v] = t1 % MOD;
}
}
int main() {
int n;
cin >> n;
int v = 0;
for (int u = 1; u < n; u++) {
cin >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 0; i < n; i++) cin >> color[i];
for (int i = 0; i < n; i++)
if (!used[i]) {
dfs(i);
}
cout << cnt_1[0];
return 0;
}
```
|
#include <bits/stdc++.h>
#pragma GCC optimize("-O2")
using namespace std;
void err(istream_iterator<string> it) { cerr << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << "\t";
err(++it, args...);
}
template <typename T1, typename T2>
ostream& operator<<(ostream& c, pair<T1, T2>& v) {
c << "(" << v.first << "," << v.second << ")";
return c;
}
template <template <class...> class TT, class... T>
ostream& operator<<(ostream& out, TT<T...>& c) {
out << "{ ";
for (auto& x : c) out << x << " ";
out << "}";
return out;
}
const long long int MOD = 1e9 + 7, LIM = 1e5 + 5;
const long double EPS = 1e-9;
int n;
vector<int> adj[LIM];
bool visited[LIM];
long long int dp[LIM][2];
int x[LIM];
long long int dfs(int v) {
dp[v][0] = 1;
dp[v][1] = 0;
for (auto& r : adj[v]) {
if (visited[r]) continue;
visited[r] = 1;
dfs(r);
dp[v][1] *= dp[r][0];
dp[v][1] += (dp[v][0] * dp[r][1]);
dp[v][1] %= MOD;
dp[v][0] *= dp[r][0];
dp[v][0] %= MOD;
}
if (x[v])
dp[v][1] = dp[v][0];
else
dp[v][0] += dp[v][1];
if (dp[v][0] > MOD) dp[v][0] -= MOD;
return dp[v][1];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 0; i < n - 1; i++) {
int x;
cin >> x;
adj[i + 1].push_back(x);
adj[x].push_back(i + 1);
}
visited[0] = 1;
for (int i = 0; i < n; i++) cin >> x[i];
cout << dfs(0) << 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>
#pragma GCC optimize("-O2")
using namespace std;
void err(istream_iterator<string> it) { cerr << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << "\t";
err(++it, args...);
}
template <typename T1, typename T2>
ostream& operator<<(ostream& c, pair<T1, T2>& v) {
c << "(" << v.first << "," << v.second << ")";
return c;
}
template <template <class...> class TT, class... T>
ostream& operator<<(ostream& out, TT<T...>& c) {
out << "{ ";
for (auto& x : c) out << x << " ";
out << "}";
return out;
}
const long long int MOD = 1e9 + 7, LIM = 1e5 + 5;
const long double EPS = 1e-9;
int n;
vector<int> adj[LIM];
bool visited[LIM];
long long int dp[LIM][2];
int x[LIM];
long long int dfs(int v) {
dp[v][0] = 1;
dp[v][1] = 0;
for (auto& r : adj[v]) {
if (visited[r]) continue;
visited[r] = 1;
dfs(r);
dp[v][1] *= dp[r][0];
dp[v][1] += (dp[v][0] * dp[r][1]);
dp[v][1] %= MOD;
dp[v][0] *= dp[r][0];
dp[v][0] %= MOD;
}
if (x[v])
dp[v][1] = dp[v][0];
else
dp[v][0] += dp[v][1];
if (dp[v][0] > MOD) dp[v][0] -= MOD;
return dp[v][1];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 0; i < n - 1; i++) {
int x;
cin >> x;
adj[i + 1].push_back(x);
adj[x].push_back(i + 1);
}
visited[0] = 1;
for (int i = 0; i < n; i++) cin >> x[i];
cout << dfs(0) << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
struct Edge {
int v;
Edge *next;
};
int n, p[100000], x[100000], f[100000], g[100000];
vector<int> ed[100000];
int ExEuler(int a, int b, int c) {
c = c < 0 ? c + (-(c + 1) / b + 1) * b : c % b;
return a ? ((long long)ExEuler(b % a, a, -c) * b + c) / a : 0;
}
int Div(int a, int b) { return ExEuler(b, mod, a); }
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) scanf("%d", &p[i]), ed[p[i]].push_back(i);
for (int i = 0; i < n; i++) scanf("%d", &x[i]);
for (int i = n - 1; i >= 0; i--) {
if (x[i]) {
f[i] = 1;
for (int j = 0; j < ed[i].size(); j++)
f[i] = (long long)f[i] * (g[ed[i][j]] + f[ed[i][j]]) % mod;
} else {
g[i] = 1;
for (int j = 0; j < ed[i].size(); j++)
g[i] = (long long)g[i] * (g[ed[i][j]] + f[ed[i][j]]) % mod;
for (int j = 0; j < ed[i].size(); j++)
f[i] = ((long long)Div(g[i], g[ed[i][j]] + f[ed[i][j]]) * f[ed[i][j]] +
f[i]) %
mod;
}
}
printf("%d\n", f[0]);
getchar();
getchar();
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 mod = 1000000007;
struct Edge {
int v;
Edge *next;
};
int n, p[100000], x[100000], f[100000], g[100000];
vector<int> ed[100000];
int ExEuler(int a, int b, int c) {
c = c < 0 ? c + (-(c + 1) / b + 1) * b : c % b;
return a ? ((long long)ExEuler(b % a, a, -c) * b + c) / a : 0;
}
int Div(int a, int b) { return ExEuler(b, mod, a); }
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) scanf("%d", &p[i]), ed[p[i]].push_back(i);
for (int i = 0; i < n; i++) scanf("%d", &x[i]);
for (int i = n - 1; i >= 0; i--) {
if (x[i]) {
f[i] = 1;
for (int j = 0; j < ed[i].size(); j++)
f[i] = (long long)f[i] * (g[ed[i][j]] + f[ed[i][j]]) % mod;
} else {
g[i] = 1;
for (int j = 0; j < ed[i].size(); j++)
g[i] = (long long)g[i] * (g[ed[i][j]] + f[ed[i][j]]) % mod;
for (int j = 0; j < ed[i].size(); j++)
f[i] = ((long long)Div(g[i], g[ed[i][j]] + f[ed[i][j]]) * f[ed[i][j]] +
f[i]) %
mod;
}
}
printf("%d\n", f[0]);
getchar();
getchar();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int const M = 1000000007, N = 150000;
long long dp[N][2];
vector<int> E[N];
int C[N];
void dfs(int i, int rt) {
vector<int> son;
int j;
for (j = 0; j < E[i].size(); j++)
if (E[i][j] != rt) {
dfs(E[i][j], i);
son.push_back(E[i][j]);
}
if (C[i]) {
dp[i][0] = 0;
dp[i][1] = 1;
for (j = 0; j < son.size(); j++)
dp[i][1] = dp[i][1] * (dp[son[j]][0] + dp[son[j]][1]) % M;
} else {
long long sum = 1;
vector<long long> suml(son.size()), sumr(son.size());
for (j = 0; j < son.size(); j++) {
sum = sum * (dp[son[j]][0] + dp[son[j]][1]) % M;
suml[j] = sum;
}
dp[i][0] = sum;
sum = 1;
for (j = son.size() - 1; j >= 0; j--) {
sum = sum * (dp[son[j]][0] + dp[son[j]][1]) % M;
sumr[j] = sum;
}
for (j = 0; j < son.size(); j++)
dp[i][1] = (dp[i][1] + (j > 0 ? suml[j - 1] : 1) *
(j < son.size() - 1 ? sumr[j + 1] : 1) % M *
dp[son[j]][1] % M) %
M;
}
}
int main() {
int n, i, j;
cin >> n;
for (i = 1; i < n; i++) {
cin >> j;
E[j].push_back(i);
E[i].push_back(j);
}
for (i = 0; i < n; i++) cin >> C[i];
dfs(0, -1);
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;
int const M = 1000000007, N = 150000;
long long dp[N][2];
vector<int> E[N];
int C[N];
void dfs(int i, int rt) {
vector<int> son;
int j;
for (j = 0; j < E[i].size(); j++)
if (E[i][j] != rt) {
dfs(E[i][j], i);
son.push_back(E[i][j]);
}
if (C[i]) {
dp[i][0] = 0;
dp[i][1] = 1;
for (j = 0; j < son.size(); j++)
dp[i][1] = dp[i][1] * (dp[son[j]][0] + dp[son[j]][1]) % M;
} else {
long long sum = 1;
vector<long long> suml(son.size()), sumr(son.size());
for (j = 0; j < son.size(); j++) {
sum = sum * (dp[son[j]][0] + dp[son[j]][1]) % M;
suml[j] = sum;
}
dp[i][0] = sum;
sum = 1;
for (j = son.size() - 1; j >= 0; j--) {
sum = sum * (dp[son[j]][0] + dp[son[j]][1]) % M;
sumr[j] = sum;
}
for (j = 0; j < son.size(); j++)
dp[i][1] = (dp[i][1] + (j > 0 ? suml[j - 1] : 1) *
(j < son.size() - 1 ? sumr[j + 1] : 1) % M *
dp[son[j]][1] % M) %
M;
}
}
int main() {
int n, i, j;
cin >> n;
for (i = 1; i < n; i++) {
cin >> j;
E[j].push_back(i);
E[i].push_back(j);
}
for (i = 0; i < n; i++) cin >> C[i];
dfs(0, -1);
cout << dp[0][1] << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
long long n, dp[100005][2];
std::set<int> ch[100005];
bool c[100005];
void init(int h = 0) {
for (std::set<int>::iterator it = ch[h].begin(); it != ch[h].end(); ++it) {
ch[*it].erase(h);
init(*it);
}
}
void process(int h = 0) {
int sz = ch[h].size();
if (sz == 0) {
dp[h][1] = c[h];
dp[h][0] = !c[h];
return;
}
for (std::set<int>::iterator it = ch[h].begin(); it != ch[h].end(); ++it) {
process(*it);
}
dp[h][1] = c[h];
dp[h][0] = !c[h];
for (std::set<int>::iterator it = ch[h].begin(); it != ch[h].end(); ++it) {
int j = *it;
long long temp1 = dp[h][1];
long long temp0 = dp[h][0];
dp[h][1] = 0;
dp[h][0] = 0;
if (dp[j][1] > 0) {
dp[h][1] += temp1 * dp[j][1];
dp[h][0] += temp0 * dp[j][1];
}
if (dp[j][0] > 0) {
dp[h][1] += temp1 * dp[j][0];
dp[h][0] += temp0 * dp[j][0];
}
if (dp[j][1] > 0) {
dp[h][1] += temp0 * dp[j][1];
}
dp[h][1] %= MOD;
dp[h][0] %= MOD;
}
}
int main() {
std::cin >> n;
for (int k = 0; k < n - 1; ++k) {
int x;
std::cin >> x;
ch[x].insert(k + 1);
ch[k + 1].insert(x);
}
for (int k = 0; k < n; ++k) {
std::cin >> c[k];
}
init();
process();
std::cout << dp[0][1] << std::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;
const long long MOD = 1000000007;
long long n, dp[100005][2];
std::set<int> ch[100005];
bool c[100005];
void init(int h = 0) {
for (std::set<int>::iterator it = ch[h].begin(); it != ch[h].end(); ++it) {
ch[*it].erase(h);
init(*it);
}
}
void process(int h = 0) {
int sz = ch[h].size();
if (sz == 0) {
dp[h][1] = c[h];
dp[h][0] = !c[h];
return;
}
for (std::set<int>::iterator it = ch[h].begin(); it != ch[h].end(); ++it) {
process(*it);
}
dp[h][1] = c[h];
dp[h][0] = !c[h];
for (std::set<int>::iterator it = ch[h].begin(); it != ch[h].end(); ++it) {
int j = *it;
long long temp1 = dp[h][1];
long long temp0 = dp[h][0];
dp[h][1] = 0;
dp[h][0] = 0;
if (dp[j][1] > 0) {
dp[h][1] += temp1 * dp[j][1];
dp[h][0] += temp0 * dp[j][1];
}
if (dp[j][0] > 0) {
dp[h][1] += temp1 * dp[j][0];
dp[h][0] += temp0 * dp[j][0];
}
if (dp[j][1] > 0) {
dp[h][1] += temp0 * dp[j][1];
}
dp[h][1] %= MOD;
dp[h][0] %= MOD;
}
}
int main() {
std::cin >> n;
for (int k = 0; k < n - 1; ++k) {
int x;
std::cin >> x;
ch[x].insert(k + 1);
ch[k + 1].insert(x);
}
for (int k = 0; k < n; ++k) {
std::cin >> c[k];
}
init();
process();
std::cout << dp[0][1] << std::endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
int ver[100005];
vector<int> ed[100005];
int ans = 1;
int inv(int b) {
return (b == 1) ? 1 : 1ll * inv(mod % b) * (mod - mod / b) % mod;
}
void go(int wh, int p, int& adot, int& non) {
int i1;
int dottmp, nontmp;
vector<int> vdot, vnon;
non = 1;
for (i1 = 0; i1 < ed[wh].size(); i1++) {
if (ed[wh][i1] != p) {
go(ed[wh][i1], wh, dottmp, nontmp);
vdot.push_back(dottmp);
vnon.push_back(nontmp);
non = 1ll * non * nontmp % mod;
}
}
adot = 0;
for (i1 = vdot.size() - 1; i1 >= 0; i1--) {
adot = (adot + 1ll * non * vdot[i1] % mod * inv(vnon[i1])) % mod;
}
if (ver[wh] == 1) {
ans = 1ll * ans * non % mod;
adot = 1;
non = 1;
} else {
non = (non + adot) % mod;
}
}
int main() {
int i1;
int n;
int u;
int dottmp, nontmp;
scanf("%d", &n);
for (i1 = 1; i1 < n; i1++) {
scanf("%d", &u);
ed[u].push_back(i1);
ed[i1].push_back(u);
}
for (i1 = 0; i1 < n; i1++) {
scanf("%d", &ver[i1]);
}
go(n - 1, -1, dottmp, nontmp);
ans = ans * dottmp % mod;
printf("%d\n", ans);
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 = 1000000007;
int ver[100005];
vector<int> ed[100005];
int ans = 1;
int inv(int b) {
return (b == 1) ? 1 : 1ll * inv(mod % b) * (mod - mod / b) % mod;
}
void go(int wh, int p, int& adot, int& non) {
int i1;
int dottmp, nontmp;
vector<int> vdot, vnon;
non = 1;
for (i1 = 0; i1 < ed[wh].size(); i1++) {
if (ed[wh][i1] != p) {
go(ed[wh][i1], wh, dottmp, nontmp);
vdot.push_back(dottmp);
vnon.push_back(nontmp);
non = 1ll * non * nontmp % mod;
}
}
adot = 0;
for (i1 = vdot.size() - 1; i1 >= 0; i1--) {
adot = (adot + 1ll * non * vdot[i1] % mod * inv(vnon[i1])) % mod;
}
if (ver[wh] == 1) {
ans = 1ll * ans * non % mod;
adot = 1;
non = 1;
} else {
non = (non + adot) % mod;
}
}
int main() {
int i1;
int n;
int u;
int dottmp, nontmp;
scanf("%d", &n);
for (i1 = 1; i1 < n; i1++) {
scanf("%d", &u);
ed[u].push_back(i1);
ed[i1].push_back(u);
}
for (i1 = 0; i1 < n; i1++) {
scanf("%d", &ver[i1]);
}
go(n - 1, -1, dottmp, nontmp);
ans = ans * dottmp % mod;
printf("%d\n", ans);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 100001;
const int mod = 1e9 + 7;
int n;
vector<int> g[N];
int x[N];
long long dp[2][N];
void dfs(int u) {
if (x[u] == 0) {
dp[0][u] = 1;
dp[1][u] = 0;
} else {
dp[0][u] = 0;
dp[1][u] = 1;
}
for (int v : g[u]) {
dfs(v);
long long zero = dp[0][u];
long long one = dp[1][u];
dp[0][u] = dp[1][u] = 0;
dp[0][u] = (zero * dp[0][v]) % mod;
dp[1][u] = (zero * dp[1][v] + one * dp[0][v]) % mod;
dp[0][u] = (dp[0][u] + zero * dp[1][v]) % mod;
dp[1][u] = (dp[1][u] + one * dp[1][v]) % mod;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i < n; i++) {
int p;
cin >> p;
g[p].emplace_back(i);
}
for (int i = 0; i < n; i++) {
cin >> x[i];
}
dfs(0);
cout << dp[1][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 N = 100001;
const int mod = 1e9 + 7;
int n;
vector<int> g[N];
int x[N];
long long dp[2][N];
void dfs(int u) {
if (x[u] == 0) {
dp[0][u] = 1;
dp[1][u] = 0;
} else {
dp[0][u] = 0;
dp[1][u] = 1;
}
for (int v : g[u]) {
dfs(v);
long long zero = dp[0][u];
long long one = dp[1][u];
dp[0][u] = dp[1][u] = 0;
dp[0][u] = (zero * dp[0][v]) % mod;
dp[1][u] = (zero * dp[1][v] + one * dp[0][v]) % mod;
dp[0][u] = (dp[0][u] + zero * dp[1][v]) % mod;
dp[1][u] = (dp[1][u] + one * dp[1][v]) % mod;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i < n; i++) {
int p;
cin >> p;
g[p].emplace_back(i);
}
for (int i = 0; i < n; i++) {
cin >> x[i];
}
dfs(0);
cout << dp[1][0];
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
const int mod = 1e9 + 7;
long long dp[maxn][2];
vector<int> x[maxn];
int c[maxn];
void dfs(int v, int p) {
dp[v][0] = 1;
dp[v][1] = 0;
for (int i = (int)(0); i < (int)(x[v].size()); i++) {
int u = x[v][i];
if (u == p) continue;
dfs(u, v);
dp[v][1] =
((dp[v][1] * dp[u][0]) % mod + (dp[v][0] * dp[u][1]) % mod) % mod;
dp[v][0] = (dp[v][0] * dp[u][0]) % mod;
}
if (c[v])
dp[v][1] = dp[v][0];
else
dp[v][0] = (dp[v][0] + dp[v][1]) % mod;
}
int tmp, n;
int main() {
cin >> n;
for (int i = (int)(1); i < (int)(n); i++) {
cin >> tmp;
x[i].push_back(tmp);
x[tmp].push_back(i);
}
for (int i = (int)(0); i < (int)(n); i++) cin >> c[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 maxn = 1e5 + 10;
const int mod = 1e9 + 7;
long long dp[maxn][2];
vector<int> x[maxn];
int c[maxn];
void dfs(int v, int p) {
dp[v][0] = 1;
dp[v][1] = 0;
for (int i = (int)(0); i < (int)(x[v].size()); i++) {
int u = x[v][i];
if (u == p) continue;
dfs(u, v);
dp[v][1] =
((dp[v][1] * dp[u][0]) % mod + (dp[v][0] * dp[u][1]) % mod) % mod;
dp[v][0] = (dp[v][0] * dp[u][0]) % mod;
}
if (c[v])
dp[v][1] = dp[v][0];
else
dp[v][0] = (dp[v][0] + dp[v][1]) % mod;
}
int tmp, n;
int main() {
cin >> n;
for (int i = (int)(1); i < (int)(n); i++) {
cin >> tmp;
x[i].push_back(tmp);
x[tmp].push_back(i);
}
for (int i = (int)(0); i < (int)(n); i++) cin >> c[i];
dfs(0, -1);
cout << dp[0][1] << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int mod = 1000000007;
long long dp[100009][2];
int n, x;
vector<int> ed[100009];
int b[100009];
long long ans;
void dfs(int u, int fa) {
if (b[u])
dp[u][1] = 1;
else
dp[u][0] = 1;
for (int i = 0; i < ed[u].size(); i++) {
int v = ed[u][i];
if (v == fa) continue;
dfs(v, u);
dp[u][1] = (dp[u][1] * (dp[v][0] + dp[v][1]) + dp[u][0] * dp[v][1]) % mod;
dp[u][0] = (dp[u][0] * (dp[v][0] + dp[v][1])) % mod;
}
}
int main() {
ans = 1;
cin >> n;
for (int i = 1; i < n; i++) {
cin >> x;
ed[x].push_back(i);
ed[i].push_back(x);
}
for (int i = 0; i < n; i++) cin >> b[i];
dfs(0, -1);
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;
int mod = 1000000007;
long long dp[100009][2];
int n, x;
vector<int> ed[100009];
int b[100009];
long long ans;
void dfs(int u, int fa) {
if (b[u])
dp[u][1] = 1;
else
dp[u][0] = 1;
for (int i = 0; i < ed[u].size(); i++) {
int v = ed[u][i];
if (v == fa) continue;
dfs(v, u);
dp[u][1] = (dp[u][1] * (dp[v][0] + dp[v][1]) + dp[u][0] * dp[v][1]) % mod;
dp[u][0] = (dp[u][0] * (dp[v][0] + dp[v][1])) % mod;
}
}
int main() {
ans = 1;
cin >> n;
for (int i = 1; i < n; i++) {
cin >> x;
ed[x].push_back(i);
ed[i].push_back(x);
}
for (int i = 0; i < n; i++) cin >> b[i];
dfs(0, -1);
cout << dp[0][1] << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<int> G[100005];
long long dp[100005][2];
int c[100005];
long long ksm(long long a, long long b) {
long long c = 1, d = a;
while (b) {
if (b & 1) c = (c * d) % 1000000007;
d = (d * d) % 1000000007;
b >>= 1;
}
return c;
}
void dfs(int x) {
int i, len, v;
len = G[x].size();
dp[x][0] = dp[x][1] = 1;
for (i = 0; i < len; i++) {
v = G[x][i];
dfs(v);
if (c[x] == 0)
dp[x][0] = dp[x][0] * (dp[v][0] + dp[v][1]) % 1000000007;
else
dp[x][1] = dp[x][1] * (dp[v][0] + dp[v][1]) % 1000000007;
}
if (c[x] == 1)
dp[x][0] = 0;
else {
dp[x][1] = 0;
for (i = 0; i < len; i++) {
v = G[x][i];
dp[x][1] = (dp[x][1] + (dp[x][0] * dp[v][1] % 1000000007) *
ksm(dp[v][0] + dp[v][1], 1000000007 - 2)) %
1000000007;
}
}
}
int main() {
int n, i, x;
cin >> n;
for (i = 2; i <= n; i++) {
scanf("%d", &x);
G[x + 1].push_back(i);
}
for (i = 1; i <= n; i++) scanf("%d", &c[i]);
dfs(1);
cout << dp[1][1] % 1000000007 << 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;
vector<int> G[100005];
long long dp[100005][2];
int c[100005];
long long ksm(long long a, long long b) {
long long c = 1, d = a;
while (b) {
if (b & 1) c = (c * d) % 1000000007;
d = (d * d) % 1000000007;
b >>= 1;
}
return c;
}
void dfs(int x) {
int i, len, v;
len = G[x].size();
dp[x][0] = dp[x][1] = 1;
for (i = 0; i < len; i++) {
v = G[x][i];
dfs(v);
if (c[x] == 0)
dp[x][0] = dp[x][0] * (dp[v][0] + dp[v][1]) % 1000000007;
else
dp[x][1] = dp[x][1] * (dp[v][0] + dp[v][1]) % 1000000007;
}
if (c[x] == 1)
dp[x][0] = 0;
else {
dp[x][1] = 0;
for (i = 0; i < len; i++) {
v = G[x][i];
dp[x][1] = (dp[x][1] + (dp[x][0] * dp[v][1] % 1000000007) *
ksm(dp[v][0] + dp[v][1], 1000000007 - 2)) %
1000000007;
}
}
}
int main() {
int n, i, x;
cin >> n;
for (i = 2; i <= n; i++) {
scanf("%d", &x);
G[x + 1].push_back(i);
}
for (i = 1; i <= n; i++) scanf("%d", &c[i]);
dfs(1);
cout << dp[1][1] % 1000000007 << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long int N = 1e5 + 1;
const long long int mod = 1e9 + 7;
vector<vector<long long int>> graph(N);
vector<long long int> color(N);
vector<long long int> white(N, 0), black(N, 0);
void dfs1(long long int node, long long int parent) {
white[node] = (!color[node]);
black[node] = color[node];
for (auto i : graph[node]) {
if (i == parent) continue;
dfs1(i, node);
long long int zero = white[i];
long long int one = black[i];
black[node] = ((black[node] * zero) % mod + (black[node] * one) % mod +
(white[node] * one) % mod) %
mod;
white[node] =
((white[node] * zero) % mod + (white[node] * one) % mod) % mod;
}
}
int main() {
long long int n;
cin >> n;
for (long long int i = 1; i < n; i++) {
long long int a;
cin >> a;
graph[i].push_back(a);
graph[a].push_back(i);
}
for (long long int i = 0; i < n; cin >> color[i++])
;
dfs1(0, 0);
cout << black[0] << 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 int N = 1e5 + 1;
const long long int mod = 1e9 + 7;
vector<vector<long long int>> graph(N);
vector<long long int> color(N);
vector<long long int> white(N, 0), black(N, 0);
void dfs1(long long int node, long long int parent) {
white[node] = (!color[node]);
black[node] = color[node];
for (auto i : graph[node]) {
if (i == parent) continue;
dfs1(i, node);
long long int zero = white[i];
long long int one = black[i];
black[node] = ((black[node] * zero) % mod + (black[node] * one) % mod +
(white[node] * one) % mod) %
mod;
white[node] =
((white[node] * zero) % mod + (white[node] * one) % mod) % mod;
}
}
int main() {
long long int n;
cin >> n;
for (long long int i = 1; i < n; i++) {
long long int a;
cin >> a;
graph[i].push_back(a);
graph[a].push_back(i);
}
for (long long int i = 0; i < n; cin >> color[i++])
;
dfs1(0, 0);
cout << black[0] << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int M = 1000000007;
const int MAXN = 100105;
int n;
long long dp[MAXN][2];
int isBlack[MAXN];
vector<int> g[MAXN];
void dfs(int u) {
dp[u][1] = 0;
dp[u][0] = 1;
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
dfs(v);
dp[u][1] = ((dp[u][1] * dp[v][0]) % M + (dp[u][0] * dp[v][1]) % M) % M;
dp[u][0] = (dp[u][0] * dp[v][0]) % M;
}
if (isBlack[u] == 1) {
dp[u][1] = dp[u][0];
} else {
dp[u][0] = (dp[u][0] + dp[u][1]) % M;
}
}
int main() {
while (cin >> n) {
for (int i = 0; i < n; i++) {
g[i].clear();
}
memset(dp, 0, sizeof(dp));
for (int i = 1; i < n; i++) {
int u;
scanf("%d", &u);
g[u].push_back(i);
}
for (int i = 0; i < n; i++) {
scanf("%d", &isBlack[i]);
}
dfs(0);
printf("%I64d\n", dp[0][1]);
}
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 M = 1000000007;
const int MAXN = 100105;
int n;
long long dp[MAXN][2];
int isBlack[MAXN];
vector<int> g[MAXN];
void dfs(int u) {
dp[u][1] = 0;
dp[u][0] = 1;
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
dfs(v);
dp[u][1] = ((dp[u][1] * dp[v][0]) % M + (dp[u][0] * dp[v][1]) % M) % M;
dp[u][0] = (dp[u][0] * dp[v][0]) % M;
}
if (isBlack[u] == 1) {
dp[u][1] = dp[u][0];
} else {
dp[u][0] = (dp[u][0] + dp[u][1]) % M;
}
}
int main() {
while (cin >> n) {
for (int i = 0; i < n; i++) {
g[i].clear();
}
memset(dp, 0, sizeof(dp));
for (int i = 1; i < n; i++) {
int u;
scanf("%d", &u);
g[u].push_back(i);
}
for (int i = 0; i < n; i++) {
scanf("%d", &isBlack[i]);
}
dfs(0);
printf("%I64d\n", dp[0][1]);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <class T1>
void deb(T1 e1) {
cout << e1 << endl;
}
template <class T1, class T2>
void deb(T1 e1, T2 e2) {
cout << e1 << " " << e2 << endl;
}
template <class T1, class T2, class T3>
void deb(T1 e1, T2 e2, T3 e3) {
cout << e1 << " " << e2 << " " << e3 << endl;
}
template <class T1, class T2, class T3, class T4>
void deb(T1 e1, T2 e2, T3 e3, T4 e4) {
cout << e1 << " " << e2 << " " << e3 << " " << e4 << endl;
}
template <class T1, class T2, class T3, class T4, class T5>
void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) {
cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << endl;
}
template <class T1, class T2, class T3, class T4, class T5, class T6>
void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) {
cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << " " << e6
<< endl;
}
int n;
bool flag[100000 + 7];
long long int dp[100000 + 7][3];
vector<int> adj[100000 + 7];
void rec(int u, int pre) {
int v, i, j, k;
long long int one, zero;
dp[u][0] = flag[u] == 0;
dp[u][1] = flag[u] == 1;
for (i = 0; i < adj[u].size(); i++) {
v = adj[u][i];
if (v == pre) continue;
zero = dp[u][0];
one = dp[u][1];
dp[u][0] = dp[u][1] = 0;
rec(v, u);
dp[u][0] = (zero * dp[v][1]) % 1000000007;
dp[u][1] = (one * dp[v][1]) % 1000000007;
dp[u][0] += (zero * dp[v][0]) % 1000000007;
dp[u][0] %= 1000000007;
dp[u][1] += (one * dp[v][0]) % 1000000007;
dp[u][1] %= 1000000007;
dp[u][1] += (zero * dp[v][1]) % 1000000007;
dp[u][1] %= 1000000007;
}
}
int main() {
int u, c, i, j, k;
scanf("%d", &n);
for (i = 1; i < n; i++) {
scanf("%d", &u);
adj[u].push_back(i);
adj[i].push_back(u);
}
for (i = 0; i < n; i++) {
scanf("%d", &u);
flag[i] = u;
}
rec(1, -1);
printf("%lld\n", dp[1][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;
template <class T1>
void deb(T1 e1) {
cout << e1 << endl;
}
template <class T1, class T2>
void deb(T1 e1, T2 e2) {
cout << e1 << " " << e2 << endl;
}
template <class T1, class T2, class T3>
void deb(T1 e1, T2 e2, T3 e3) {
cout << e1 << " " << e2 << " " << e3 << endl;
}
template <class T1, class T2, class T3, class T4>
void deb(T1 e1, T2 e2, T3 e3, T4 e4) {
cout << e1 << " " << e2 << " " << e3 << " " << e4 << endl;
}
template <class T1, class T2, class T3, class T4, class T5>
void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) {
cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << endl;
}
template <class T1, class T2, class T3, class T4, class T5, class T6>
void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) {
cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << " " << e6
<< endl;
}
int n;
bool flag[100000 + 7];
long long int dp[100000 + 7][3];
vector<int> adj[100000 + 7];
void rec(int u, int pre) {
int v, i, j, k;
long long int one, zero;
dp[u][0] = flag[u] == 0;
dp[u][1] = flag[u] == 1;
for (i = 0; i < adj[u].size(); i++) {
v = adj[u][i];
if (v == pre) continue;
zero = dp[u][0];
one = dp[u][1];
dp[u][0] = dp[u][1] = 0;
rec(v, u);
dp[u][0] = (zero * dp[v][1]) % 1000000007;
dp[u][1] = (one * dp[v][1]) % 1000000007;
dp[u][0] += (zero * dp[v][0]) % 1000000007;
dp[u][0] %= 1000000007;
dp[u][1] += (one * dp[v][0]) % 1000000007;
dp[u][1] %= 1000000007;
dp[u][1] += (zero * dp[v][1]) % 1000000007;
dp[u][1] %= 1000000007;
}
}
int main() {
int u, c, i, j, k;
scanf("%d", &n);
for (i = 1; i < n; i++) {
scanf("%d", &u);
adj[u].push_back(i);
adj[i].push_back(u);
}
for (i = 0; i < n; i++) {
scanf("%d", &u);
flag[i] = u;
}
rec(1, -1);
printf("%lld\n", dp[1][1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 1e5 + 1e3;
const long long Mod = 1e9 + 7;
vector<int> al[Maxn];
int bad[Maxn], dp[Maxn][2];
long long pw(long long a, long long b) {
if (!b) return 1;
return (pw((a * a) % Mod, b / 2) * (b % 2 ? a : 1)) % Mod;
}
long long dfs(int v, int p = -1, int state = 1) {
if (dp[v][state] != -1) return dp[v][state];
if (bad[v]) {
if (!state) return 0;
long long ret = 1;
for (int i = (0); i < ((int)al[v].size()); ++i) {
int u = al[v][i];
if (u == p) continue;
ret *= (dfs(u, v, 0) + dfs(u, v, 1));
ret %= Mod;
}
return dp[v][state] = ret;
}
long long ret = 1;
for (int i = (0); i < ((int)al[v].size()); ++i) {
int u = al[v][i];
if (u == p) continue;
ret *= (dfs(u, v, 1) + dfs(u, v, 0));
ret %= Mod;
}
if (!state) return dp[v][state] = ret;
long long res = 0;
for (int i = (0); i < ((int)al[v].size()); ++i) {
int u = al[v][i];
if (u == p) continue;
long long div = dfs(u, v, 1) + dfs(u, v, 0);
div %= Mod;
res += (((ret * pw(div, Mod - 2)) % Mod) * dfs(u, v, 1)) % Mod;
res %= Mod;
}
return dp[v][state] = res;
}
int main() {
int n;
cin >> n;
for (int i = (0); i < ((int)n - 1); ++i) {
int now;
cin >> now;
al[now].push_back(i + 1);
al[i + 1].push_back(now);
}
for (int i = (0); i < ((int)n); ++i) {
cin >> bad[i];
}
memset(dp, -1, sizeof dp);
cout << dfs(0) << 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 Maxn = 1e5 + 1e3;
const long long Mod = 1e9 + 7;
vector<int> al[Maxn];
int bad[Maxn], dp[Maxn][2];
long long pw(long long a, long long b) {
if (!b) return 1;
return (pw((a * a) % Mod, b / 2) * (b % 2 ? a : 1)) % Mod;
}
long long dfs(int v, int p = -1, int state = 1) {
if (dp[v][state] != -1) return dp[v][state];
if (bad[v]) {
if (!state) return 0;
long long ret = 1;
for (int i = (0); i < ((int)al[v].size()); ++i) {
int u = al[v][i];
if (u == p) continue;
ret *= (dfs(u, v, 0) + dfs(u, v, 1));
ret %= Mod;
}
return dp[v][state] = ret;
}
long long ret = 1;
for (int i = (0); i < ((int)al[v].size()); ++i) {
int u = al[v][i];
if (u == p) continue;
ret *= (dfs(u, v, 1) + dfs(u, v, 0));
ret %= Mod;
}
if (!state) return dp[v][state] = ret;
long long res = 0;
for (int i = (0); i < ((int)al[v].size()); ++i) {
int u = al[v][i];
if (u == p) continue;
long long div = dfs(u, v, 1) + dfs(u, v, 0);
div %= Mod;
res += (((ret * pw(div, Mod - 2)) % Mod) * dfs(u, v, 1)) % Mod;
res %= Mod;
}
return dp[v][state] = res;
}
int main() {
int n;
cin >> n;
for (int i = (0); i < ((int)n - 1); ++i) {
int now;
cin >> now;
al[now].push_back(i + 1);
al[i + 1].push_back(now);
}
for (int i = (0); i < ((int)n); ++i) {
cin >> bad[i];
}
memset(dp, -1, sizeof dp);
cout << dfs(0) << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e9 + 7;
vector<long long> v[112345];
long long n, p[112345], f[112345][2];
bool vis[112345];
void dfs(long long x) {
vis[x] = 1;
for (long long i = 0; i < v[x].size(); i++) {
long long dir = v[x][i];
if (vis[dir]) continue;
dfs(dir);
f[x][1] =
(f[x][1] * (f[dir][1] + f[dir][0]) % N + f[x][0] * f[dir][1] % N) % N;
f[x][0] = (f[x][0] * (f[dir][1] + f[dir][0])) % N;
}
vis[x] = 0;
}
signed main() {
scanf("%lld", &n);
long long u;
for (long long i = 1; i < n; i++) {
scanf("%lld", &u);
v[i].push_back(u);
v[u].push_back(i);
}
for (long long i = 0; i < n; i++) {
scanf("%lld", &p[i]);
f[i][p[i]] = 1;
}
dfs(0);
printf("%lld", f[0][1] % 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 N = 1e9 + 7;
vector<long long> v[112345];
long long n, p[112345], f[112345][2];
bool vis[112345];
void dfs(long long x) {
vis[x] = 1;
for (long long i = 0; i < v[x].size(); i++) {
long long dir = v[x][i];
if (vis[dir]) continue;
dfs(dir);
f[x][1] =
(f[x][1] * (f[dir][1] + f[dir][0]) % N + f[x][0] * f[dir][1] % N) % N;
f[x][0] = (f[x][0] * (f[dir][1] + f[dir][0])) % N;
}
vis[x] = 0;
}
signed main() {
scanf("%lld", &n);
long long u;
for (long long i = 1; i < n; i++) {
scanf("%lld", &u);
v[i].push_back(u);
v[u].push_back(i);
}
for (long long i = 0; i < n; i++) {
scanf("%lld", &p[i]);
f[i][p[i]] = 1;
}
dfs(0);
printf("%lld", f[0][1] % N);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, mod = 1e9 + 7;
int head[N], nxt[N << 1], to[N << 1], cnt;
int n, col[N];
long long dp[N][2];
void init() {
memset(head, -1, sizeof(head));
cnt = 0;
}
void addEdge(int u, int v) {
nxt[cnt] = head[u];
to[cnt] = v;
head[u] = cnt++;
}
void dfs(int u, int pre) {
dp[u][0] = 1;
dp[u][1] = 0;
for (int i = head[u]; ~i; i = nxt[i]) {
int v = to[i];
if (v == pre) continue;
dfs(v, u);
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 (col[u])
dp[u][1] = dp[u][0];
else
dp[u][0] = (dp[u][0] + dp[u][1]) % mod;
}
int main() {
while (~scanf("%d", &n)) {
init();
for (int i = 0, j; i < n - 1; i++) {
scanf("%d", &j);
addEdge(j, i + 1);
addEdge(i + 1, j);
}
for (int i = 0; i < n; i++) scanf("%d", col + i);
dfs(0, -1);
printf("%I64d\n", 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 int N = 1e5 + 10, mod = 1e9 + 7;
int head[N], nxt[N << 1], to[N << 1], cnt;
int n, col[N];
long long dp[N][2];
void init() {
memset(head, -1, sizeof(head));
cnt = 0;
}
void addEdge(int u, int v) {
nxt[cnt] = head[u];
to[cnt] = v;
head[u] = cnt++;
}
void dfs(int u, int pre) {
dp[u][0] = 1;
dp[u][1] = 0;
for (int i = head[u]; ~i; i = nxt[i]) {
int v = to[i];
if (v == pre) continue;
dfs(v, u);
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 (col[u])
dp[u][1] = dp[u][0];
else
dp[u][0] = (dp[u][0] + dp[u][1]) % mod;
}
int main() {
while (~scanf("%d", &n)) {
init();
for (int i = 0, j; i < n - 1; i++) {
scanf("%d", &j);
addEdge(j, i + 1);
addEdge(i + 1, j);
}
for (int i = 0; i < n; i++) scanf("%d", col + i);
dfs(0, -1);
printf("%I64d\n", dp[0][1]);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
vector<int> child[100000];
int black[100000];
int dp[100000][2];
int pref[100000 + 1][2], suf[100000 + 1][2];
void calc(int v) {
const int nchild = (int)child[v].size();
for (int i = 0; i < nchild; i++) {
int v2 = child[v][i];
calc(v2);
}
for (int j = 0; j < 2; j++) {
pref[0][j] = 1;
suf[0][j] = 1;
for (int i = 0; i < nchild; i++) {
pref[i + 1][j] = (long long)dp[child[v][i]][j] * pref[i][j] % mod;
suf[i + 1][j] =
(long long)dp[child[v][nchild - 1 - i]][j] * suf[i][j] % mod;
}
}
{
const int bexp = 0;
int &ret = dp[v][bexp];
ret = 0;
if (black[v]) {
ret = (ret + pref[nchild][1]) % mod;
} else {
for (int i = 0; i < nchild; i++)
ret = (ret + (long long)dp[child[v][i]][0] * pref[i][1] % mod *
suf[nchild - i - 1][1] % mod) %
mod;
}
}
{
const int bexp = 1;
int &ret = dp[v][bexp];
ret = dp[v][0];
if (black[v]) {
} else {
ret = (ret + pref[nchild][1]) % mod;
}
}
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int p;
scanf("%d", &p);
child[p].push_back(i);
}
for (int i = 0; i < n; i++) scanf("%d", &black[i]);
calc(0);
printf("%d\n", dp[0][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 long long mod = 1000000007;
vector<int> child[100000];
int black[100000];
int dp[100000][2];
int pref[100000 + 1][2], suf[100000 + 1][2];
void calc(int v) {
const int nchild = (int)child[v].size();
for (int i = 0; i < nchild; i++) {
int v2 = child[v][i];
calc(v2);
}
for (int j = 0; j < 2; j++) {
pref[0][j] = 1;
suf[0][j] = 1;
for (int i = 0; i < nchild; i++) {
pref[i + 1][j] = (long long)dp[child[v][i]][j] * pref[i][j] % mod;
suf[i + 1][j] =
(long long)dp[child[v][nchild - 1 - i]][j] * suf[i][j] % mod;
}
}
{
const int bexp = 0;
int &ret = dp[v][bexp];
ret = 0;
if (black[v]) {
ret = (ret + pref[nchild][1]) % mod;
} else {
for (int i = 0; i < nchild; i++)
ret = (ret + (long long)dp[child[v][i]][0] * pref[i][1] % mod *
suf[nchild - i - 1][1] % mod) %
mod;
}
}
{
const int bexp = 1;
int &ret = dp[v][bexp];
ret = dp[v][0];
if (black[v]) {
} else {
ret = (ret + pref[nchild][1]) % mod;
}
}
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int p;
scanf("%d", &p);
child[p].push_back(i);
}
for (int i = 0; i < n; i++) scanf("%d", &black[i]);
calc(0);
printf("%d\n", dp[0][0]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int N;
vector<int> graph[100010];
long long dp0[100010], dp1[100010];
int color[100010];
int main(void) {
int i, j;
cin >> N;
for ((i) = 0; (i) < (int)(N - 1); (i)++) {
int p;
scanf("%d", &p);
graph[p].push_back(i + 1);
}
for ((i) = 0; (i) < (int)(N); (i)++) scanf("%d", &color[i]);
for (i = N - 1; i >= 0; i--) {
long long zero = 0, one = 0;
if (color[i] == 0)
zero++;
else
one++;
for ((j) = 0; (j) < (int)(graph[i].size()); (j)++) {
int v = graph[i][j];
long long zero2 = (zero * dp1[v] + zero * dp0[v]) % 1000000007ll;
long long one2 =
(one * dp1[v] + zero * dp1[v] + one * dp0[v]) % 1000000007ll;
zero = zero2;
one = one2;
}
dp0[i] = zero;
dp1[i] = one;
}
cout << dp1[0] << 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 N;
vector<int> graph[100010];
long long dp0[100010], dp1[100010];
int color[100010];
int main(void) {
int i, j;
cin >> N;
for ((i) = 0; (i) < (int)(N - 1); (i)++) {
int p;
scanf("%d", &p);
graph[p].push_back(i + 1);
}
for ((i) = 0; (i) < (int)(N); (i)++) scanf("%d", &color[i]);
for (i = N - 1; i >= 0; i--) {
long long zero = 0, one = 0;
if (color[i] == 0)
zero++;
else
one++;
for ((j) = 0; (j) < (int)(graph[i].size()); (j)++) {
int v = graph[i][j];
long long zero2 = (zero * dp1[v] + zero * dp0[v]) % 1000000007ll;
long long one2 =
(one * dp1[v] + zero * dp1[v] + one * dp0[v]) % 1000000007ll;
zero = zero2;
one = one2;
}
dp0[i] = zero;
dp1[i] = one;
}
cout << dp1[0] << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T _sq(T a) {
return a * a;
}
template <class T>
inline T _sqrt(T a) {
return (T)sqrt((double)a);
}
template <class T, class X>
inline T _pow(T a, X y) {
T z = 1;
for (int i = 1; i <= y; i++) {
z *= a;
}
return z;
}
template <class T>
inline T _gcd(T a, T b) {
a = abs(a);
b = abs(b);
if (!b) return a;
return _gcd(b, a % b);
}
template <class T>
inline T _lcm(T a, T b) {
a = abs(a);
b = abs(b);
return (a / _gcd(a, b)) * b;
}
template <class T>
inline T _extended(T a, T b, T &x, T &y) {
a = abs(a);
b = abs(b);
T g, x1, y1;
if (!b) {
x = 1;
y = 0;
g = a;
return g;
}
g = _extended(b, a % b, x1, y1);
x = y1;
y = x1 - (a / b) * y1;
return g;
}
template <class T, class X>
inline bool getbit(T a, X i) {
T t = 1;
return ((a & (t << i)) > 0);
}
template <class T, class X>
inline T setbit(T a, X i) {
T t = 1;
return (a | (t << i));
}
template <class T, class X>
inline T resetbit(T a, X i) {
T t = 1;
return (a & (~(t << i)));
}
template <class T, class X>
inline T togglebit(T a, X i) {
T t = 1;
return (a ^ (t << i));
}
template <class T>
void pv(T v) {
for (int i = 0; i < ((int)v.size()); i++) cout << v[i] << " ";
cout << endl;
}
template <class T, class X>
inline T _bigmod(T n, X m) {
unsigned long long ret = 1, a = n % 1000000007;
while (m) {
if (m & 1) ret = (ret * a) % 1000000007;
m >>= 1;
a = (a * a) % 1000000007;
}
ret %= 1000000007;
return (T)ret;
}
template <class T>
inline T _modinv(T n) {
return _bigmod(n, 1000000007 - 2);
}
int col[(100000 + 3)];
vector<int> adj[(100000 + 3)], edge[(100000 + 3)];
bool vis[(100000 + 3)];
void dfs(int u, int p) {
vis[u] = 1;
adj[p].push_back(u);
for (int i = 0; i < ((int)edge[u].size()); i++) {
if (!vis[edge[u][i]]) {
dfs(edge[u][i], u);
}
}
return;
}
long long dp[(100000 + 3)][2];
long long go(int n, int p, int b) {
if (p >= ((int)adj[n].size())) return !b;
long long &ret = dp[adj[n][p]][b];
if (ret != -1) return ret;
ret = 0;
if (!col[adj[n][p]] && b) {
ret = (ret + go(n, p + 1, 1) * go(adj[n][p], 0, 0)) % 1000000007;
ret = (ret + go(n, p + 1, 0) * go(adj[n][p], 0, 1)) % 1000000007;
} else if (col[adj[n][p]] == b) {
ret = (ret + go(n, p + 1, 0) * go(adj[n][p], 0, 0)) % 1000000007;
}
ret =
(ret + go(n, p + 1, b) * go(adj[n][p], 0, !col[adj[n][p]])) % 1000000007;
return ret;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for (int i = 0; i <= n - 2; i++) {
int x;
cin >> x;
edge[i + 1 + 1].push_back(x + 1);
edge[x + 1].push_back(i + 1 + 1);
}
for (int i = 1; i <= n; i++) {
cin >> col[i];
}
vis[0] = 1;
dfs(1, 0);
memset(dp, -1, sizeof dp);
cout << go(0, 0, 1) % 1000000007 << 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;
template <class T>
inline T _sq(T a) {
return a * a;
}
template <class T>
inline T _sqrt(T a) {
return (T)sqrt((double)a);
}
template <class T, class X>
inline T _pow(T a, X y) {
T z = 1;
for (int i = 1; i <= y; i++) {
z *= a;
}
return z;
}
template <class T>
inline T _gcd(T a, T b) {
a = abs(a);
b = abs(b);
if (!b) return a;
return _gcd(b, a % b);
}
template <class T>
inline T _lcm(T a, T b) {
a = abs(a);
b = abs(b);
return (a / _gcd(a, b)) * b;
}
template <class T>
inline T _extended(T a, T b, T &x, T &y) {
a = abs(a);
b = abs(b);
T g, x1, y1;
if (!b) {
x = 1;
y = 0;
g = a;
return g;
}
g = _extended(b, a % b, x1, y1);
x = y1;
y = x1 - (a / b) * y1;
return g;
}
template <class T, class X>
inline bool getbit(T a, X i) {
T t = 1;
return ((a & (t << i)) > 0);
}
template <class T, class X>
inline T setbit(T a, X i) {
T t = 1;
return (a | (t << i));
}
template <class T, class X>
inline T resetbit(T a, X i) {
T t = 1;
return (a & (~(t << i)));
}
template <class T, class X>
inline T togglebit(T a, X i) {
T t = 1;
return (a ^ (t << i));
}
template <class T>
void pv(T v) {
for (int i = 0; i < ((int)v.size()); i++) cout << v[i] << " ";
cout << endl;
}
template <class T, class X>
inline T _bigmod(T n, X m) {
unsigned long long ret = 1, a = n % 1000000007;
while (m) {
if (m & 1) ret = (ret * a) % 1000000007;
m >>= 1;
a = (a * a) % 1000000007;
}
ret %= 1000000007;
return (T)ret;
}
template <class T>
inline T _modinv(T n) {
return _bigmod(n, 1000000007 - 2);
}
int col[(100000 + 3)];
vector<int> adj[(100000 + 3)], edge[(100000 + 3)];
bool vis[(100000 + 3)];
void dfs(int u, int p) {
vis[u] = 1;
adj[p].push_back(u);
for (int i = 0; i < ((int)edge[u].size()); i++) {
if (!vis[edge[u][i]]) {
dfs(edge[u][i], u);
}
}
return;
}
long long dp[(100000 + 3)][2];
long long go(int n, int p, int b) {
if (p >= ((int)adj[n].size())) return !b;
long long &ret = dp[adj[n][p]][b];
if (ret != -1) return ret;
ret = 0;
if (!col[adj[n][p]] && b) {
ret = (ret + go(n, p + 1, 1) * go(adj[n][p], 0, 0)) % 1000000007;
ret = (ret + go(n, p + 1, 0) * go(adj[n][p], 0, 1)) % 1000000007;
} else if (col[adj[n][p]] == b) {
ret = (ret + go(n, p + 1, 0) * go(adj[n][p], 0, 0)) % 1000000007;
}
ret =
(ret + go(n, p + 1, b) * go(adj[n][p], 0, !col[adj[n][p]])) % 1000000007;
return ret;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for (int i = 0; i <= n - 2; i++) {
int x;
cin >> x;
edge[i + 1 + 1].push_back(x + 1);
edge[x + 1].push_back(i + 1 + 1);
}
for (int i = 1; i <= n; i++) {
cin >> col[i];
}
vis[0] = 1;
dfs(1, 0);
memset(dp, -1, sizeof dp);
cout << go(0, 0, 1) % 1000000007 << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[100005];
long long dp[100005][2];
int color[100005];
void dfs(int u) {
dp[u][0] = 1;
dp[u][1] = 0;
for (int v : adj[u]) {
dfs(v);
dp[u][1] *= dp[v][0];
dp[u][1] %= 1000000007LL;
dp[u][1] += dp[u][0] * dp[v][1] % 1000000007LL;
dp[u][1] %= 1000000007LL;
dp[u][0] *= dp[v][0];
dp[u][0] %= 1000000007LL;
}
if (color[u])
dp[u][1] = dp[u][0];
else {
dp[u][0] += dp[u][1];
dp[u][0] %= 1000000007LL;
}
}
int main() {
int n;
scanf("%d", &n);
for (int v = 1; v < n; v++) {
int u;
scanf("%d", &u);
adj[u].push_back(v);
}
for (int i = 0; i < n; i++) scanf("%d", color + 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> adj[100005];
long long dp[100005][2];
int color[100005];
void dfs(int u) {
dp[u][0] = 1;
dp[u][1] = 0;
for (int v : adj[u]) {
dfs(v);
dp[u][1] *= dp[v][0];
dp[u][1] %= 1000000007LL;
dp[u][1] += dp[u][0] * dp[v][1] % 1000000007LL;
dp[u][1] %= 1000000007LL;
dp[u][0] *= dp[v][0];
dp[u][0] %= 1000000007LL;
}
if (color[u])
dp[u][1] = dp[u][0];
else {
dp[u][0] += dp[u][1];
dp[u][0] %= 1000000007LL;
}
}
int main() {
int n;
scanf("%d", &n);
for (int v = 1; v < n; v++) {
int u;
scanf("%d", &u);
adj[u].push_back(v);
}
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 INF = 0x3f3f3f3f, MOD = 1e9 + 7;
const int n_ = 1e5 + 1000;
long long gcd(long long a, long long b) { return (a ? gcd(b % a, a) : b); }
long long power(long long a, long long n) {
long long p = 1;
while (n > 0) {
if (n % 2) {
p = p * a;
}
n >>= 1;
a *= a;
}
return p;
}
long long power(long long a, long long n, long long mod) {
long long p = 1;
while (n > 0) {
if (n % 2) {
p = p * a;
p %= mod;
}
n >>= 1;
a *= a;
a %= mod;
}
return p % mod;
}
int n, d[n_];
long long dp[n_][3];
vector<int> e[n_];
void dfs(int u) {
dp[u][0] = 1;
dp[u][1] = 0;
for (auto v : e[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) % MOD;
dp[u][0] *= dp[v][0];
dp[u][0] %= MOD;
}
if (d[u]) {
dp[u][1] = dp[u][0];
} else {
dp[u][0] += dp[u][1];
dp[u][0] %= MOD;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
scanf("%d", &n);
for (int(i) = 1; (i) <= (n - 1); (i)++) {
int p;
scanf("%d", &p);
e[p].push_back(i);
}
for (int(i) = 0; (i) < (n); (i)++) {
scanf("%d", d + i);
}
dfs(0);
printf("%lld\n", dp[0][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;
const int INF = 0x3f3f3f3f, MOD = 1e9 + 7;
const int n_ = 1e5 + 1000;
long long gcd(long long a, long long b) { return (a ? gcd(b % a, a) : b); }
long long power(long long a, long long n) {
long long p = 1;
while (n > 0) {
if (n % 2) {
p = p * a;
}
n >>= 1;
a *= a;
}
return p;
}
long long power(long long a, long long n, long long mod) {
long long p = 1;
while (n > 0) {
if (n % 2) {
p = p * a;
p %= mod;
}
n >>= 1;
a *= a;
a %= mod;
}
return p % mod;
}
int n, d[n_];
long long dp[n_][3];
vector<int> e[n_];
void dfs(int u) {
dp[u][0] = 1;
dp[u][1] = 0;
for (auto v : e[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) % MOD;
dp[u][0] *= dp[v][0];
dp[u][0] %= MOD;
}
if (d[u]) {
dp[u][1] = dp[u][0];
} else {
dp[u][0] += dp[u][1];
dp[u][0] %= MOD;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
scanf("%d", &n);
for (int(i) = 1; (i) <= (n - 1); (i)++) {
int p;
scanf("%d", &p);
e[p].push_back(i);
}
for (int(i) = 0; (i) < (n); (i)++) {
scanf("%d", d + i);
}
dfs(0);
printf("%lld\n", dp[0][1]);
return 0;
}
```
|
#include <bits/stdc++.h>
const int MAXN = 100000;
const int MAXM = 200000;
const int MOD = 1e9 + 7;
using namespace std;
inline void enableFileIO() {
freopen("test.in", "r", stdin);
freopen("test.out", "w", stdout);
}
inline int read() {
int x = 0, w = 1;
char c = ' ';
while (c < '0' || c > '9') {
c = getchar();
if (c == '-') w = -1;
}
while (c >= '0' && c <= '9') {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return x * w;
}
int tot, head[MAXN + 5], x[MAXN + 5], n;
long long dp[MAXN + 5][2];
struct Edge {
int to, nxt;
Edge() {}
Edge(int _to, int _nxt) : to(_to), nxt(_nxt) {}
} edge[MAXM + 5];
inline void add(int u, int v) {
edge[tot] = Edge(v, head[u]);
head[u] = tot++;
edge[tot] = Edge(u, head[v]);
head[v] = tot++;
}
void init() {
tot = 0;
memset(head, -1, sizeof(head));
n = read();
for (int i = 2; i <= n; ++i) {
int p = read();
add(i, p + 1);
}
for (int i = 1; i <= n; ++i) x[i] = read();
}
void dfs(int u, int fa) {
dp[u][x[u]] = 1;
for (int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].to;
if (v == fa) continue;
dfs(v, u);
if (x[u]) {
dp[u][1] = dp[u][1] * (dp[v][0] + dp[v][1]) % MOD;
} else {
dp[u][1] = (dp[u][1] * (dp[v][0] + dp[v][1]) + dp[u][0] * dp[v][1]) % MOD;
dp[u][0] = dp[u][0] * (dp[v][0] + dp[v][1]) % MOD;
}
}
}
int main() {
init();
dfs(1, 0);
printf("%I64d\n", dp[1][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>
const int MAXN = 100000;
const int MAXM = 200000;
const int MOD = 1e9 + 7;
using namespace std;
inline void enableFileIO() {
freopen("test.in", "r", stdin);
freopen("test.out", "w", stdout);
}
inline int read() {
int x = 0, w = 1;
char c = ' ';
while (c < '0' || c > '9') {
c = getchar();
if (c == '-') w = -1;
}
while (c >= '0' && c <= '9') {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return x * w;
}
int tot, head[MAXN + 5], x[MAXN + 5], n;
long long dp[MAXN + 5][2];
struct Edge {
int to, nxt;
Edge() {}
Edge(int _to, int _nxt) : to(_to), nxt(_nxt) {}
} edge[MAXM + 5];
inline void add(int u, int v) {
edge[tot] = Edge(v, head[u]);
head[u] = tot++;
edge[tot] = Edge(u, head[v]);
head[v] = tot++;
}
void init() {
tot = 0;
memset(head, -1, sizeof(head));
n = read();
for (int i = 2; i <= n; ++i) {
int p = read();
add(i, p + 1);
}
for (int i = 1; i <= n; ++i) x[i] = read();
}
void dfs(int u, int fa) {
dp[u][x[u]] = 1;
for (int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].to;
if (v == fa) continue;
dfs(v, u);
if (x[u]) {
dp[u][1] = dp[u][1] * (dp[v][0] + dp[v][1]) % MOD;
} else {
dp[u][1] = (dp[u][1] * (dp[v][0] + dp[v][1]) + dp[u][0] * dp[v][1]) % MOD;
dp[u][0] = dp[u][0] * (dp[v][0] + dp[v][1]) % MOD;
}
}
}
int main() {
init();
dfs(1, 0);
printf("%I64d\n", dp[1][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(mul(cnt0[v], 1), cnt1[v]));
cnt1[u] = add(cnt1[u], divmod(cnt1[v], add(mul(cnt0[v], 1), 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::cerr && false && std::cerr << std::string(40, '-') << std::endl;
for (int u = 0; u < n; ++u) {
std::cerr && false &&
std::cerr << "u = " << u << ", cnt0 = " << cnt0[u]
<< ", cnt1 = " << cnt1[u] << std::endl;
}
std::cerr && false && std::cerr << std::string(40, '-') << std::endl;
std::cout << answ << std::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>
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(mul(cnt0[v], 1), cnt1[v]));
cnt1[u] = add(cnt1[u], divmod(cnt1[v], add(mul(cnt0[v], 1), 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::cerr && false && std::cerr << std::string(40, '-') << std::endl;
for (int u = 0; u < n; ++u) {
std::cerr && false &&
std::cerr << "u = " << u << ", cnt0 = " << cnt0[u]
<< ", cnt1 = " << cnt1[u] << std::endl;
}
std::cerr && false && std::cerr << std::string(40, '-') << std::endl;
std::cout << answ << std::endl;
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long power(long long x, long long y) {
long long res = 1LL;
x = x % 1000000007;
while (y > 0) {
if (y & 1) res = (res * x) % 1000000007;
y = y >> 1;
x = (x * x) % 1000000007;
}
return res % 1000000007;
}
long long inv(long long n) { return power(n, 1000000007 - 2) % 1000000007; }
long long isprime(long long n) {
if (n < 2) return 0;
long long i;
for (i = 2; i * i <= n; i++)
if (n % i == 0) return 0;
return 1;
}
long long n, x, col[200005], dp[2][200005];
vector<long long> a[200005];
void dfs(long long v, long long p) {
long long leaf = 1;
long long pro = 1;
for (auto x : a[v]) {
if (x == p) continue;
dfs(x, v);
pro *= (dp[0][x] + dp[1][x]) % 1000000007;
pro %= 1000000007;
}
if (leaf) {
if (col[v]) {
dp[1][v] = 1;
dp[0][v] = 0;
} else {
dp[1][v] = 0;
dp[0][v] = 1;
}
}
if (col[v]) {
dp[1][v] = pro;
dp[0][v] = 0;
} else {
dp[0][v] = pro;
for (auto x : a[v]) {
if (x == p) continue;
dp[1][v] +=
((pro * inv((dp[0][x] + dp[1][x]) % 1000000007) % 1000000007) *
dp[1][x]) %
1000000007;
dp[1][v] %= 1000000007;
}
}
}
void solve() {
cin >> n;
long long i;
for (i = 1; i < n; i++) {
cin >> x;
a[i].push_back(x);
a[x].push_back(i);
}
for (i = 0; i < n; i++) cin >> col[i];
dfs(0, -1);
cout << dp[1][0] << "\n";
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t = 1;
long long tc = 0;
while (t--) {
tc++;
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;
long long power(long long x, long long y) {
long long res = 1LL;
x = x % 1000000007;
while (y > 0) {
if (y & 1) res = (res * x) % 1000000007;
y = y >> 1;
x = (x * x) % 1000000007;
}
return res % 1000000007;
}
long long inv(long long n) { return power(n, 1000000007 - 2) % 1000000007; }
long long isprime(long long n) {
if (n < 2) return 0;
long long i;
for (i = 2; i * i <= n; i++)
if (n % i == 0) return 0;
return 1;
}
long long n, x, col[200005], dp[2][200005];
vector<long long> a[200005];
void dfs(long long v, long long p) {
long long leaf = 1;
long long pro = 1;
for (auto x : a[v]) {
if (x == p) continue;
dfs(x, v);
pro *= (dp[0][x] + dp[1][x]) % 1000000007;
pro %= 1000000007;
}
if (leaf) {
if (col[v]) {
dp[1][v] = 1;
dp[0][v] = 0;
} else {
dp[1][v] = 0;
dp[0][v] = 1;
}
}
if (col[v]) {
dp[1][v] = pro;
dp[0][v] = 0;
} else {
dp[0][v] = pro;
for (auto x : a[v]) {
if (x == p) continue;
dp[1][v] +=
((pro * inv((dp[0][x] + dp[1][x]) % 1000000007) % 1000000007) *
dp[1][x]) %
1000000007;
dp[1][v] %= 1000000007;
}
}
}
void solve() {
cin >> n;
long long i;
for (i = 1; i < n; i++) {
cin >> x;
a[i].push_back(x);
a[x].push_back(i);
}
for (i = 0; i < n; i++) cin >> col[i];
dfs(0, -1);
cout << dp[1][0] << "\n";
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t = 1;
long long tc = 0;
while (t--) {
tc++;
solve();
}
}
```
|
#include <bits/stdc++.h>
int const mo = 1000000007;
int const maxn = 100007;
bool black[maxn];
int n;
long long f[maxn][2];
std::vector<std::vector<int> > tree;
void add_edge(int u, int v) {
tree[u].push_back(v);
tree[v].push_back(u);
}
void dp(int x, int father) {
f[x][0] = 1;
f[x][1] = 0;
for (int i = 0; i < (int)tree[x].size(); i++) {
int v = tree[x][i];
if (father == v) continue;
dp(v, x);
f[x][1] = (f[x][1] * f[v][0]) % mo;
f[x][1] = (f[x][1] + f[x][0] * f[v][1]) % mo;
f[x][0] = (f[x][0] * f[v][0]) % mo;
}
if (black[x])
f[x][1] = f[x][0];
else
f[x][0] = (f[x][0] + f[x][1]) % mo;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin >> n;
tree.resize(n + 1);
for (int i = 0, x; i < n - 1; i++) {
std::cin >> x;
add_edge(i + 2, x + 1);
}
for (int i = 1; i <= n; i++) std::cin >> black[i];
dp(1, -1);
std::cout << f[1][1] << "\n";
}
|
### 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>
int const mo = 1000000007;
int const maxn = 100007;
bool black[maxn];
int n;
long long f[maxn][2];
std::vector<std::vector<int> > tree;
void add_edge(int u, int v) {
tree[u].push_back(v);
tree[v].push_back(u);
}
void dp(int x, int father) {
f[x][0] = 1;
f[x][1] = 0;
for (int i = 0; i < (int)tree[x].size(); i++) {
int v = tree[x][i];
if (father == v) continue;
dp(v, x);
f[x][1] = (f[x][1] * f[v][0]) % mo;
f[x][1] = (f[x][1] + f[x][0] * f[v][1]) % mo;
f[x][0] = (f[x][0] * f[v][0]) % mo;
}
if (black[x])
f[x][1] = f[x][0];
else
f[x][0] = (f[x][0] + f[x][1]) % mo;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin >> n;
tree.resize(n + 1);
for (int i = 0, x; i < n - 1; i++) {
std::cin >> x;
add_edge(i + 2, x + 1);
}
for (int i = 1; i <= n; i++) std::cin >> black[i];
dp(1, -1);
std::cout << f[1][1] << "\n";
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
long long f1[100010];
long long f2[100010];
list<int> l[100010];
int a[100010];
void dfs(int x) {
if (a[x]) {
f1[x] = 1;
f2[x] = 0;
} else {
f1[x] = 0;
f2[x] = 1;
}
for (auto v : l[x]) {
dfs(v);
f1[x] = (f1[x] * (f2[v] + f1[v]) + f2[x] * f1[v]) % mod;
f2[x] = f2[x] * (f2[v] + f1[v]) % mod;
}
}
int main() {
int n;
int x, i;
scanf("%d", &n);
for (i = 2; i <= n; i++) {
scanf("%d", &x);
x++;
l[x].push_back(i);
}
for (i = 1; i <= n; i++) scanf("%d", &a[i]);
dfs(1);
printf("%I64d\n", f1[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;
long long mod = 1000000007;
long long f1[100010];
long long f2[100010];
list<int> l[100010];
int a[100010];
void dfs(int x) {
if (a[x]) {
f1[x] = 1;
f2[x] = 0;
} else {
f1[x] = 0;
f2[x] = 1;
}
for (auto v : l[x]) {
dfs(v);
f1[x] = (f1[x] * (f2[v] + f1[v]) + f2[x] * f1[v]) % mod;
f2[x] = f2[x] * (f2[v] + f1[v]) % mod;
}
}
int main() {
int n;
int x, i;
scanf("%d", &n);
for (i = 2; i <= n; i++) {
scanf("%d", &x);
x++;
l[x].push_back(i);
}
for (i = 1; i <= n; i++) scanf("%d", &a[i]);
dfs(1);
printf("%I64d\n", f1[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
std::vector<int> children[100005];
int N;
long long int dp1[100005], dp2[100005];
bool isBlack[100005];
inline int max(int a, int b) { return a + (b - a) * (b > a); }
void extendedGCD(int a, int b, long long int& x, long long int& y) {
if (b) {
extendedGCD(b, a % b, x, y);
int aux = y;
y = x - y * (a / b);
x = aux;
} else {
x = 1;
y = 0;
}
}
int modullarInverse(int x) {
long long int a, b;
extendedGCD(1000000007, x, a, b);
while (b < 0) b += 1000000007;
b %= 1000000007;
return b;
}
void solve(int node) {
long long int prod = 1;
for (std::vector<int>::iterator i0 = children[node].begin(),
i1 = children[node].end();
i0 != i1; ++i0) {
solve(*i0);
if (dp2[*i0]) prod = (prod * (dp1[*i0] + dp2[*i0])) % 1000000007;
}
if (!isBlack[node]) {
dp1[node] = 1;
dp2[node] = 0;
for (std::vector<int>::iterator i0 = children[node].begin(),
i1 = children[node].end();
i0 != i1; ++i0) {
dp1[node] = (dp1[node] * (dp1[*i0] + dp2[*i0])) % 1000000007;
if (dp2[*i0])
dp2[node] = (dp2[node] + (prod * modullarInverse(dp1[*i0] + dp2[*i0]) %
1000000007) *
dp2[*i0]) %
1000000007;
}
} else
dp2[node] = prod, dp1[node] = 0;
}
int main() {
int i, x;
scanf("%d", &N);
for (i = 1; i < N; ++i) {
scanf("%d", &x);
children[x].push_back(i);
}
for (i = 0; i < N; ++i) scanf("%d", isBlack + i);
solve(0);
printf("%lld", dp2[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>
std::vector<int> children[100005];
int N;
long long int dp1[100005], dp2[100005];
bool isBlack[100005];
inline int max(int a, int b) { return a + (b - a) * (b > a); }
void extendedGCD(int a, int b, long long int& x, long long int& y) {
if (b) {
extendedGCD(b, a % b, x, y);
int aux = y;
y = x - y * (a / b);
x = aux;
} else {
x = 1;
y = 0;
}
}
int modullarInverse(int x) {
long long int a, b;
extendedGCD(1000000007, x, a, b);
while (b < 0) b += 1000000007;
b %= 1000000007;
return b;
}
void solve(int node) {
long long int prod = 1;
for (std::vector<int>::iterator i0 = children[node].begin(),
i1 = children[node].end();
i0 != i1; ++i0) {
solve(*i0);
if (dp2[*i0]) prod = (prod * (dp1[*i0] + dp2[*i0])) % 1000000007;
}
if (!isBlack[node]) {
dp1[node] = 1;
dp2[node] = 0;
for (std::vector<int>::iterator i0 = children[node].begin(),
i1 = children[node].end();
i0 != i1; ++i0) {
dp1[node] = (dp1[node] * (dp1[*i0] + dp2[*i0])) % 1000000007;
if (dp2[*i0])
dp2[node] = (dp2[node] + (prod * modullarInverse(dp1[*i0] + dp2[*i0]) %
1000000007) *
dp2[*i0]) %
1000000007;
}
} else
dp2[node] = prod, dp1[node] = 0;
}
int main() {
int i, x;
scanf("%d", &N);
for (i = 1; i < N; ++i) {
scanf("%d", &x);
children[x].push_back(i);
}
for (i = 0; i < N; ++i) scanf("%d", isBlack + i);
solve(0);
printf("%lld", dp2[0]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
struct Edge {
int np;
Edge *next;
pair<long long, long long> val;
} E[101000 * 2], *V[101000];
int tope = -1;
int root;
int c[101000];
void addedge(int x, int y) {
E[++tope].np = y;
E[tope].next = V[x];
V[x] = &E[tope];
}
int fa[101000];
bool nr[101000];
pair<long long, long long> dfs(int now) {
Edge *ne;
pair<long long, long long> ret = make_pair(1, 1), pr;
for (ne = V[now]; ne; ne = ne->next) {
if (ne->np == fa[now]) continue;
pr = dfs(ne->np);
ne->val = pr;
}
if (c[now] == 1) {
ret = make_pair(1, 0);
for (ne = V[now]; ne; ne = ne->next) {
if (ne->np == fa[now]) continue;
ret.first = ret.first * (ne->val.first + ne->val.second) % 1000000007;
}
ret.second = 0;
return ret;
} else {
ret = make_pair(0, 1);
for (ne = V[now]; ne; ne = ne->next) {
if (ne->np == fa[now]) continue;
ret.first = (ret.first * (ne->val.second + ne->val.first) % 1000000007 +
ret.second * ne->val.first % 1000000007) %
1000000007;
ret.second = ret.second * (ne->val.second + ne->val.first) % 1000000007;
}
}
return ret;
}
int main() {
int i, j, k, x, y, z;
int n, m;
scanf("%d", &n);
for (i = 0; i < n - 1; i++) {
scanf("%d", &x);
addedge(x, i + 1);
addedge(i + 1, x);
fa[i + 1] = x;
}
for (i = 0; i < n; i++) {
scanf("%d", &c[i]);
}
root = 0;
fa[0] = 0;
pair<long long, long long> pr;
long long ans = dfs(root).first % 1000000007;
printf(
"%I64d"
"\n",
ans);
}
|
### 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;
struct Edge {
int np;
Edge *next;
pair<long long, long long> val;
} E[101000 * 2], *V[101000];
int tope = -1;
int root;
int c[101000];
void addedge(int x, int y) {
E[++tope].np = y;
E[tope].next = V[x];
V[x] = &E[tope];
}
int fa[101000];
bool nr[101000];
pair<long long, long long> dfs(int now) {
Edge *ne;
pair<long long, long long> ret = make_pair(1, 1), pr;
for (ne = V[now]; ne; ne = ne->next) {
if (ne->np == fa[now]) continue;
pr = dfs(ne->np);
ne->val = pr;
}
if (c[now] == 1) {
ret = make_pair(1, 0);
for (ne = V[now]; ne; ne = ne->next) {
if (ne->np == fa[now]) continue;
ret.first = ret.first * (ne->val.first + ne->val.second) % 1000000007;
}
ret.second = 0;
return ret;
} else {
ret = make_pair(0, 1);
for (ne = V[now]; ne; ne = ne->next) {
if (ne->np == fa[now]) continue;
ret.first = (ret.first * (ne->val.second + ne->val.first) % 1000000007 +
ret.second * ne->val.first % 1000000007) %
1000000007;
ret.second = ret.second * (ne->val.second + ne->val.first) % 1000000007;
}
}
return ret;
}
int main() {
int i, j, k, x, y, z;
int n, m;
scanf("%d", &n);
for (i = 0; i < n - 1; i++) {
scanf("%d", &x);
addedge(x, i + 1);
addedge(i + 1, x);
fa[i + 1] = x;
}
for (i = 0; i < n; i++) {
scanf("%d", &c[i]);
}
root = 0;
fa[0] = 0;
pair<long long, long long> pr;
long long ans = dfs(root).first % 1000000007;
printf(
"%I64d"
"\n",
ans);
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n, p[100010], x[100010];
vector<int> ch[100010];
long long a[2][100010];
long long modpow(long long q, long long w) {
long long res = 1;
q %= 1000000007;
while (w) {
if (w & 1) res = res * q % 1000000007;
q = q * q % 1000000007;
w >>= 1;
}
return res;
}
void dfs(int u) {
a[0][u] = 1;
for (int i = 0; i < ch[u].size(); i++) {
int v = ch[u][i];
dfs(v);
a[0][u] *= a[0][v] + a[1][v];
a[0][u] %= 1000000007;
}
if (x[u]) {
a[1][u] = a[0][u];
a[0][u] = 0;
return;
}
for (int i = 0; i < ch[u].size(); i++) {
int v = ch[u][i];
long long b =
a[0][u] * modpow(a[0][v] + a[1][v], 1000000007 - 2) % 1000000007;
b = b * a[1][v] % 1000000007;
a[1][u] += b;
a[1][u] %= 1000000007;
}
}
int main() {
cin >> n;
for (int i = 1; i < n; i++) {
cin >> p[i];
ch[p[i]].push_back(i);
}
for (int i = 0; i < n; i++) cin >> x[i];
dfs(0);
cout << a[1][0] << '\n';
}
|
### 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, p[100010], x[100010];
vector<int> ch[100010];
long long a[2][100010];
long long modpow(long long q, long long w) {
long long res = 1;
q %= 1000000007;
while (w) {
if (w & 1) res = res * q % 1000000007;
q = q * q % 1000000007;
w >>= 1;
}
return res;
}
void dfs(int u) {
a[0][u] = 1;
for (int i = 0; i < ch[u].size(); i++) {
int v = ch[u][i];
dfs(v);
a[0][u] *= a[0][v] + a[1][v];
a[0][u] %= 1000000007;
}
if (x[u]) {
a[1][u] = a[0][u];
a[0][u] = 0;
return;
}
for (int i = 0; i < ch[u].size(); i++) {
int v = ch[u][i];
long long b =
a[0][u] * modpow(a[0][v] + a[1][v], 1000000007 - 2) % 1000000007;
b = b * a[1][v] % 1000000007;
a[1][u] += b;
a[1][u] %= 1000000007;
}
}
int main() {
cin >> n;
for (int i = 1; i < n; i++) {
cin >> p[i];
ch[p[i]].push_back(i);
}
for (int i = 0; i < n; i++) cin >> x[i];
dfs(0);
cout << a[1][0] << '\n';
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <int POS, class TUPLE>
void deploy(std::ostream &os, const TUPLE &tuple) {}
template <int POS, class TUPLE, class H, class... Ts>
void deploy(std::ostream &os, const TUPLE &t) {
os << (POS == 0 ? "" : ", ") << get<POS>(t);
deploy<POS + 1, TUPLE, Ts...>(os, t);
}
template <class T>
std::ostream &operator<<(std::ostream &os, std::vector<T> &v) {
int remain = v.size();
os << "{";
for (auto e : v) os << e << (--remain == 0 ? "}" : ", ");
return os;
}
template <class T>
std::ostream &operator<<(std::ostream &os, std::set<T> &v) {
int remain = v.size();
os << "{";
for (auto e : v) os << e << (--remain == 0 ? "}" : ", ");
return os;
}
template <class T, class K>
std::ostream &operator<<(std::ostream &os, std::map<T, K> &make_pair) {
int remain = make_pair.size();
os << "{";
for (auto e : make_pair)
os << "(" << e.first << " -> " << e.second << ")"
<< (--remain == 0 ? "}" : ", ");
return os;
}
int n;
vector<int> edges[100100];
bool color[100100];
long long memo[100100][2];
void dfs(int now) {
memo[now][0] = 1;
memo[now][1] = 0;
for (auto u : edges[now]) {
dfs(u);
memo[now][1] *= memo[u][0];
memo[now][1] %= 1000000007LL;
memo[now][1] += (memo[now][0] * memo[u][1]) % 1000000007LL;
memo[now][1] %= 1000000007LL;
memo[now][0] *= memo[u][0];
memo[now][0] %= 1000000007LL;
}
if (color[now]) {
memo[now][1] = memo[now][0];
} else {
memo[now][0] += memo[now][1];
memo[now][0] %= 1000000007LL;
}
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cin >> n;
for (int i = 1; i < (n); i++) {
int x;
cin >> x;
edges[x].push_back(i);
}
for (int i = 0; i < (n); i++) {
cin >> color[i];
}
dfs(0);
cout << memo[0][1] << "\n";
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;
template <int POS, class TUPLE>
void deploy(std::ostream &os, const TUPLE &tuple) {}
template <int POS, class TUPLE, class H, class... Ts>
void deploy(std::ostream &os, const TUPLE &t) {
os << (POS == 0 ? "" : ", ") << get<POS>(t);
deploy<POS + 1, TUPLE, Ts...>(os, t);
}
template <class T>
std::ostream &operator<<(std::ostream &os, std::vector<T> &v) {
int remain = v.size();
os << "{";
for (auto e : v) os << e << (--remain == 0 ? "}" : ", ");
return os;
}
template <class T>
std::ostream &operator<<(std::ostream &os, std::set<T> &v) {
int remain = v.size();
os << "{";
for (auto e : v) os << e << (--remain == 0 ? "}" : ", ");
return os;
}
template <class T, class K>
std::ostream &operator<<(std::ostream &os, std::map<T, K> &make_pair) {
int remain = make_pair.size();
os << "{";
for (auto e : make_pair)
os << "(" << e.first << " -> " << e.second << ")"
<< (--remain == 0 ? "}" : ", ");
return os;
}
int n;
vector<int> edges[100100];
bool color[100100];
long long memo[100100][2];
void dfs(int now) {
memo[now][0] = 1;
memo[now][1] = 0;
for (auto u : edges[now]) {
dfs(u);
memo[now][1] *= memo[u][0];
memo[now][1] %= 1000000007LL;
memo[now][1] += (memo[now][0] * memo[u][1]) % 1000000007LL;
memo[now][1] %= 1000000007LL;
memo[now][0] *= memo[u][0];
memo[now][0] %= 1000000007LL;
}
if (color[now]) {
memo[now][1] = memo[now][0];
} else {
memo[now][0] += memo[now][1];
memo[now][0] %= 1000000007LL;
}
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cin >> n;
for (int i = 1; i < (n); i++) {
int x;
cin >> x;
edges[x].push_back(i);
}
for (int i = 0; i < (n); i++) {
cin >> color[i];
}
dfs(0);
cout << memo[0][1] << "\n";
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long dp[100005][2];
vector<int> adj[1000000];
long long mod = 1000000007;
int x[1000000];
int n;
void dfs(int v) {
dp[v][x[v]] = 1;
for (auto u : adj[v]) {
dfs(u);
dp[v][1] = (dp[v][0] * dp[u][1] + dp[v][1] * (dp[u][0] + dp[u][1])) % mod;
dp[v][0] = (dp[v][0] * (dp[u][0] + dp[u][1])) % mod;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin >> n;
for (int i = 2; i <= n; i++) {
int p;
cin >> p;
p++;
adj[p].push_back(i);
}
for (int i = 1; i <= n; i++) cin >> x[i];
dfs(1);
cout << dp[1][1] << 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;
long long dp[100005][2];
vector<int> adj[1000000];
long long mod = 1000000007;
int x[1000000];
int n;
void dfs(int v) {
dp[v][x[v]] = 1;
for (auto u : adj[v]) {
dfs(u);
dp[v][1] = (dp[v][0] * dp[u][1] + dp[v][1] * (dp[u][0] + dp[u][1])) % mod;
dp[v][0] = (dp[v][0] * (dp[u][0] + dp[u][1])) % mod;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin >> n;
for (int i = 2; i <= n; i++) {
int p;
cin >> p;
p++;
adj[p].push_back(i);
}
for (int i = 1; i <= n; i++) cin >> x[i];
dfs(1);
cout << dp[1][1] << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long p(long long x, long long n) {
x %= 1000000007ll;
long long ret = 1;
while (n > 0) {
if (n & (1ll)) ret = ret * x % 1000000007ll;
x = x * x % 1000000007ll;
n >>= (1ll);
}
return ret;
}
int n;
vector<int> node[233333];
int x[233333];
long long dp[233333][3];
void dfs(int u, int fa) {
int cnt = 0;
for (int i = 0; i < node[u].size(); i++) {
if (node[u][i] != fa) dfs(node[u][i], u), cnt++;
}
if (cnt == 0) {
if (x[u] == 0) {
dp[u][0] = 0;
dp[u][1] = 1;
dp[u][2] = 0;
} else {
dp[u][0] = 1;
dp[u][1] = 0;
dp[u][2] = 1;
}
return;
}
if (x[u] == 1) {
dp[u][0] = 1;
dp[u][1] = 0;
for (int i = 0; i < node[u].size(); i++) {
int v = node[u][i];
if (v == fa) continue;
dp[u][0] =
(dp[u][0] * ((dp[v][1] + dp[v][2]) % 1000000007ll)) % 1000000007ll;
}
dp[u][2] = dp[u][0];
} else {
dp[u][1] = 1;
dp[u][0] = 0;
for (int i = 0; i < node[u].size(); i++) {
int v = node[u][i];
if (v == fa) continue;
dp[u][0] = dp[u][0] * (dp[v][1] + dp[v][2]) % 1000000007ll +
dp[u][1] * dp[v][0] % 1000000007ll;
dp[u][0] %= 1000000007ll;
dp[u][1] = dp[u][1] * (dp[v][1] + dp[v][2]) % 1000000007ll;
}
dp[u][2] = dp[u][0];
}
}
int Main() {
cin >> n;
for (int i = 0; i < n - 1; i++) {
int p;
cin >> p;
node[i + 2].push_back(p + 1);
node[p + 1].push_back(i + 2);
}
for (int i = 1; i <= n; i++) {
cin >> x[i];
}
dfs(1, -1);
cout << dp[1][2] << endl;
return 0;
}
int main() { return Main(); }
|
### 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 p(long long x, long long n) {
x %= 1000000007ll;
long long ret = 1;
while (n > 0) {
if (n & (1ll)) ret = ret * x % 1000000007ll;
x = x * x % 1000000007ll;
n >>= (1ll);
}
return ret;
}
int n;
vector<int> node[233333];
int x[233333];
long long dp[233333][3];
void dfs(int u, int fa) {
int cnt = 0;
for (int i = 0; i < node[u].size(); i++) {
if (node[u][i] != fa) dfs(node[u][i], u), cnt++;
}
if (cnt == 0) {
if (x[u] == 0) {
dp[u][0] = 0;
dp[u][1] = 1;
dp[u][2] = 0;
} else {
dp[u][0] = 1;
dp[u][1] = 0;
dp[u][2] = 1;
}
return;
}
if (x[u] == 1) {
dp[u][0] = 1;
dp[u][1] = 0;
for (int i = 0; i < node[u].size(); i++) {
int v = node[u][i];
if (v == fa) continue;
dp[u][0] =
(dp[u][0] * ((dp[v][1] + dp[v][2]) % 1000000007ll)) % 1000000007ll;
}
dp[u][2] = dp[u][0];
} else {
dp[u][1] = 1;
dp[u][0] = 0;
for (int i = 0; i < node[u].size(); i++) {
int v = node[u][i];
if (v == fa) continue;
dp[u][0] = dp[u][0] * (dp[v][1] + dp[v][2]) % 1000000007ll +
dp[u][1] * dp[v][0] % 1000000007ll;
dp[u][0] %= 1000000007ll;
dp[u][1] = dp[u][1] * (dp[v][1] + dp[v][2]) % 1000000007ll;
}
dp[u][2] = dp[u][0];
}
}
int Main() {
cin >> n;
for (int i = 0; i < n - 1; i++) {
int p;
cin >> p;
node[i + 2].push_back(p + 1);
node[p + 1].push_back(i + 2);
}
for (int i = 1; i <= n; i++) {
cin >> x[i];
}
dfs(1, -1);
cout << dp[1][2] << endl;
return 0;
}
int main() { return Main(); }
```
|
#include <bits/stdc++.h>
using namespace std;
const long long mx = 1000 * 100 + 10;
vector<long long> ad[mx];
long long col[mx], dp0[mx], dp1[mx], m = 1000 * 1000 * 1000 + 7;
void dfs(long long v) {
if (col[v])
dp1[v] = 1;
else
dp0[v] = 1;
for (long long i = 0; i < (long long)ad[v].size(); i++) {
long long u = ad[v][i];
dfs(u);
dp1[v] = (dp1[v] * (dp1[u] + dp0[u]) + dp0[v] * dp1[u]) % m;
dp0[v] = (dp0[v] * (dp1[u] + dp0[u])) % m;
}
return;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, x;
cin >> n;
for (long long i = 0; i < n - 1; i++) {
cin >> x;
ad[x].push_back(i + 1);
}
for (long long i = 0; i < n; i++) cin >> col[i];
dfs(0);
cout << dp1[0] << 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 long long mx = 1000 * 100 + 10;
vector<long long> ad[mx];
long long col[mx], dp0[mx], dp1[mx], m = 1000 * 1000 * 1000 + 7;
void dfs(long long v) {
if (col[v])
dp1[v] = 1;
else
dp0[v] = 1;
for (long long i = 0; i < (long long)ad[v].size(); i++) {
long long u = ad[v][i];
dfs(u);
dp1[v] = (dp1[v] * (dp1[u] + dp0[u]) + dp0[v] * dp1[u]) % m;
dp0[v] = (dp0[v] * (dp1[u] + dp0[u])) % m;
}
return;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, x;
cin >> n;
for (long long i = 0; i < n - 1; i++) {
cin >> x;
ad[x].push_back(i + 1);
}
for (long long i = 0; i < n; i++) cin >> col[i];
dfs(0);
cout << dp1[0] << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
const int MAXE = 200005;
const int mod = 1e9 + 7;
struct Edge {
int v;
Edge* next;
} E[MAXE], *H[MAXN], *cur;
int color[MAXN];
long long dp[MAXN][2];
void clear() {
cur = E;
memset(H, 0, sizeof H);
memset(color, 0, sizeof color);
memset(dp, 0, sizeof dp);
}
void addedge(int u, int v) {
cur->v = v;
cur->next = H[u];
H[u] = cur++;
}
void scanf(int& x, char c = 0) {
while ((c = getchar()) < '0' || c > '9')
;
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9') x = x * 10 + c - '0';
}
void dfs(int u, int f = 0) {
dp[u][color[u]] = 1;
for (Edge* e = H[u]; e; e = e->next) {
int v = e->v;
if (v == f) continue;
dfs(v, u);
dp[u][1] =
(dp[u][1] * (dp[v][0] + dp[v][1]) % mod + dp[u][0] * dp[v][1]) % mod;
dp[u][0] = dp[u][0] * (dp[v][0] + dp[v][1]) % mod;
}
}
void solve() {
int n, x;
while (~scanf("%d", &n)) {
clear();
for (int i = (1); i < (n); ++i) {
scanf(x);
addedge(x, i);
addedge(i, x);
}
for (int i = (0); i < (n); ++i) scanf(color[i]);
dfs(0);
printf("%I64d\n", dp[0][1]);
}
}
int main() {
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 = 100005;
const int MAXE = 200005;
const int mod = 1e9 + 7;
struct Edge {
int v;
Edge* next;
} E[MAXE], *H[MAXN], *cur;
int color[MAXN];
long long dp[MAXN][2];
void clear() {
cur = E;
memset(H, 0, sizeof H);
memset(color, 0, sizeof color);
memset(dp, 0, sizeof dp);
}
void addedge(int u, int v) {
cur->v = v;
cur->next = H[u];
H[u] = cur++;
}
void scanf(int& x, char c = 0) {
while ((c = getchar()) < '0' || c > '9')
;
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9') x = x * 10 + c - '0';
}
void dfs(int u, int f = 0) {
dp[u][color[u]] = 1;
for (Edge* e = H[u]; e; e = e->next) {
int v = e->v;
if (v == f) continue;
dfs(v, u);
dp[u][1] =
(dp[u][1] * (dp[v][0] + dp[v][1]) % mod + dp[u][0] * dp[v][1]) % mod;
dp[u][0] = dp[u][0] * (dp[v][0] + dp[v][1]) % mod;
}
}
void solve() {
int n, x;
while (~scanf("%d", &n)) {
clear();
for (int i = (1); i < (n); ++i) {
scanf(x);
addedge(x, i);
addedge(i, x);
}
for (int i = (0); i < (n); ++i) scanf(color[i]);
dfs(0);
printf("%I64d\n", dp[0][1]);
}
}
int main() {
solve();
return 0;
}
```
|
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:100000000000000")
const long long int INF = 2e15 + 1;
using namespace std;
vector<long long int> g[2000010];
long long int cnt[2000010][2];
long long int color[2000010];
long long int mod = 1000000007;
vector<long long int> d;
long long int binpow(long long int a, long long int b) {
if (!b) return 1;
if (b & 1) {
return (a * binpow(a, b - 1)) % mod;
}
long long int te = binpow(a, b / 2);
return (te * te) % mod;
}
void dfs(long long int id, long long int p) {
long long int q = -1;
for (long long int(i) = 0; (i) < ((long long int)(g[id]).size()); (i)++) {
long long int to = g[id][i];
if (to == p) continue;
dfs(to, id);
if (color[id]) {
if (cnt[to][1] != -1) {
if (q == -1) {
q = cnt[to][1];
} else
q = (q * cnt[to][1]) % mod;
}
} else {
if (cnt[to][1] != -1) {
if (cnt[id][1] == -1)
cnt[id][1] = cnt[to][1];
else
cnt[id][1] = (cnt[id][1] * cnt[to][1]) % mod;
}
}
}
if (q != -1) d.push_back(q);
if (color[id]) {
cnt[id][0] = cnt[id][1] = 1;
} else if (cnt[id][1] != -1) {
cnt[id][0] = 0;
for (long long int(i) = 0; (i) < ((long long int)(g[id]).size()); (i)++) {
long long int to = g[id][i];
if (to == p || cnt[to][1] == -1) continue;
long long int all = (cnt[id][1] * binpow(cnt[to][1], mod - 2)) % mod;
cnt[id][0] = (cnt[id][0] + (all * cnt[to][0]) % mod) % mod;
}
cnt[id][1] = (cnt[id][1] + cnt[id][0]) % mod;
} else {
cnt[id][0] = 0;
cnt[id][1] = 1;
}
}
int main() {
memset(cnt, -1, sizeof cnt);
long long int n;
cin >> n;
for (long long int(i) = 0; (i) < (n - 1); (i)++) {
long long int with;
cin >> with;
g[with].push_back(i + 1);
g[i + 1].push_back(with);
}
for (long long int(i) = 0; (i) < (n); (i)++) {
cin >> color[i];
}
for (long long int(i) = 0; (i) < (n); (i)++) {
if (color[i]) {
dfs(i, -1);
break;
}
}
if (!(long long int)(d).size()) {
cout << 0;
} else {
long long int ans = 1;
for (long long int(i) = 0; (i) < ((long long int)(d).size()); (i)++) {
ans = (ans * d[i]) % mod;
}
cout << ans;
}
}
|
### 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>
#pragma comment(linker, "/STACK:100000000000000")
const long long int INF = 2e15 + 1;
using namespace std;
vector<long long int> g[2000010];
long long int cnt[2000010][2];
long long int color[2000010];
long long int mod = 1000000007;
vector<long long int> d;
long long int binpow(long long int a, long long int b) {
if (!b) return 1;
if (b & 1) {
return (a * binpow(a, b - 1)) % mod;
}
long long int te = binpow(a, b / 2);
return (te * te) % mod;
}
void dfs(long long int id, long long int p) {
long long int q = -1;
for (long long int(i) = 0; (i) < ((long long int)(g[id]).size()); (i)++) {
long long int to = g[id][i];
if (to == p) continue;
dfs(to, id);
if (color[id]) {
if (cnt[to][1] != -1) {
if (q == -1) {
q = cnt[to][1];
} else
q = (q * cnt[to][1]) % mod;
}
} else {
if (cnt[to][1] != -1) {
if (cnt[id][1] == -1)
cnt[id][1] = cnt[to][1];
else
cnt[id][1] = (cnt[id][1] * cnt[to][1]) % mod;
}
}
}
if (q != -1) d.push_back(q);
if (color[id]) {
cnt[id][0] = cnt[id][1] = 1;
} else if (cnt[id][1] != -1) {
cnt[id][0] = 0;
for (long long int(i) = 0; (i) < ((long long int)(g[id]).size()); (i)++) {
long long int to = g[id][i];
if (to == p || cnt[to][1] == -1) continue;
long long int all = (cnt[id][1] * binpow(cnt[to][1], mod - 2)) % mod;
cnt[id][0] = (cnt[id][0] + (all * cnt[to][0]) % mod) % mod;
}
cnt[id][1] = (cnt[id][1] + cnt[id][0]) % mod;
} else {
cnt[id][0] = 0;
cnt[id][1] = 1;
}
}
int main() {
memset(cnt, -1, sizeof cnt);
long long int n;
cin >> n;
for (long long int(i) = 0; (i) < (n - 1); (i)++) {
long long int with;
cin >> with;
g[with].push_back(i + 1);
g[i + 1].push_back(with);
}
for (long long int(i) = 0; (i) < (n); (i)++) {
cin >> color[i];
}
for (long long int(i) = 0; (i) < (n); (i)++) {
if (color[i]) {
dfs(i, -1);
break;
}
}
if (!(long long int)(d).size()) {
cout << 0;
} else {
long long int ans = 1;
for (long long int(i) = 0; (i) < ((long long int)(d).size()); (i)++) {
ans = (ans * d[i]) % mod;
}
cout << ans;
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int sz = 2e5 + 5, mod = 1e9 + 7;
long long ans = 1;
int a[sz], csub[sz], fdp[sz], dp[sz];
vector<int> g[sz];
void go(int u, int p) {
csub[u] = a[u];
for (int v : g[u])
if (v ^ p) go(v, u), csub[u] += csub[v];
}
int dfs(int u, int p, int d);
int fnc(int u, int p, int d) {
int &w = fdp[u];
if (w >= 0) return w;
if (a[u]) {
long long sum = 1;
for (int v : g[u])
if (v ^ p)
if (csub[v]) sum = sum * dfs(v, u, 1) % mod;
return w = sum;
} else {
int cnt = 0;
for (int v : g[u])
if (v ^ p) cnt += csub[v] > 0;
if (cnt == 1)
for (int v : g[u])
if (v ^ p and csub[v]) return w = fnc(v, u, d + 1);
vector<int> res, fuck;
for (int v : g[u])
if (v ^ p and csub[v]) {
res.push_back(dfs(v, u, 1));
fuck.push_back(fnc(v, u, 1));
}
long long sum = 0;
vector<long long> suff(cnt), pref(cnt);
pref[0] = res[0];
for (int i = 1; i < cnt; i++) pref[i] = pref[i - 1] * res[i] % mod;
suff.back() = res.back();
for (int i = cnt - 2; i >= 0; i--) suff[i] = suff[i + 1] * res[i] % mod;
for (int i = 0; i < cnt; i++) {
long long now = fuck[i];
if (i) now = now * pref[i - 1] % mod;
if (i + 1 < cnt) now = now * suff[i + 1] % mod;
sum += now;
}
return fdp[u] = sum % mod;
}
}
int dfs(int u, int p, int d) {
int &w = dp[u];
if (w >= 0) return w;
if (a[u]) {
long long sum = d;
for (int v : g[u])
if (v ^ p)
if (csub[v]) sum = sum * dfs(v, u, 1) % mod;
return w = sum;
} else {
int cnt = 0;
for (int v : g[u])
if (v ^ p) cnt += csub[v] > 0;
if (cnt == 1)
for (int v : g[u])
if (v ^ p and csub[v]) return w = dfs(v, u, d + 1);
vector<int> res, fuck;
for (int v : g[u])
if (v ^ p and csub[v]) {
res.push_back(dfs(v, u, 1));
fuck.push_back(fnc(v, u, 1));
}
long long sum = 1;
for (int v : res) sum = sum * v % mod;
vector<long long> suff(cnt), pref(cnt);
pref[0] = res[0];
for (int i = 1; i < cnt; i++) pref[i] = pref[i - 1] * res[i] % mod;
suff.back() = res.back();
for (int i = cnt - 2; i >= 0; i--) suff[i] = suff[i + 1] * res[i] % mod;
for (int i = 0; i < cnt; i++) {
long long now = (long long)d * fuck[i] % mod;
if (i) now = now * pref[i - 1] % mod;
if (i + 1 < cnt) now = now * suff[i + 1] % mod;
sum += now;
}
return w = sum % mod;
}
}
int main() {
int n;
cin >> n;
for (int i = 2; i <= n; i++) {
int j;
scanf("%d", &j);
j++;
g[j].push_back(i);
g[i].push_back(j);
}
memset(dp, -1, sizeof(dp));
memset(fdp, -1, sizeof(fdp));
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n; i++)
if (a[i]) {
go(i, 0);
cout << dfs(i, 0, 1);
break;
}
}
|
### 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 sz = 2e5 + 5, mod = 1e9 + 7;
long long ans = 1;
int a[sz], csub[sz], fdp[sz], dp[sz];
vector<int> g[sz];
void go(int u, int p) {
csub[u] = a[u];
for (int v : g[u])
if (v ^ p) go(v, u), csub[u] += csub[v];
}
int dfs(int u, int p, int d);
int fnc(int u, int p, int d) {
int &w = fdp[u];
if (w >= 0) return w;
if (a[u]) {
long long sum = 1;
for (int v : g[u])
if (v ^ p)
if (csub[v]) sum = sum * dfs(v, u, 1) % mod;
return w = sum;
} else {
int cnt = 0;
for (int v : g[u])
if (v ^ p) cnt += csub[v] > 0;
if (cnt == 1)
for (int v : g[u])
if (v ^ p and csub[v]) return w = fnc(v, u, d + 1);
vector<int> res, fuck;
for (int v : g[u])
if (v ^ p and csub[v]) {
res.push_back(dfs(v, u, 1));
fuck.push_back(fnc(v, u, 1));
}
long long sum = 0;
vector<long long> suff(cnt), pref(cnt);
pref[0] = res[0];
for (int i = 1; i < cnt; i++) pref[i] = pref[i - 1] * res[i] % mod;
suff.back() = res.back();
for (int i = cnt - 2; i >= 0; i--) suff[i] = suff[i + 1] * res[i] % mod;
for (int i = 0; i < cnt; i++) {
long long now = fuck[i];
if (i) now = now * pref[i - 1] % mod;
if (i + 1 < cnt) now = now * suff[i + 1] % mod;
sum += now;
}
return fdp[u] = sum % mod;
}
}
int dfs(int u, int p, int d) {
int &w = dp[u];
if (w >= 0) return w;
if (a[u]) {
long long sum = d;
for (int v : g[u])
if (v ^ p)
if (csub[v]) sum = sum * dfs(v, u, 1) % mod;
return w = sum;
} else {
int cnt = 0;
for (int v : g[u])
if (v ^ p) cnt += csub[v] > 0;
if (cnt == 1)
for (int v : g[u])
if (v ^ p and csub[v]) return w = dfs(v, u, d + 1);
vector<int> res, fuck;
for (int v : g[u])
if (v ^ p and csub[v]) {
res.push_back(dfs(v, u, 1));
fuck.push_back(fnc(v, u, 1));
}
long long sum = 1;
for (int v : res) sum = sum * v % mod;
vector<long long> suff(cnt), pref(cnt);
pref[0] = res[0];
for (int i = 1; i < cnt; i++) pref[i] = pref[i - 1] * res[i] % mod;
suff.back() = res.back();
for (int i = cnt - 2; i >= 0; i--) suff[i] = suff[i + 1] * res[i] % mod;
for (int i = 0; i < cnt; i++) {
long long now = (long long)d * fuck[i] % mod;
if (i) now = now * pref[i - 1] % mod;
if (i + 1 < cnt) now = now * suff[i + 1] % mod;
sum += now;
}
return w = sum % mod;
}
}
int main() {
int n;
cin >> n;
for (int i = 2; i <= n; i++) {
int j;
scanf("%d", &j);
j++;
g[j].push_back(i);
g[i].push_back(j);
}
memset(dp, -1, sizeof(dp));
memset(fdp, -1, sizeof(fdp));
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n; i++)
if (a[i]) {
go(i, 0);
cout << dfs(i, 0, 1);
break;
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long int inf = 1e18, M = 1e9 + 7;
const long long int N = 1e5 + 5;
vector<long long int> v[N], val(N, 0);
long long int dp[N][2];
long long int zero, one;
void dfs(long long int x, long long int p) {
dp[x][val[x]] = 1;
for (auto c : v[x]) {
if (c == p) continue;
dfs(c, x);
zero = dp[x][0];
one = dp[x][1];
dp[x][0] = (zero * dp[c][0]) % M;
dp[x][1] = ((zero * dp[c][1]) % M + (one * dp[c][0]) % M) % M;
dp[x][0] = (dp[x][0] + (zero * dp[c][1]) % M) % M;
dp[x][1] = (dp[x][1] + (one * dp[c][1]) % M) % M;
}
}
void solve() {
memset(dp, 0, sizeof(dp));
long long int n;
cin >> n;
long long int x;
for (long long int i = 1; i < n; ++i) {
cin >> x;
v[x].push_back(i);
v[i].push_back(x);
}
for (long long int i = 0; i < n; ++i) cin >> val[i];
dfs(0, -1);
cout << dp[0][1];
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long int t = 1;
while (t--) {
solve();
}
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 int inf = 1e18, M = 1e9 + 7;
const long long int N = 1e5 + 5;
vector<long long int> v[N], val(N, 0);
long long int dp[N][2];
long long int zero, one;
void dfs(long long int x, long long int p) {
dp[x][val[x]] = 1;
for (auto c : v[x]) {
if (c == p) continue;
dfs(c, x);
zero = dp[x][0];
one = dp[x][1];
dp[x][0] = (zero * dp[c][0]) % M;
dp[x][1] = ((zero * dp[c][1]) % M + (one * dp[c][0]) % M) % M;
dp[x][0] = (dp[x][0] + (zero * dp[c][1]) % M) % M;
dp[x][1] = (dp[x][1] + (one * dp[c][1]) % M) % M;
}
}
void solve() {
memset(dp, 0, sizeof(dp));
long long int n;
cin >> n;
long long int x;
for (long long int i = 1; i < n; ++i) {
cin >> x;
v[x].push_back(i);
v[i].push_back(x);
}
for (long long int i = 0; i < n; ++i) cin >> val[i];
dfs(0, -1);
cout << dp[0][1];
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long int t = 1;
while (t--) {
solve();
}
return 0;
}
```
|
#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
int n;
vector<int> g[100020];
int col[100020];
long long dp[100020][2];
void dfs(int u) {
dp[u][0] = 1;
dp[u][1] = 0;
for (int i = 0; i < g[u].size(); ++i) {
int v = g[u][i];
dfs(v);
dp[u][1] = dp[u][1] * dp[v][0] % 1000000007;
dp[u][1] = (dp[u][1] + dp[u][0] * dp[v][1]) % 1000000007;
dp[u][0] = dp[u][0] * dp[v][0] % 1000000007;
}
if (col[u] == 1)
dp[u][1] = dp[u][0];
else
dp[u][0] += dp[u][1];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
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
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>
#pragma warning(disable : 4996)
using namespace std;
int n;
vector<int> g[100020];
int col[100020];
long long dp[100020][2];
void dfs(int u) {
dp[u][0] = 1;
dp[u][1] = 0;
for (int i = 0; i < g[u].size(); ++i) {
int v = g[u][i];
dfs(v);
dp[u][1] = dp[u][1] * dp[v][0] % 1000000007;
dp[u][1] = (dp[u][1] + dp[u][0] * dp[v][1]) % 1000000007;
dp[u][0] = dp[u][0] * dp[v][0] % 1000000007;
}
if (col[u] == 1)
dp[u][1] = dp[u][0];
else
dp[u][0] += dp[u][1];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
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;
const int mod = 1e9 + 7;
vector<int> G[100010];
int dp[100010][2], color[100010];
inline void addIt(int &x, int val) {
x += val;
if (x >= mod) x -= mod;
}
void dfs(int u, int father) {
dp[u][1] = color[u];
dp[u][0] = color[u] ^ 1;
for (vector<int>::iterator e = G[u].begin(); e != G[u].end(); e++) {
int v = *e;
if (v == father) continue;
dfs(v, u);
int temp[2] = {0, 0};
addIt(temp[0], dp[u][0] * 1LL * dp[v][0] % mod);
addIt(temp[0], dp[u][0] * 1LL * dp[v][1] % mod);
addIt(temp[1], dp[u][1] * 1LL * dp[v][0] % mod);
addIt(temp[1], dp[u][1] * 1LL * dp[v][1] % mod);
addIt(temp[1], dp[u][0] * 1LL * dp[v][1] % mod);
dp[u][0] = temp[0];
dp[u][1] = temp[1];
}
}
int main() {
int N, from, to, u;
scanf("%d", &N);
for (int i = 0; i < N - 1; i++) {
scanf("%d", &from);
G[from].push_back(i + 1);
G[i + 1].push_back(from);
}
for (int i = 0; i < N; i++) scanf("%d", color + i);
dfs(0, -1);
printf("%d\n", dp[0][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;
const int mod = 1e9 + 7;
vector<int> G[100010];
int dp[100010][2], color[100010];
inline void addIt(int &x, int val) {
x += val;
if (x >= mod) x -= mod;
}
void dfs(int u, int father) {
dp[u][1] = color[u];
dp[u][0] = color[u] ^ 1;
for (vector<int>::iterator e = G[u].begin(); e != G[u].end(); e++) {
int v = *e;
if (v == father) continue;
dfs(v, u);
int temp[2] = {0, 0};
addIt(temp[0], dp[u][0] * 1LL * dp[v][0] % mod);
addIt(temp[0], dp[u][0] * 1LL * dp[v][1] % mod);
addIt(temp[1], dp[u][1] * 1LL * dp[v][0] % mod);
addIt(temp[1], dp[u][1] * 1LL * dp[v][1] % mod);
addIt(temp[1], dp[u][0] * 1LL * dp[v][1] % mod);
dp[u][0] = temp[0];
dp[u][1] = temp[1];
}
}
int main() {
int N, from, to, u;
scanf("%d", &N);
for (int i = 0; i < N - 1; i++) {
scanf("%d", &from);
G[from].push_back(i + 1);
G[i + 1].push_back(from);
}
for (int i = 0; i < N; i++) scanf("%d", color + i);
dfs(0, -1);
printf("%d\n", dp[0][1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n, u, v, x[int(1e5 + 10)];
long long int dp0[int(1e5 + 10)], dp1[int(1e5 + 10)];
vector<int> g[int(1e5 + 10)];
long long int power(long long int x, long long int n) {
if (n == 0ll) return 1ll;
long long int y = power(x, n / 2ll);
y = (y * y) % int(1e9 + 7);
if (n % 2ll) {
return (x * y) % int(1e9 + 7);
}
return y;
}
void dfs(int s, int p) {
dp0[s] = 1ll;
dp1[s] = 0ll;
long long int tmp;
for (int i = 0; i < g[s].size(); i++) {
if (g[s][i] != p) {
dfs(g[s][i], s);
dp0[s] = (dp0[s] * (dp0[g[s][i]] + dp1[g[s][i]]) % int(1e9 + 7)) %
int(1e9 + 7);
}
}
for (int i = 0; i < g[s].size(); i++) {
if (g[s][i] != p) {
tmp = (dp1[g[s][i]] * dp0[s]) % int(1e9 + 7);
tmp = (tmp * power((dp0[g[s][i]] + dp1[g[s][i]]) % int(1e9 + 7),
int(1e9 + 7) - 2)) %
int(1e9 + 7);
dp1[s] = (dp1[s] + tmp) % int(1e9 + 7);
}
}
if (x[s] == 1) {
swap(dp0[s], dp1[s]);
dp0[s] = 0;
}
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cout.precision(10);
cin >> n;
for (int i = 0; i + 1 < n; i++) {
cin >> u;
v = i + 1;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 0; i < n; i++) {
cin >> x[i];
}
dfs(0, -1);
cout << dp1[0] << 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;
int n, u, v, x[int(1e5 + 10)];
long long int dp0[int(1e5 + 10)], dp1[int(1e5 + 10)];
vector<int> g[int(1e5 + 10)];
long long int power(long long int x, long long int n) {
if (n == 0ll) return 1ll;
long long int y = power(x, n / 2ll);
y = (y * y) % int(1e9 + 7);
if (n % 2ll) {
return (x * y) % int(1e9 + 7);
}
return y;
}
void dfs(int s, int p) {
dp0[s] = 1ll;
dp1[s] = 0ll;
long long int tmp;
for (int i = 0; i < g[s].size(); i++) {
if (g[s][i] != p) {
dfs(g[s][i], s);
dp0[s] = (dp0[s] * (dp0[g[s][i]] + dp1[g[s][i]]) % int(1e9 + 7)) %
int(1e9 + 7);
}
}
for (int i = 0; i < g[s].size(); i++) {
if (g[s][i] != p) {
tmp = (dp1[g[s][i]] * dp0[s]) % int(1e9 + 7);
tmp = (tmp * power((dp0[g[s][i]] + dp1[g[s][i]]) % int(1e9 + 7),
int(1e9 + 7) - 2)) %
int(1e9 + 7);
dp1[s] = (dp1[s] + tmp) % int(1e9 + 7);
}
}
if (x[s] == 1) {
swap(dp0[s], dp1[s]);
dp0[s] = 0;
}
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cout.precision(10);
cin >> n;
for (int i = 0; i + 1 < n; i++) {
cin >> u;
v = i + 1;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 0; i < n; i++) {
cin >> x[i];
}
dfs(0, -1);
cout << dp1[0] << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
vector<long long int> adjlist[300005];
long long int color[300005];
long long int dp[300005][2];
long long int mark[300005];
void dfs(long long int cur) {
mark[cur] = 1;
long long int len = adjlist[cur].size();
long long int i, j;
dp[cur][color[cur]] = 1;
for (i = 0; i < len; i++) {
long long int child = adjlist[cur][i];
if (mark[child]) continue;
dfs(child);
dp[cur][1] = (dp[cur][0] * dp[child][1] +
dp[cur][1] * (dp[child][0] + dp[child][1])) %
mod;
dp[cur][0] = (dp[cur][0] * (dp[child][0] + dp[child][1])) % mod;
}
}
int main() {
ios_base ::sync_with_stdio(false);
cin.tie(NULL);
long long int n;
long long int i, j;
cin >> n;
for (i = 0; i < n - 1; i++) {
long long int temp;
cin >> temp;
adjlist[i + 1].push_back(temp);
adjlist[temp].push_back(i + 1);
}
for (i = 0; i < n; i++) cin >> color[i];
dfs(0);
cout << 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;
const long long int mod = 1e9 + 7;
vector<long long int> adjlist[300005];
long long int color[300005];
long long int dp[300005][2];
long long int mark[300005];
void dfs(long long int cur) {
mark[cur] = 1;
long long int len = adjlist[cur].size();
long long int i, j;
dp[cur][color[cur]] = 1;
for (i = 0; i < len; i++) {
long long int child = adjlist[cur][i];
if (mark[child]) continue;
dfs(child);
dp[cur][1] = (dp[cur][0] * dp[child][1] +
dp[cur][1] * (dp[child][0] + dp[child][1])) %
mod;
dp[cur][0] = (dp[cur][0] * (dp[child][0] + dp[child][1])) % mod;
}
}
int main() {
ios_base ::sync_with_stdio(false);
cin.tie(NULL);
long long int n;
long long int i, j;
cin >> n;
for (i = 0; i < n - 1; i++) {
long long int temp;
cin >> temp;
adjlist[i + 1].push_back(temp);
adjlist[temp].push_back(i + 1);
}
for (i = 0; i < n; i++) cin >> color[i];
dfs(0);
cout << dp[0][1];
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n, col[123456];
long long dp[123456][2];
vector<int> G[123456];
void addedge(int x, int y) { G[x].push_back(y); }
void init() {
scanf("%d", &n);
for (int i = 0; i < n - 1; i++) {
int tmp;
scanf("%d", &tmp);
addedge(tmp, i + 1);
}
for (int i = 0; i < n; i++) scanf("%d", &col[i]);
}
void dfs(int v) {
dp[v][col[v]] = 1;
for (int i = 0; i < G[v].size(); i++) {
int u = G[v][i];
dfs(u);
dp[v][1] =
(dp[v][1] * dp[u][0] + dp[v][1] * dp[u][1] + dp[v][0] * dp[u][1]) %
1000000007;
dp[v][0] = (dp[v][0] * dp[u][0] + dp[v][0] * dp[u][1]) % 1000000007;
}
}
void solve() {
dfs(0);
cout << dp[0][1] << endl;
}
int main() {
init();
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;
int n, col[123456];
long long dp[123456][2];
vector<int> G[123456];
void addedge(int x, int y) { G[x].push_back(y); }
void init() {
scanf("%d", &n);
for (int i = 0; i < n - 1; i++) {
int tmp;
scanf("%d", &tmp);
addedge(tmp, i + 1);
}
for (int i = 0; i < n; i++) scanf("%d", &col[i]);
}
void dfs(int v) {
dp[v][col[v]] = 1;
for (int i = 0; i < G[v].size(); i++) {
int u = G[v][i];
dfs(u);
dp[v][1] =
(dp[v][1] * dp[u][0] + dp[v][1] * dp[u][1] + dp[v][0] * dp[u][1]) %
1000000007;
dp[v][0] = (dp[v][0] * dp[u][0] + dp[v][0] * dp[u][1]) % 1000000007;
}
}
void solve() {
dfs(0);
cout << dp[0][1] << endl;
}
int main() {
init();
solve();
}
```
|
#include <bits/stdc++.h>
const int mod = 1e9 + 7;
int head[100005], e, n;
long long dp[100005][2];
struct E {
int to, next;
} edge[100005];
inline void addedge(int u, int v) {
edge[e].to = v;
edge[e].next = head[u];
head[u] = e++;
}
void dfs(int u) {
int i;
for (i = head[u]; i; i = edge[i].next) {
int v = edge[i].to;
dfs(v);
dp[u][1] =
(dp[u][1] * (dp[v][1] + dp[v][0]) % mod + dp[u][0] * dp[v][1] % mod) %
mod;
dp[u][0] = dp[u][0] * (dp[v][1] + dp[v][0]) % mod;
}
}
int main() {
scanf("%d", &n);
int i, tmp;
e = 1;
for (i = 1; i < n; i++) scanf("%d", &tmp), addedge(tmp, i);
for (i = 0; i < n; i++) scanf("%d", &tmp), dp[i][tmp] = 1;
dfs(0);
printf("%I64d\n", dp[0][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>
const int mod = 1e9 + 7;
int head[100005], e, n;
long long dp[100005][2];
struct E {
int to, next;
} edge[100005];
inline void addedge(int u, int v) {
edge[e].to = v;
edge[e].next = head[u];
head[u] = e++;
}
void dfs(int u) {
int i;
for (i = head[u]; i; i = edge[i].next) {
int v = edge[i].to;
dfs(v);
dp[u][1] =
(dp[u][1] * (dp[v][1] + dp[v][0]) % mod + dp[u][0] * dp[v][1] % mod) %
mod;
dp[u][0] = dp[u][0] * (dp[v][1] + dp[v][0]) % mod;
}
}
int main() {
scanf("%d", &n);
int i, tmp;
e = 1;
for (i = 1; i < n; i++) scanf("%d", &tmp), addedge(tmp, i);
for (i = 0; i < n; i++) scanf("%d", &tmp), dp[i][tmp] = 1;
dfs(0);
printf("%I64d\n", dp[0][1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 55;
vector<int> vec[N];
int m = 1e9 + 7;
long long dp1[N];
long long dp2[N];
int a[N];
void dfs(int u, int parent) {
dp1[u] = 1;
dp2[u] = 0;
for (auto v : vec[u]) {
if (v == parent) continue;
dfs(v, u);
dp2[u] *= dp1[v];
dp2[u] += dp1[u] * dp2[v];
dp1[u] *= dp1[v];
dp1[u] %= m;
dp2[u] %= m;
}
if (a[u] == 1)
dp2[u] = dp1[u];
else
dp1[u] += dp2[u];
dp1[u] %= m;
dp2[u] %= m;
}
int main() {
ios::sync_with_stdio(0);
int n;
cin >> n;
int x;
for (int i = 1; i < n; i++) {
cin >> x;
vec[i].push_back(x);
vec[x].push_back(i);
}
for (int i = 0; i < n; i++) cin >> a[i];
dfs(0, 0);
cout << dp2[0] % m << endl;
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 = 1e5 + 55;
vector<int> vec[N];
int m = 1e9 + 7;
long long dp1[N];
long long dp2[N];
int a[N];
void dfs(int u, int parent) {
dp1[u] = 1;
dp2[u] = 0;
for (auto v : vec[u]) {
if (v == parent) continue;
dfs(v, u);
dp2[u] *= dp1[v];
dp2[u] += dp1[u] * dp2[v];
dp1[u] *= dp1[v];
dp1[u] %= m;
dp2[u] %= m;
}
if (a[u] == 1)
dp2[u] = dp1[u];
else
dp1[u] += dp2[u];
dp1[u] %= m;
dp2[u] %= m;
}
int main() {
ios::sync_with_stdio(0);
int n;
cin >> n;
int x;
for (int i = 1; i < n; i++) {
cin >> x;
vec[i].push_back(x);
vec[x].push_back(i);
}
for (int i = 0; i < n; i++) cin >> a[i];
dfs(0, 0);
cout << dp2[0] % m << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const long long INF = 1e9 + 10;
const int MOD = 1e9 + 7;
long long dp[N][2], color[N];
vector<vector<int>> tree;
long long arr = 1;
void dfs(int go, int cur) {
dp[go][0] = 1;
for (auto v : tree[go]) {
if (v == cur) continue;
dfs(v, go);
dp[go][1] = (dp[go][1] * dp[v][0]) % MOD;
dp[go][1] = (dp[go][1] + (dp[v][1] * dp[go][0]) % MOD) % MOD;
dp[go][0] = (dp[go][0] * dp[v][0]) % MOD;
}
if (color[go])
dp[go][1] = dp[go][0];
else
dp[go][0] += dp[go][1];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
tree.assign(n, vector<int>(0));
for (int i = 1; i < n; i++) {
int p;
cin >> p;
tree[i].push_back(p);
tree[p].push_back(i);
}
for (int i = 0; i < n; i++) cin >> color[i];
dfs(0, 0);
cout << dp[0][1];
}
|
### 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 = 2e5 + 10;
const long long INF = 1e9 + 10;
const int MOD = 1e9 + 7;
long long dp[N][2], color[N];
vector<vector<int>> tree;
long long arr = 1;
void dfs(int go, int cur) {
dp[go][0] = 1;
for (auto v : tree[go]) {
if (v == cur) continue;
dfs(v, go);
dp[go][1] = (dp[go][1] * dp[v][0]) % MOD;
dp[go][1] = (dp[go][1] + (dp[v][1] * dp[go][0]) % MOD) % MOD;
dp[go][0] = (dp[go][0] * dp[v][0]) % MOD;
}
if (color[go])
dp[go][1] = dp[go][0];
else
dp[go][0] += dp[go][1];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
tree.assign(n, vector<int>(0));
for (int i = 1; i < n; i++) {
int p;
cin >> p;
tree[i].push_back(p);
tree[p].push_back(i);
}
for (int i = 0; i < n; i++) cin >> color[i];
dfs(0, 0);
cout << dp[0][1];
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007, inf = 1000000000;
const int N = 100010;
inline int read() {
char ch;
while (!isdigit(ch = getchar()))
;
int x = ch - '0';
while (isdigit(ch = getchar())) x = (x << 1) + (x << 3) + ch - '0';
return x;
}
int n, cnt;
int a[N], last[N];
long long f[N][2];
struct edge {
int to, next;
} e[N << 1];
void insert(int u, int v) {
e[++cnt].to = v;
e[cnt].next = last[u];
last[u] = cnt;
e[++cnt].to = u;
e[cnt].next = last[v];
last[v] = cnt;
}
void dfs(int x, int fa) {
long long t1, t2;
if (a[x] == 1) f[x][1] = 1;
f[x][0] = f[x][1] ^ 1;
for (int i = last[x]; i; i = e[i].next)
if (e[i].to != fa) {
int y = e[i].to;
dfs(y, x);
t1 = f[x][0] * (f[y][0] + f[y][1]);
t2 = (f[x][1] * (f[y][0] + f[y][1]) + f[x][0] * f[y][1]);
f[x][0] = t1 % mod;
f[x][1] = t2 % mod;
}
}
int main() {
n = read();
for (int i = 1; i < n; i++) insert(read(), i);
for (int i = 0; i < n; i++) a[i] = read();
dfs(0, 0);
cout << f[0][1] << endl;
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 long long mod = 1000000007, inf = 1000000000;
const int N = 100010;
inline int read() {
char ch;
while (!isdigit(ch = getchar()))
;
int x = ch - '0';
while (isdigit(ch = getchar())) x = (x << 1) + (x << 3) + ch - '0';
return x;
}
int n, cnt;
int a[N], last[N];
long long f[N][2];
struct edge {
int to, next;
} e[N << 1];
void insert(int u, int v) {
e[++cnt].to = v;
e[cnt].next = last[u];
last[u] = cnt;
e[++cnt].to = u;
e[cnt].next = last[v];
last[v] = cnt;
}
void dfs(int x, int fa) {
long long t1, t2;
if (a[x] == 1) f[x][1] = 1;
f[x][0] = f[x][1] ^ 1;
for (int i = last[x]; i; i = e[i].next)
if (e[i].to != fa) {
int y = e[i].to;
dfs(y, x);
t1 = f[x][0] * (f[y][0] + f[y][1]);
t2 = (f[x][1] * (f[y][0] + f[y][1]) + f[x][0] * f[y][1]);
f[x][0] = t1 % mod;
f[x][1] = t2 % mod;
}
}
int main() {
n = read();
for (int i = 1; i < n; i++) insert(read(), i);
for (int i = 0; i < n; i++) a[i] = read();
dfs(0, 0);
cout << f[0][1] << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long n, tot, e[500010], nt[500010], hd[500010], f[100010][2], p = 1e9 + 7,
a[100010];
void build(long long x, long long y) {
tot++;
e[tot] = y;
nt[tot] = hd[x];
hd[x] = tot;
}
void dfs(long long x) {
long long i;
f[x][a[x]] = 1;
for (i = hd[x]; i; i = nt[i]) {
dfs(e[i]);
f[x][1] =
(f[x][1] * (f[e[i]][0] + f[e[i]][1]) % p + f[x][0] * f[e[i]][1] % p) %
p;
f[x][0] = f[x][0] * (f[e[i]][0] + f[e[i]][1]) % p;
}
}
int main() {
long long i, x;
scanf("%lld", &n);
for (i = 2; i <= n; i++) {
scanf("%lld", &x);
x++;
build(x, i);
}
for (i = 1; i <= n; i++) scanf("%lld", &a[i]);
dfs(1);
printf("%lld", f[1][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;
long long n, tot, e[500010], nt[500010], hd[500010], f[100010][2], p = 1e9 + 7,
a[100010];
void build(long long x, long long y) {
tot++;
e[tot] = y;
nt[tot] = hd[x];
hd[x] = tot;
}
void dfs(long long x) {
long long i;
f[x][a[x]] = 1;
for (i = hd[x]; i; i = nt[i]) {
dfs(e[i]);
f[x][1] =
(f[x][1] * (f[e[i]][0] + f[e[i]][1]) % p + f[x][0] * f[e[i]][1] % p) %
p;
f[x][0] = f[x][0] * (f[e[i]][0] + f[e[i]][1]) % p;
}
}
int main() {
long long i, x;
scanf("%lld", &n);
for (i = 2; i <= n; i++) {
scanf("%lld", &x);
x++;
build(x, i);
}
for (i = 1; i <= n; i++) scanf("%lld", &a[i]);
dfs(1);
printf("%lld", f[1][1]);
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 300005;
const int M = 1000000007;
int n;
int pp[N];
vector<int> a[N];
int g[N];
int dp[N][2];
int s[N], p[N];
void dfs(int x) {
if (g[x] == 1) dp[x][1] = 1;
if (g[x] == 0) dp[x][0] = 1;
for (int i = 0; i < a[x].size(); ++i) {
int h = a[x][i];
dfs(h);
if (g[x] == 1) {
dp[x][1] =
((dp[x][1] * 1LL * dp[h][1]) % M + (dp[x][1] * 1LL * dp[h][0])) % M;
}
if (g[x] == 0) {
dp[x][0] =
((dp[x][0] * 1LL * dp[h][1]) % M + (dp[x][0] * 1LL * dp[h][0])) % M;
}
}
for (int i = 0; i < a[x].size(); ++i) {
int h = a[x][i];
if (i == 0)
p[i] = (dp[h][0] + dp[h][1]) % M;
else
p[i] = (p[i - 1] * 1LL * (dp[h][0] + dp[h][1]) % M) % M;
}
for (int i = (int)a[x].size() - 1; i >= 0; --i) {
int h = a[x][i];
if (i == (int)a[x].size() - 1)
s[i] = (dp[h][0] + dp[h][1]) % M;
else
s[i] = (s[i + 1] * 1LL * (dp[h][0] + dp[h][1]) % M) % M;
}
if (g[x] == 0) {
for (int i = 0; i < a[x].size(); ++i) {
int h = a[x][i];
int u = 1;
if (i > 0) u = (u * 1LL * p[i - 1]) % M;
if (i < (int)a[x].size() - 1) u = (u * 1LL * s[i + 1]) % M;
dp[x][1] += (dp[h][1] * 1LL * u) % M;
dp[x][1] %= M;
}
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; ++i) {
scanf("%d", &pp[i]);
a[pp[i]].push_back(i);
}
for (int i = 0; i < n; ++i) scanf("%d", &g[i]);
dfs(0);
printf("%d\n", 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 int N = 300005;
const int M = 1000000007;
int n;
int pp[N];
vector<int> a[N];
int g[N];
int dp[N][2];
int s[N], p[N];
void dfs(int x) {
if (g[x] == 1) dp[x][1] = 1;
if (g[x] == 0) dp[x][0] = 1;
for (int i = 0; i < a[x].size(); ++i) {
int h = a[x][i];
dfs(h);
if (g[x] == 1) {
dp[x][1] =
((dp[x][1] * 1LL * dp[h][1]) % M + (dp[x][1] * 1LL * dp[h][0])) % M;
}
if (g[x] == 0) {
dp[x][0] =
((dp[x][0] * 1LL * dp[h][1]) % M + (dp[x][0] * 1LL * dp[h][0])) % M;
}
}
for (int i = 0; i < a[x].size(); ++i) {
int h = a[x][i];
if (i == 0)
p[i] = (dp[h][0] + dp[h][1]) % M;
else
p[i] = (p[i - 1] * 1LL * (dp[h][0] + dp[h][1]) % M) % M;
}
for (int i = (int)a[x].size() - 1; i >= 0; --i) {
int h = a[x][i];
if (i == (int)a[x].size() - 1)
s[i] = (dp[h][0] + dp[h][1]) % M;
else
s[i] = (s[i + 1] * 1LL * (dp[h][0] + dp[h][1]) % M) % M;
}
if (g[x] == 0) {
for (int i = 0; i < a[x].size(); ++i) {
int h = a[x][i];
int u = 1;
if (i > 0) u = (u * 1LL * p[i - 1]) % M;
if (i < (int)a[x].size() - 1) u = (u * 1LL * s[i + 1]) % M;
dp[x][1] += (dp[h][1] * 1LL * u) % M;
dp[x][1] %= M;
}
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; ++i) {
scanf("%d", &pp[i]);
a[pp[i]].push_back(i);
}
for (int i = 0; i < n; ++i) scanf("%d", &g[i]);
dfs(0);
printf("%d\n", dp[0][1]);
return 0;
}
```
|
#include <bits/stdc++.h>
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
using namespace std;
const long long INF = 1e10;
struct Edge {
long long v = 0, u = 0, q = 0;
Edge() {}
Edge(int v1, int u1) {
v = v1;
u = u1;
}
bool operator<(const Edge &p) const { return tie(v, u) < tie(p.v, p.u); }
};
vector<vector<int>> g;
vector<int> a;
vector<int> used;
vector<Edge> dp;
const long long MOD = 1e9 + 7;
void dfs(int v) {
used[v] = 1;
dp[v].v = 1;
for (auto it : g[v]) {
if (!used[it]) {
dfs(it);
dp[v].u = ((dp[v].u * dp[it].v) % MOD + (dp[v].v * dp[it].u) % MOD) % MOD;
dp[v].v = (dp[v].v * dp[it].v) % MOD;
}
}
if (a[v] == 1)
dp[v].u = dp[v].v;
else
dp[v].v = (dp[v].v + dp[v].u) % MOD;
}
int main() {
int n;
cin >> n;
g.resize(n);
a.resize(n);
dp.assign(n, Edge(0, 0));
used.assign(n, 0);
for (int i = 0; i < n - 1; ++i) {
int t;
cin >> t;
g[t].push_back(i + 1);
}
for (int i = 0; i < n; ++i) cin >> a[i];
dfs(0);
cout << dp[0].u;
}
|
### 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>
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
using namespace std;
const long long INF = 1e10;
struct Edge {
long long v = 0, u = 0, q = 0;
Edge() {}
Edge(int v1, int u1) {
v = v1;
u = u1;
}
bool operator<(const Edge &p) const { return tie(v, u) < tie(p.v, p.u); }
};
vector<vector<int>> g;
vector<int> a;
vector<int> used;
vector<Edge> dp;
const long long MOD = 1e9 + 7;
void dfs(int v) {
used[v] = 1;
dp[v].v = 1;
for (auto it : g[v]) {
if (!used[it]) {
dfs(it);
dp[v].u = ((dp[v].u * dp[it].v) % MOD + (dp[v].v * dp[it].u) % MOD) % MOD;
dp[v].v = (dp[v].v * dp[it].v) % MOD;
}
}
if (a[v] == 1)
dp[v].u = dp[v].v;
else
dp[v].v = (dp[v].v + dp[v].u) % MOD;
}
int main() {
int n;
cin >> n;
g.resize(n);
a.resize(n);
dp.assign(n, Edge(0, 0));
used.assign(n, 0);
for (int i = 0; i < n - 1; ++i) {
int t;
cin >> t;
g[t].push_back(i + 1);
}
for (int i = 0; i < n; ++i) cin >> a[i];
dfs(0);
cout << dp[0].u;
}
```
|
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:33554432")
const int MOD = 1e9 + 7;
int N, K, A[100005], par[100005];
int D[100005][2];
vector<int> con[100005];
int inv(int n) {
int ret = 1, v = n;
for (int i = 0; i < 30; i++, v = (long long)v * v % MOD)
if (((MOD - 2) >> i) & 1) {
ret = (long long)ret * v % MOD;
}
return ret;
}
void dfs(int n) {
int i, k;
D[n][0] = 1;
for (i = ((int)(con[n]).size()); i--;) {
k = con[n][i];
dfs(k);
D[n][0] = (long long)D[n][0] * D[k][0] % MOD;
}
if (A[n])
D[n][1] = D[n][0];
else {
for (i = ((int)(con[n]).size()); i--;) {
k = con[n][i];
int d = (long long)D[n][0] * inv(D[k][0]) % MOD;
D[n][1] = (D[n][1] + (long long)D[k][1] * d % MOD) % MOD;
}
D[n][0] = (D[n][0] + (D[n][1])) % (MOD);
}
}
int main() {
int i, j, k;
scanf("%d", &N);
for (i = 2; i <= N; i++)
scanf("%d", par + i), ++par[i], con[par[i]].push_back(i);
for (i = 1; i <= N; i++) scanf("%d", A + i), K += A[i];
dfs(1);
printf("%d\n", D[1][1]);
}
|
### 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;
#pragma comment(linker, "/STACK:33554432")
const int MOD = 1e9 + 7;
int N, K, A[100005], par[100005];
int D[100005][2];
vector<int> con[100005];
int inv(int n) {
int ret = 1, v = n;
for (int i = 0; i < 30; i++, v = (long long)v * v % MOD)
if (((MOD - 2) >> i) & 1) {
ret = (long long)ret * v % MOD;
}
return ret;
}
void dfs(int n) {
int i, k;
D[n][0] = 1;
for (i = ((int)(con[n]).size()); i--;) {
k = con[n][i];
dfs(k);
D[n][0] = (long long)D[n][0] * D[k][0] % MOD;
}
if (A[n])
D[n][1] = D[n][0];
else {
for (i = ((int)(con[n]).size()); i--;) {
k = con[n][i];
int d = (long long)D[n][0] * inv(D[k][0]) % MOD;
D[n][1] = (D[n][1] + (long long)D[k][1] * d % MOD) % MOD;
}
D[n][0] = (D[n][0] + (D[n][1])) % (MOD);
}
}
int main() {
int i, j, k;
scanf("%d", &N);
for (i = 2; i <= N; i++)
scanf("%d", par + i), ++par[i], con[par[i]].push_back(i);
for (i = 1; i <= N; i++) scanf("%d", A + i), K += A[i];
dfs(1);
printf("%d\n", D[1][1]);
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 101000;
const long long MOD = 1e9 + 7;
vector<int> v[MAX_N];
int col[MAX_N];
long long dp[MAX_N][2];
bool have[MAX_N];
long long ans = 1;
void dfs(int x, int fa) {
int i;
dp[x][0] = 1;
dp[x][1] = 0;
for (i = 0; i < v[x].size(); i++) {
int y = v[x][i];
if (y == fa) continue;
dfs(y, x);
dp[x][1] = (dp[x][1] * dp[y][0] + dp[x][0] * dp[y][1]) % MOD;
dp[x][0] = dp[x][0] * dp[y][0] % MOD;
}
if (col[x] == 1)
dp[x][1] = dp[x][0];
else
dp[x][0] = (dp[x][1] + dp[x][0]) % MOD;
}
int main(void) {
int n, x, i;
scanf("%d", &n);
for (i = 1; i < n; i++) {
scanf("%d", &x);
x++;
v[x].push_back(i + 1);
v[i + 1].push_back(x);
}
for (i = 1; i <= n; i++) {
scanf("%d", &col[i]);
}
dfs(1, 0);
cout << dp[1][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 int MAX_N = 101000;
const long long MOD = 1e9 + 7;
vector<int> v[MAX_N];
int col[MAX_N];
long long dp[MAX_N][2];
bool have[MAX_N];
long long ans = 1;
void dfs(int x, int fa) {
int i;
dp[x][0] = 1;
dp[x][1] = 0;
for (i = 0; i < v[x].size(); i++) {
int y = v[x][i];
if (y == fa) continue;
dfs(y, x);
dp[x][1] = (dp[x][1] * dp[y][0] + dp[x][0] * dp[y][1]) % MOD;
dp[x][0] = dp[x][0] * dp[y][0] % MOD;
}
if (col[x] == 1)
dp[x][1] = dp[x][0];
else
dp[x][0] = (dp[x][1] + dp[x][0]) % MOD;
}
int main(void) {
int n, x, i;
scanf("%d", &n);
for (i = 1; i < n; i++) {
scanf("%d", &x);
x++;
v[x].push_back(i + 1);
v[i + 1].push_back(x);
}
for (i = 1; i <= n; i++) {
scanf("%d", &col[i]);
}
dfs(1, 0);
cout << dp[1][1] << "\n";
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
int n, a[100005];
vector<int> edges[100005];
long long dp[100005][2];
void solve(int x, int px) {
dp[x][0] = 1LL;
dp[x][1] = 0LL;
for (int i = 0; i < edges[x].size(); i++) {
int y = edges[x][i];
if (y == px) continue;
solve(y, x);
dp[x][1] *= dp[y][0];
dp[x][1] %= mod;
dp[x][1] += dp[x][0] * dp[y][1];
dp[x][1] %= mod;
dp[x][0] *= dp[y][0];
dp[x][0] %= mod;
}
if (a[x] == 1) {
dp[x][1] = dp[x][0];
} else {
dp[x][0] += dp[x][1];
dp[x][0] %= mod;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
;
cin >> n;
for (int i = 1; i < n; i++) {
int x;
cin >> x;
edges[x].push_back(i);
edges[i].push_back(x);
}
for (int i = 0; i < n; i++) {
cin >> a[i];
}
solve(0, -1);
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 mod = 1e9 + 7;
int n, a[100005];
vector<int> edges[100005];
long long dp[100005][2];
void solve(int x, int px) {
dp[x][0] = 1LL;
dp[x][1] = 0LL;
for (int i = 0; i < edges[x].size(); i++) {
int y = edges[x][i];
if (y == px) continue;
solve(y, x);
dp[x][1] *= dp[y][0];
dp[x][1] %= mod;
dp[x][1] += dp[x][0] * dp[y][1];
dp[x][1] %= mod;
dp[x][0] *= dp[y][0];
dp[x][0] %= mod;
}
if (a[x] == 1) {
dp[x][1] = dp[x][0];
} else {
dp[x][0] += dp[x][1];
dp[x][0] %= mod;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
;
cin >> n;
for (int i = 1; i < n; i++) {
int x;
cin >> x;
edges[x].push_back(i);
edges[i].push_back(x);
}
for (int i = 0; i < n; i++) {
cin >> a[i];
}
solve(0, -1);
cout << dp[0][1] << "\n";
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long a[int(1e6 + 6)], i, m, ans, k, l, j, x, n, ma, mi;
string s;
long long d[int(1e6 + 6)][2];
vector<long long> v[int(1e6 + 6)];
void go(int u) {
d[u][0] = 1;
d[u][1] = 0;
for (int i = 0; i < v[u].size(); i++) {
go(v[u][i]);
int X = v[u][i];
d[u][1] *= d[X][0];
d[u][1] %= int(1e9) + 7;
d[u][1] += d[u][0] * d[X][1];
d[u][1] %= int(1e9) + 7;
d[u][0] *= d[X][0];
d[u][0] %= int(1e9) + 7;
}
if (a[u] == 1)
d[u][1] = d[u][0];
else
d[u][0] += d[u][1];
d[u][1] %= int(1e9) + 7;
d[u][0] %= int(1e9) + 7;
}
int main() {
cin >> n;
for (int i = 2; i <= n; i++) {
cin >> x;
x++;
v[x].push_back(i);
}
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
go(1);
cout << d[1][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;
long long a[int(1e6 + 6)], i, m, ans, k, l, j, x, n, ma, mi;
string s;
long long d[int(1e6 + 6)][2];
vector<long long> v[int(1e6 + 6)];
void go(int u) {
d[u][0] = 1;
d[u][1] = 0;
for (int i = 0; i < v[u].size(); i++) {
go(v[u][i]);
int X = v[u][i];
d[u][1] *= d[X][0];
d[u][1] %= int(1e9) + 7;
d[u][1] += d[u][0] * d[X][1];
d[u][1] %= int(1e9) + 7;
d[u][0] *= d[X][0];
d[u][0] %= int(1e9) + 7;
}
if (a[u] == 1)
d[u][1] = d[u][0];
else
d[u][0] += d[u][1];
d[u][1] %= int(1e9) + 7;
d[u][0] %= int(1e9) + 7;
}
int main() {
cin >> n;
for (int i = 2; i <= n; i++) {
cin >> x;
x++;
v[x].push_back(i);
}
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
go(1);
cout << d[1][1] << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e5 + 5;
const long long mod = 1e9 + 7;
vector<long long> AdjList[maxn];
long long dp[2][maxn];
long long col[maxn];
long long binpow(long long a, long long b) {
if (b == 0) return 1;
long long x = binpow(a, b / 2);
x = (x * x) % mod;
if (b % 2 == 0)
return x;
else
return (x * a) % mod;
}
long long bindiv(long long a, long long b) {
long long inv = binpow(b, mod - 2);
return (a * inv) % mod;
}
void dfs(long long s, long long p) {
for (long long u : AdjList[s]) {
if (u != p) {
dfs(u, s);
}
}
if (col[s] == 1) {
long long tmp1 = 1;
long long tmp2 = 0;
dp[0][s] = 0;
for (long long u : AdjList[s]) {
if (u != p) tmp1 = (tmp1 * dp[0][u] % mod + tmp1 * dp[1][u] % mod) % mod;
}
dp[1][s] = tmp1;
} else {
long long tmp1 = 1;
long long tmp2 = 0;
for (long long u : AdjList[s]) {
if (u != p) {
tmp2 = (tmp2 * dp[0][u] % mod + tmp2 * dp[1][u] % mod +
tmp1 * dp[1][u] % mod) %
mod;
tmp1 = (tmp1 * dp[0][u] % mod + tmp1 * dp[1][u] % mod) % mod;
}
}
dp[1][s] = tmp2;
dp[0][s] = tmp1;
}
}
signed main() {
long long n;
cin >> n;
for (long long i = 1; i < n; i++) {
long long p;
cin >> p;
AdjList[p].push_back(i);
AdjList[i].push_back(p);
}
for (long long i = 0; i < n; i++) {
cin >> col[i];
dp[0][i] = dp[1][i] = 0;
}
dfs(0, 0);
cout << (dp[1][0]) % 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 long long maxn = 1e5 + 5;
const long long mod = 1e9 + 7;
vector<long long> AdjList[maxn];
long long dp[2][maxn];
long long col[maxn];
long long binpow(long long a, long long b) {
if (b == 0) return 1;
long long x = binpow(a, b / 2);
x = (x * x) % mod;
if (b % 2 == 0)
return x;
else
return (x * a) % mod;
}
long long bindiv(long long a, long long b) {
long long inv = binpow(b, mod - 2);
return (a * inv) % mod;
}
void dfs(long long s, long long p) {
for (long long u : AdjList[s]) {
if (u != p) {
dfs(u, s);
}
}
if (col[s] == 1) {
long long tmp1 = 1;
long long tmp2 = 0;
dp[0][s] = 0;
for (long long u : AdjList[s]) {
if (u != p) tmp1 = (tmp1 * dp[0][u] % mod + tmp1 * dp[1][u] % mod) % mod;
}
dp[1][s] = tmp1;
} else {
long long tmp1 = 1;
long long tmp2 = 0;
for (long long u : AdjList[s]) {
if (u != p) {
tmp2 = (tmp2 * dp[0][u] % mod + tmp2 * dp[1][u] % mod +
tmp1 * dp[1][u] % mod) %
mod;
tmp1 = (tmp1 * dp[0][u] % mod + tmp1 * dp[1][u] % mod) % mod;
}
}
dp[1][s] = tmp2;
dp[0][s] = tmp1;
}
}
signed main() {
long long n;
cin >> n;
for (long long i = 1; i < n; i++) {
long long p;
cin >> p;
AdjList[p].push_back(i);
AdjList[i].push_back(p);
}
for (long long i = 0; i < n; i++) {
cin >> col[i];
dp[0][i] = dp[1][i] = 0;
}
dfs(0, 0);
cout << (dp[1][0]) % mod;
}
```
|
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long x = 0, w = 1;
char ch = getchar();
while (ch > '9' || ch < '0') {
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
return x * w;
}
inline void write(long long x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
const long long maxn = 1e5 + 5;
const long long mod = 1e9 + 7;
const long long inf = 1e9;
inline long long min(long long x, long long y) { return x < y ? x : y; }
inline long long max(long long x, long long y) { return x > y ? x : y; }
struct node {
long long nxt, to;
} edge[maxn << 1];
long long n, tot, head[maxn], f[maxn], g[maxn];
inline void addedge(long long u, long long v) {
edge[++tot] = (node){head[u], v}, head[u] = tot;
}
inline void dfs(long long u, long long fath) {
for (long long i = head[u]; i; i = edge[i].nxt) {
long long v = edge[i].to;
if (v == fath) continue;
dfs(v, u);
f[u] = (f[u] * (f[v] + g[v]) % mod + g[u] * f[v] % mod) % mod;
g[u] = g[u] * (f[v] + g[v]) % mod;
}
}
signed main(void) {
n = read();
for (long long i = 2, fath; i <= n; i++) {
fath = read() + 1;
addedge(fath, i), addedge(i, fath);
}
for (long long i = 1, col; i <= n; i++) {
col = read();
if (col == 1)
f[i] = 1;
else
g[i] = 1;
}
dfs(1, 0);
printf("%lld\n", f[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;
inline long long read() {
long long x = 0, w = 1;
char ch = getchar();
while (ch > '9' || ch < '0') {
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
return x * w;
}
inline void write(long long x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
const long long maxn = 1e5 + 5;
const long long mod = 1e9 + 7;
const long long inf = 1e9;
inline long long min(long long x, long long y) { return x < y ? x : y; }
inline long long max(long long x, long long y) { return x > y ? x : y; }
struct node {
long long nxt, to;
} edge[maxn << 1];
long long n, tot, head[maxn], f[maxn], g[maxn];
inline void addedge(long long u, long long v) {
edge[++tot] = (node){head[u], v}, head[u] = tot;
}
inline void dfs(long long u, long long fath) {
for (long long i = head[u]; i; i = edge[i].nxt) {
long long v = edge[i].to;
if (v == fath) continue;
dfs(v, u);
f[u] = (f[u] * (f[v] + g[v]) % mod + g[u] * f[v] % mod) % mod;
g[u] = g[u] * (f[v] + g[v]) % mod;
}
}
signed main(void) {
n = read();
for (long long i = 2, fath; i <= n; i++) {
fath = read() + 1;
addedge(fath, i), addedge(i, fath);
}
for (long long i = 1, col; i <= n; i++) {
col = read();
if (col == 1)
f[i] = 1;
else
g[i] = 1;
}
dfs(1, 0);
printf("%lld\n", f[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int base = 1000000007;
const double pi = acos(-1);
vector<int> ad[100100];
int n, f1[100100], f0[100100], F1[100100], F0[100100], top, SR[100100],
SL[100100];
int color[100100];
void visit(int pa, int i) {
bool ok = 0;
for (int j = 0; j <= (int)ad[i].size() - 1; j++)
if (ad[i][j] != pa) {
visit(i, ad[i][j]);
ok = 1;
}
if (ok == 0) {
if (color[i]) {
f1[i] = 1;
f0[i] = 0;
} else {
f1[i] = 0;
f0[i] = 1;
}
return;
}
if (color[i])
f0[i] = 0;
else {
f0[i] = 1;
for (int j = 0; j <= (int)ad[i].size() - 1; j++)
if (ad[i][j] != pa) {
int k = ad[i][j];
f0[i] = (long long)f0[i] * (f0[k] + f1[k]) % base;
}
}
if (color[i]) {
f1[i] = 1;
for (int j = 0; j <= (int)ad[i].size() - 1; j++)
if (ad[i][j] != pa) {
int k = ad[i][j];
f1[i] = (long long)f1[i] * (f0[k] + f1[k]) % base;
}
} else {
top = 0;
for (int j = 0; j <= (int)ad[i].size() - 1; j++)
if (ad[i][j] != pa) {
F1[++top] = f1[ad[i][j]];
F0[top] = f0[ad[i][j]];
}
SL[0] = 1;
for (int j = 1; j <= top; j++)
SL[j] = (long long)SL[j - 1] * (F1[j] + F0[j]) % base;
SR[top + 1] = 1;
for (int j = top; j >= 1; j--)
SR[j] = (long long)SR[j + 1] * (F1[j] + F0[j]) % base;
f1[i] = 0;
for (int j = 1; j <= top; j++) {
int x = ((long long)F1[j] * SL[j - 1] % base) * SR[j + 1] % base;
f1[i] = (f1[i] + x) % base;
}
}
}
void solve() {
visit(0, 1);
cout << f1[1];
}
int main() {
cin >> n;
for (int i = 2; i <= n; i++) {
int x;
cin >> x;
x++;
ad[x].push_back(i);
ad[i].push_back(x);
}
for (int i = 1; i <= n; i++) scanf("%d", &color[i]);
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 base = 1000000007;
const double pi = acos(-1);
vector<int> ad[100100];
int n, f1[100100], f0[100100], F1[100100], F0[100100], top, SR[100100],
SL[100100];
int color[100100];
void visit(int pa, int i) {
bool ok = 0;
for (int j = 0; j <= (int)ad[i].size() - 1; j++)
if (ad[i][j] != pa) {
visit(i, ad[i][j]);
ok = 1;
}
if (ok == 0) {
if (color[i]) {
f1[i] = 1;
f0[i] = 0;
} else {
f1[i] = 0;
f0[i] = 1;
}
return;
}
if (color[i])
f0[i] = 0;
else {
f0[i] = 1;
for (int j = 0; j <= (int)ad[i].size() - 1; j++)
if (ad[i][j] != pa) {
int k = ad[i][j];
f0[i] = (long long)f0[i] * (f0[k] + f1[k]) % base;
}
}
if (color[i]) {
f1[i] = 1;
for (int j = 0; j <= (int)ad[i].size() - 1; j++)
if (ad[i][j] != pa) {
int k = ad[i][j];
f1[i] = (long long)f1[i] * (f0[k] + f1[k]) % base;
}
} else {
top = 0;
for (int j = 0; j <= (int)ad[i].size() - 1; j++)
if (ad[i][j] != pa) {
F1[++top] = f1[ad[i][j]];
F0[top] = f0[ad[i][j]];
}
SL[0] = 1;
for (int j = 1; j <= top; j++)
SL[j] = (long long)SL[j - 1] * (F1[j] + F0[j]) % base;
SR[top + 1] = 1;
for (int j = top; j >= 1; j--)
SR[j] = (long long)SR[j + 1] * (F1[j] + F0[j]) % base;
f1[i] = 0;
for (int j = 1; j <= top; j++) {
int x = ((long long)F1[j] * SL[j - 1] % base) * SR[j + 1] % base;
f1[i] = (f1[i] + x) % base;
}
}
}
void solve() {
visit(0, 1);
cout << f1[1];
}
int main() {
cin >> n;
for (int i = 2; i <= n; i++) {
int x;
cin >> x;
x++;
ad[x].push_back(i);
ad[i].push_back(x);
}
for (int i = 1; i <= n; i++) scanf("%d", &color[i]);
solve();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 100010, M = N << 1;
const int MOD = 1000000007;
int n, c[N], son[N];
int head[N], pre[M], nxt[M], e;
long long dp[N][2], ans;
void init() {
memset(head, -1, sizeof(head));
e = 0;
}
void addedge(int u, int v) { pre[e] = v, nxt[e] = head[u], head[u] = e++; }
void dfs(int u, int f) {
son[u] = c[u];
dp[u][0] = c[u] ? 0 : 1;
dp[u][1] = c[u] ? 1 : 0;
for (int i = head[u]; ~i; i = nxt[i]) {
int v = pre[i];
if (v == f) continue;
dfs(v, u);
if (son[v] == 0) continue;
if (c[u] == 1) {
if (c[v] == 1)
ans = ans * dp[v][1] % MOD;
else
dp[u][1] = dp[u][1] * (dp[v][0] + dp[v][1]) % MOD;
} else {
dp[u][1] = dp[u][1] * (dp[v][0] + dp[v][1]) % MOD;
dp[u][1] = (dp[u][1] + dp[u][0] * dp[v][1]) % MOD;
dp[u][0] = dp[u][0] * (dp[v][0] + dp[v][1]) % MOD;
}
son[u] += son[v];
}
}
int main() {
scanf("%d", &n);
int p;
init();
for (int i = 1; i < n; ++i) {
scanf("%d", &p);
addedge(p, i);
addedge(i, p);
}
ans = 1;
bool flag = true;
for (int i = 0; i < n; ++i) {
scanf("%d", &c[i]);
if (c[i] == 1) flag = false;
}
if (flag) {
puts("1");
return 0;
}
for (int i = 0; i < n; ++i)
if (c[i] == 1) {
dfs(i, -1);
cout << ans * dp[i][1] % MOD << endl;
break;
}
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 N = 100010, M = N << 1;
const int MOD = 1000000007;
int n, c[N], son[N];
int head[N], pre[M], nxt[M], e;
long long dp[N][2], ans;
void init() {
memset(head, -1, sizeof(head));
e = 0;
}
void addedge(int u, int v) { pre[e] = v, nxt[e] = head[u], head[u] = e++; }
void dfs(int u, int f) {
son[u] = c[u];
dp[u][0] = c[u] ? 0 : 1;
dp[u][1] = c[u] ? 1 : 0;
for (int i = head[u]; ~i; i = nxt[i]) {
int v = pre[i];
if (v == f) continue;
dfs(v, u);
if (son[v] == 0) continue;
if (c[u] == 1) {
if (c[v] == 1)
ans = ans * dp[v][1] % MOD;
else
dp[u][1] = dp[u][1] * (dp[v][0] + dp[v][1]) % MOD;
} else {
dp[u][1] = dp[u][1] * (dp[v][0] + dp[v][1]) % MOD;
dp[u][1] = (dp[u][1] + dp[u][0] * dp[v][1]) % MOD;
dp[u][0] = dp[u][0] * (dp[v][0] + dp[v][1]) % MOD;
}
son[u] += son[v];
}
}
int main() {
scanf("%d", &n);
int p;
init();
for (int i = 1; i < n; ++i) {
scanf("%d", &p);
addedge(p, i);
addedge(i, p);
}
ans = 1;
bool flag = true;
for (int i = 0; i < n; ++i) {
scanf("%d", &c[i]);
if (c[i] == 1) flag = false;
}
if (flag) {
puts("1");
return 0;
}
for (int i = 0; i < n; ++i)
if (c[i] == 1) {
dfs(i, -1);
cout << ans * dp[i][1] % MOD << endl;
break;
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
static const long long PRIME = 1000000007;
static const long long N = 100000;
long long inv(long long n, long long p) {
n = n % p;
long long r = 1;
long long pow = p - 2;
while (pow > 0) {
if (pow & 1) {
r = (r * n) % p;
}
pow = pow >> 1;
n = (n * n) % p;
}
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]; }
const T& at(T x, T y) const { 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 = ((get(0, child) + get(1, child)) * result) % PRIME;
}
} 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 = ((get(0, child) + get(1, child)) * result) % PRIME;
}
} else if (a == 1 && colors[b] == 0 && children[b].size() == 0) {
result = 0;
} else if (a == 1 && colors[b] == 0 && children[b].size() == 1) {
result = get(1, children[b][0]);
} else if (a == 1 && colors[b] == 0 && children[b].size() > 1) {
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 = (d0 + d1) % PRIME;
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 (int i = 0; i < children[b].size(); i++) {
prod = (prod * pairlist[i]) % PRIME;
long long term = (d1list[i] * inv(pairlist[i], PRIME)) % PRIME;
sum = (sum + term) % PRIME;
}
result = (sum * prod) % PRIME;
} else {
long long prod = 1;
for (const long long& j : children[b]) {
if (j != zeroindex) {
prod = (prod * pairlist[j]) % PRIME;
} else {
prod = (prod * d1list[j]) % PRIME;
}
}
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);
{
vector<vector<long long>> adj(n);
for (long long i = 0; i < n - 1; i++) {
long long v;
scanf("%lld", &v);
long long u = i + 1;
adj[u].push_back(v);
adj[v].push_back(u);
}
vector<bool> marked(n);
marked[0] = true;
queue<long long> qu;
qu.push(0);
while (!qu.empty()) {
long long v = qu.front();
for (const long long& u : adj[v]) {
if (!marked[u]) {
marked[u] = true;
children[v].push_back(u);
qu.push(u);
}
}
qu.pop();
}
}
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
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;
static const long long PRIME = 1000000007;
static const long long N = 100000;
long long inv(long long n, long long p) {
n = n % p;
long long r = 1;
long long pow = p - 2;
while (pow > 0) {
if (pow & 1) {
r = (r * n) % p;
}
pow = pow >> 1;
n = (n * n) % p;
}
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]; }
const T& at(T x, T y) const { 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 = ((get(0, child) + get(1, child)) * result) % PRIME;
}
} 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 = ((get(0, child) + get(1, child)) * result) % PRIME;
}
} else if (a == 1 && colors[b] == 0 && children[b].size() == 0) {
result = 0;
} else if (a == 1 && colors[b] == 0 && children[b].size() == 1) {
result = get(1, children[b][0]);
} else if (a == 1 && colors[b] == 0 && children[b].size() > 1) {
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 = (d0 + d1) % PRIME;
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 (int i = 0; i < children[b].size(); i++) {
prod = (prod * pairlist[i]) % PRIME;
long long term = (d1list[i] * inv(pairlist[i], PRIME)) % PRIME;
sum = (sum + term) % PRIME;
}
result = (sum * prod) % PRIME;
} else {
long long prod = 1;
for (const long long& j : children[b]) {
if (j != zeroindex) {
prod = (prod * pairlist[j]) % PRIME;
} else {
prod = (prod * d1list[j]) % PRIME;
}
}
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);
{
vector<vector<long long>> adj(n);
for (long long i = 0; i < n - 1; i++) {
long long v;
scanf("%lld", &v);
long long u = i + 1;
adj[u].push_back(v);
adj[v].push_back(u);
}
vector<bool> marked(n);
marked[0] = true;
queue<long long> qu;
qu.push(0);
while (!qu.empty()) {
long long v = qu.front();
for (const long long& u : adj[v]) {
if (!marked[u]) {
marked[u] = true;
children[v].push_back(u);
qu.push(u);
}
}
qu.pop();
}
}
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;
void RI(int& x) {
x = 0;
char c = getchar();
while (!(c >= '0' && c <= '9' || c == '-')) c = getchar();
bool flag = 1;
if (c == '-') {
flag = 0;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
if (!flag) x = -x;
}
void RII(int& x, int& y) { RI(x), RI(y); }
void RIII(int& x, int& y, int& z) { RI(x), RI(y), RI(z); }
void RC(char& c) {
c = getchar();
while (c == ' ' || c == '\n') c = getchar();
}
char RC() {
char c = getchar();
while (c == ' ' || c == '\n') c = getchar();
return c;
}
const long long mod = 1e9 + 7;
const long long LINF = 1e18;
const int INF = 1e9;
const double EPS = 1e-8;
const int N = 200005;
long long POW(int n, int m) {
if (m == 0) return 1;
if (m == 1) return n;
long long tmp = POW(n, m / 2);
if (m & 1) return tmp * tmp % mod * n % mod;
return tmp * tmp % mod;
}
vector<int> g[N];
long long dp[N][2];
int a[N];
void dfs(int u, int fa) {
for (int i = 0; i <= (int)g[u].size() - 1; i++) {
int v = g[u][i];
if (v != fa) {
dfs(v, u);
}
}
if (a[u] == 1) {
dp[u][0] = 0;
dp[u][1] = 1;
for (int i = 0; i <= (int)g[u].size() - 1; i++) {
int v = g[u][i];
if (v != fa) {
dp[u][1] = (dp[u][1] * (dp[v][1] + dp[v][0])) % mod;
}
}
} else {
dp[u][0] = 1;
dp[u][1] = 0;
for (int i = 0; i <= (int)g[u].size() - 1; i++) {
int v = g[u][i];
if (v != fa) {
dp[u][0] = dp[u][0] * (dp[v][0] + dp[v][1]) % mod;
}
}
for (int i = 0; i <= (int)g[u].size() - 1; i++) {
int v = g[u][i];
if (v != fa) {
dp[u][1] = (dp[u][1] + dp[u][0] * POW(dp[v][0] + dp[v][1], mod - 2) %
mod * dp[v][1]) %
mod;
}
}
}
}
int main() {
int n;
RI(n);
for (int i = 2; i <= n; i++) {
int t;
RI(t);
g[i].push_back(t + 1);
g[t + 1].push_back(i);
}
for (int i = 1; i <= n; i++) {
RI(a[i]);
}
dfs(1, 0);
cout << dp[1][1] << 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;
void RI(int& x) {
x = 0;
char c = getchar();
while (!(c >= '0' && c <= '9' || c == '-')) c = getchar();
bool flag = 1;
if (c == '-') {
flag = 0;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
if (!flag) x = -x;
}
void RII(int& x, int& y) { RI(x), RI(y); }
void RIII(int& x, int& y, int& z) { RI(x), RI(y), RI(z); }
void RC(char& c) {
c = getchar();
while (c == ' ' || c == '\n') c = getchar();
}
char RC() {
char c = getchar();
while (c == ' ' || c == '\n') c = getchar();
return c;
}
const long long mod = 1e9 + 7;
const long long LINF = 1e18;
const int INF = 1e9;
const double EPS = 1e-8;
const int N = 200005;
long long POW(int n, int m) {
if (m == 0) return 1;
if (m == 1) return n;
long long tmp = POW(n, m / 2);
if (m & 1) return tmp * tmp % mod * n % mod;
return tmp * tmp % mod;
}
vector<int> g[N];
long long dp[N][2];
int a[N];
void dfs(int u, int fa) {
for (int i = 0; i <= (int)g[u].size() - 1; i++) {
int v = g[u][i];
if (v != fa) {
dfs(v, u);
}
}
if (a[u] == 1) {
dp[u][0] = 0;
dp[u][1] = 1;
for (int i = 0; i <= (int)g[u].size() - 1; i++) {
int v = g[u][i];
if (v != fa) {
dp[u][1] = (dp[u][1] * (dp[v][1] + dp[v][0])) % mod;
}
}
} else {
dp[u][0] = 1;
dp[u][1] = 0;
for (int i = 0; i <= (int)g[u].size() - 1; i++) {
int v = g[u][i];
if (v != fa) {
dp[u][0] = dp[u][0] * (dp[v][0] + dp[v][1]) % mod;
}
}
for (int i = 0; i <= (int)g[u].size() - 1; i++) {
int v = g[u][i];
if (v != fa) {
dp[u][1] = (dp[u][1] + dp[u][0] * POW(dp[v][0] + dp[v][1], mod - 2) %
mod * dp[v][1]) %
mod;
}
}
}
}
int main() {
int n;
RI(n);
for (int i = 2; i <= n; i++) {
int t;
RI(t);
g[i].push_back(t + 1);
g[t + 1].push_back(i);
}
for (int i = 1; i <= n; i++) {
RI(a[i]);
}
dfs(1, 0);
cout << dp[1][1] << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > vv;
long long dp[200001][2];
const int INF = 1e9 + 7;
vector<int> a;
void dfs(int v, int p) {
dp[v][0] = 1;
dp[v][1] = 0;
for (int i = 0; i < vv[v].size(); i++) {
int to = vv[v][i];
if (to != p) {
dfs(to, v);
dp[v][1] *= dp[to][0];
dp[v][1] %= INF;
dp[v][1] += (dp[v][0] * dp[to][1]) % INF;
dp[v][1] %= INF;
dp[v][0] *= dp[to][0];
dp[v][0] %= INF;
}
}
if (a[v] == 1)
dp[v][1] = dp[v][0];
else
dp[v][0] += dp[v][1];
}
int main() {
int n;
cin >> n;
vv.resize(n);
a.resize(n);
for (int i = 0; i < n - 1; i++) {
int x;
cin >> x;
vv[x].push_back(i + 1);
vv[i + 1].push_back(x);
}
for (int i = 0; i < n; i++) cin >> a[i];
dfs(0, 0);
cout << dp[0][1];
}
|
### 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;
vector<vector<int> > vv;
long long dp[200001][2];
const int INF = 1e9 + 7;
vector<int> a;
void dfs(int v, int p) {
dp[v][0] = 1;
dp[v][1] = 0;
for (int i = 0; i < vv[v].size(); i++) {
int to = vv[v][i];
if (to != p) {
dfs(to, v);
dp[v][1] *= dp[to][0];
dp[v][1] %= INF;
dp[v][1] += (dp[v][0] * dp[to][1]) % INF;
dp[v][1] %= INF;
dp[v][0] *= dp[to][0];
dp[v][0] %= INF;
}
}
if (a[v] == 1)
dp[v][1] = dp[v][0];
else
dp[v][0] += dp[v][1];
}
int main() {
int n;
cin >> n;
vv.resize(n);
a.resize(n);
for (int i = 0; i < n - 1; i++) {
int x;
cin >> x;
vv[x].push_back(i + 1);
vv[i + 1].push_back(x);
}
for (int i = 0; i < n; i++) cin >> a[i];
dfs(0, 0);
cout << dp[0][1];
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long int dp[100005][2];
long long int x[100005];
std::vector<long long int> g[100005];
void dfs(long long int v, long long int vp) {
dp[v][1] = 0;
dp[v][0] = 1;
for (long long int u : g[v]) {
if (u == vp) continue;
dfs(u, v);
dp[v][1] =
(((dp[v][1] % 1000000007) * (dp[u][0] % 1000000007)) % 1000000007 +
((dp[v][0] % 1000000007) * (dp[u][1] % 1000000007)) % 1000000007) %
1000000007;
dp[v][0] = ((dp[v][0] % 1000000007) * (dp[u][0] % 1000000007)) % 1000000007;
}
if (x[v] == 1)
dp[v][1] = dp[v][0];
else
dp[v][0] = (dp[v][0] + dp[v][1]) % 1000000007;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int n;
cin >> n;
long long int u;
for (long long int i = 0; i <= n - 1 - 1; i++) {
cin >> u;
g[u].push_back(i + 1);
g[i + 1].push_back(u);
}
for (long long int i = 0; i <= n - 1; i++) cin >> x[i];
dfs(0, -1);
cout << dp[0][1] << "\n";
}
|
### 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 dp[100005][2];
long long int x[100005];
std::vector<long long int> g[100005];
void dfs(long long int v, long long int vp) {
dp[v][1] = 0;
dp[v][0] = 1;
for (long long int u : g[v]) {
if (u == vp) continue;
dfs(u, v);
dp[v][1] =
(((dp[v][1] % 1000000007) * (dp[u][0] % 1000000007)) % 1000000007 +
((dp[v][0] % 1000000007) * (dp[u][1] % 1000000007)) % 1000000007) %
1000000007;
dp[v][0] = ((dp[v][0] % 1000000007) * (dp[u][0] % 1000000007)) % 1000000007;
}
if (x[v] == 1)
dp[v][1] = dp[v][0];
else
dp[v][0] = (dp[v][0] + dp[v][1]) % 1000000007;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int n;
cin >> n;
long long int u;
for (long long int i = 0; i <= n - 1 - 1; i++) {
cin >> u;
g[u].push_back(i + 1);
g[i + 1].push_back(u);
}
for (long long int i = 0; i <= n - 1; i++) cin >> x[i];
dfs(0, -1);
cout << dp[0][1] << "\n";
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int sz = 100005;
int n;
vector<int> T[sz];
vector<int> col;
const int MOD = 1000000007;
long long P[sz][2];
void dfs(int u, int prev) {
if (col[u] == 1) P[u][1] = 1;
P[u][0] = P[u][1] ^ 1;
for (int i = 0; i < T[u].size(); i++) {
int v = T[u][i];
if (v == prev) continue;
dfs(v, u);
P[u][1] *= (P[v][0] + P[v][1]);
P[u][1] %= MOD;
P[u][1] += P[u][0] * P[v][1];
P[u][1] %= MOD;
P[u][0] = P[u][0] * (P[v][0] + P[v][1]);
P[u][0] %= MOD;
}
}
int main() {
memset(P, 0, sizeof P);
scanf("%d", &n);
for (int i = 0; i < n - 1; i++) {
int x;
scanf("%d", &x);
T[i + 1].push_back(x);
T[x].push_back(i + 1);
}
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
col.push_back(x);
}
dfs(0, -1);
cout << P[0][1] << "\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;
const int sz = 100005;
int n;
vector<int> T[sz];
vector<int> col;
const int MOD = 1000000007;
long long P[sz][2];
void dfs(int u, int prev) {
if (col[u] == 1) P[u][1] = 1;
P[u][0] = P[u][1] ^ 1;
for (int i = 0; i < T[u].size(); i++) {
int v = T[u][i];
if (v == prev) continue;
dfs(v, u);
P[u][1] *= (P[v][0] + P[v][1]);
P[u][1] %= MOD;
P[u][1] += P[u][0] * P[v][1];
P[u][1] %= MOD;
P[u][0] = P[u][0] * (P[v][0] + P[v][1]);
P[u][0] %= MOD;
}
}
int main() {
memset(P, 0, sizeof P);
scanf("%d", &n);
for (int i = 0; i < n - 1; i++) {
int x;
scanf("%d", &x);
T[i + 1].push_back(x);
T[x].push_back(i + 1);
}
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
col.push_back(x);
}
dfs(0, -1);
cout << P[0][1] << "\n";
return 0;
}
```
|
#include <bits/stdc++.h>
std::vector<int> path[300000];
int p[300000];
long long dp[300000][2];
void extended_gcd(long long m, long long n, long long &i, long long &j) {
if (n == 0) {
i = 1;
j = 0;
return;
}
extended_gcd(n, m % n, j, i);
j = ((j - (m / n) * i) % 1000000007 + 1000000007) % 1000000007;
}
long long verse(long long x) {
long long i, j;
extended_gcd(x, 1000000007, i, j);
return i;
}
void dfs(int x, int f) {
if (p[x]) {
dp[x][0] = 0;
dp[x][1] = 1;
} else {
dp[x][0] = 1;
dp[x][1] = 0;
}
for (int i = 0; i < path[x].size(); i++) {
int to = path[x][i];
if (to != f) {
dfs(to, x);
if (p[x]) {
dp[x][1] = (dp[x][1] * (dp[to][0] + dp[to][1])) % 1000000007;
} else {
dp[x][0] = (dp[x][0] * (dp[to][0] + dp[to][1])) % 1000000007;
}
}
}
if (p[x] == 0) {
for (int i = 0; i < path[x].size(); i++) {
int to = path[x][i];
if (to != f) {
dp[x][1] += ((dp[x][0] * dp[to][1]) % 1000000007) *
verse(dp[to][0] + dp[to][1]) % 1000000007;
dp[x][1] %= 1000000007;
}
}
}
}
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) path[i].clear();
int x;
for (int i = 1; i < n; i++) {
scanf("%d", &x);
path[x].push_back(i);
path[i].push_back(x);
}
for (int i = 0; i < n; i++) scanf("%d", &p[i]);
int st = -1;
for (int i = n - 1; i >= 0; i--)
if (p[i]) {
st = i;
break;
}
if (st == -1)
puts("0");
else {
dfs(st, -1);
printf("%I64d\n", dp[st][1]);
}
}
|
### 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>
std::vector<int> path[300000];
int p[300000];
long long dp[300000][2];
void extended_gcd(long long m, long long n, long long &i, long long &j) {
if (n == 0) {
i = 1;
j = 0;
return;
}
extended_gcd(n, m % n, j, i);
j = ((j - (m / n) * i) % 1000000007 + 1000000007) % 1000000007;
}
long long verse(long long x) {
long long i, j;
extended_gcd(x, 1000000007, i, j);
return i;
}
void dfs(int x, int f) {
if (p[x]) {
dp[x][0] = 0;
dp[x][1] = 1;
} else {
dp[x][0] = 1;
dp[x][1] = 0;
}
for (int i = 0; i < path[x].size(); i++) {
int to = path[x][i];
if (to != f) {
dfs(to, x);
if (p[x]) {
dp[x][1] = (dp[x][1] * (dp[to][0] + dp[to][1])) % 1000000007;
} else {
dp[x][0] = (dp[x][0] * (dp[to][0] + dp[to][1])) % 1000000007;
}
}
}
if (p[x] == 0) {
for (int i = 0; i < path[x].size(); i++) {
int to = path[x][i];
if (to != f) {
dp[x][1] += ((dp[x][0] * dp[to][1]) % 1000000007) *
verse(dp[to][0] + dp[to][1]) % 1000000007;
dp[x][1] %= 1000000007;
}
}
}
}
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) path[i].clear();
int x;
for (int i = 1; i < n; i++) {
scanf("%d", &x);
path[x].push_back(i);
path[i].push_back(x);
}
for (int i = 0; i < n; i++) scanf("%d", &p[i]);
int st = -1;
for (int i = n - 1; i >= 0; i--)
if (p[i]) {
st = i;
break;
}
if (st == -1)
puts("0");
else {
dfs(st, -1);
printf("%I64d\n", dp[st][1]);
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 * 100 + 3;
const long long MOD = 1000000007;
int n;
bool x[maxn];
vector<int> e[maxn];
long long dp[maxn][2];
void dfs(int source) {
dp[source][0] = 1ll;
dp[source][1] = 0ll;
for (int i = 0; i < e[source].size(); i++) {
int u = e[source][i];
dfs(u);
dp[source][1] = (dp[source][1] * dp[u][0]) % MOD;
dp[source][1] = (dp[source][1] + ((dp[source][0] * dp[u][1]) % MOD)) % MOD;
dp[source][0] = (dp[source][0] * dp[u][0]) % MOD;
}
if (x[source])
dp[source][1] = dp[source][0];
else
dp[source][0] = (dp[source][0] + dp[source][1]) % MOD;
}
int main() {
int an;
cin >> n;
for (int i = 1; i < n; i++) cin >> an, e[an].push_back(i);
for (int i = 0; i < n; i++) cin >> x[i];
dfs(0);
cout << dp[0][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 int maxn = 1000 * 100 + 3;
const long long MOD = 1000000007;
int n;
bool x[maxn];
vector<int> e[maxn];
long long dp[maxn][2];
void dfs(int source) {
dp[source][0] = 1ll;
dp[source][1] = 0ll;
for (int i = 0; i < e[source].size(); i++) {
int u = e[source][i];
dfs(u);
dp[source][1] = (dp[source][1] * dp[u][0]) % MOD;
dp[source][1] = (dp[source][1] + ((dp[source][0] * dp[u][1]) % MOD)) % MOD;
dp[source][0] = (dp[source][0] * dp[u][0]) % MOD;
}
if (x[source])
dp[source][1] = dp[source][0];
else
dp[source][0] = (dp[source][0] + dp[source][1]) % MOD;
}
int main() {
int an;
cin >> n;
for (int i = 1; i < n; i++) cin >> an, e[an].push_back(i);
for (int i = 0; i < n; i++) cin >> x[i];
dfs(0);
cout << dp[0][1] << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long dp[100005][2];
vector<int> adj[1000000];
long long mod = 1000000007;
bool vis[1000000];
int x[1000000];
int n;
void dfs(int v) {
dp[v][x[v]] = 1;
for (auto u : adj[v]) {
dfs(u);
dp[v][1] = (dp[v][0] * dp[u][1] + dp[v][1] * dp[u][0]) % mod;
dp[v][0] = (dp[v][0] * dp[u][0]) % mod;
}
dp[v][0] += dp[v][1];
dp[v][0] %= mod;
}
int main() {
ios_base::sync_with_stdio(0);
cin >> n;
for (int i = 2; i <= n; i++) {
int p;
cin >> p;
p++;
adj[p].push_back(i);
}
for (int i = 1; i <= n; i++) cin >> x[i];
dfs(1);
cout << dp[1][1] << 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;
long long dp[100005][2];
vector<int> adj[1000000];
long long mod = 1000000007;
bool vis[1000000];
int x[1000000];
int n;
void dfs(int v) {
dp[v][x[v]] = 1;
for (auto u : adj[v]) {
dfs(u);
dp[v][1] = (dp[v][0] * dp[u][1] + dp[v][1] * dp[u][0]) % mod;
dp[v][0] = (dp[v][0] * dp[u][0]) % mod;
}
dp[v][0] += dp[v][1];
dp[v][0] %= mod;
}
int main() {
ios_base::sync_with_stdio(0);
cin >> n;
for (int i = 2; i <= n; i++) {
int p;
cin >> p;
p++;
adj[p].push_back(i);
}
for (int i = 1; i <= n; i++) cin >> x[i];
dfs(1);
cout << dp[1][1] << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n, col[100050];
long long dp[100050][2];
vector<int> edge[100050];
void dfs(int at, int past) {
dp[at][0] = 1;
dp[at][1] = 0;
if (col[at]) swap(dp[at][0], dp[at][1]);
for (int i = 0; i < edge[at].size(); i++) {
int u = edge[at][i];
if (u == past) continue;
dfs(u, at);
if (col[at]) {
dp[at][1] *= (dp[u][1] + dp[u][0]) % 1000000007;
dp[at][1] %= 1000000007;
} else {
dp[at][1] *= (dp[u][1] + dp[u][0]) % 1000000007;
dp[at][1] %= 1000000007;
dp[at][1] += (dp[at][0] * dp[u][1]) % 1000000007;
dp[at][1] %= 1000000007;
dp[at][0] *= (dp[u][0] + dp[u][1]) % 1000000007;
dp[at][0] %= 1000000007;
}
}
}
int main() {
int x, y;
scanf("%d", &n);
for (int i = 1; i <= n - 1; i++) {
scanf("%d", &x);
y = i;
edge[x].push_back(y);
edge[y].push_back(x);
}
for (int i = 0; i < n; i++) scanf("%d", &col[i]);
dfs(0, -1);
printf("%lld\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;
int n, col[100050];
long long dp[100050][2];
vector<int> edge[100050];
void dfs(int at, int past) {
dp[at][0] = 1;
dp[at][1] = 0;
if (col[at]) swap(dp[at][0], dp[at][1]);
for (int i = 0; i < edge[at].size(); i++) {
int u = edge[at][i];
if (u == past) continue;
dfs(u, at);
if (col[at]) {
dp[at][1] *= (dp[u][1] + dp[u][0]) % 1000000007;
dp[at][1] %= 1000000007;
} else {
dp[at][1] *= (dp[u][1] + dp[u][0]) % 1000000007;
dp[at][1] %= 1000000007;
dp[at][1] += (dp[at][0] * dp[u][1]) % 1000000007;
dp[at][1] %= 1000000007;
dp[at][0] *= (dp[u][0] + dp[u][1]) % 1000000007;
dp[at][0] %= 1000000007;
}
}
}
int main() {
int x, y;
scanf("%d", &n);
for (int i = 1; i <= n - 1; i++) {
scanf("%d", &x);
y = i;
edge[x].push_back(y);
edge[y].push_back(x);
}
for (int i = 0; i < n; i++) scanf("%d", &col[i]);
dfs(0, -1);
printf("%lld\n", dp[0][1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long const N = 2e5 + 20, MOD = 1e9 + 7;
long long n, x, dp[N][2];
bool vis[N], col[N];
vector<long long> child[N];
void dfs(int v = 0) {
for (auto u : child[v]) dfs(u);
vector<long long> preChild, sufChild;
preChild.push_back(1), sufChild.push_back(1);
for (auto u : child[v]) {
preChild.push_back((preChild.back() * ((dp[u][0] + dp[u][1]) % MOD)) % MOD);
}
for (int i = child[v].size() - 1; i >= 0; i--) {
int u = child[v][i];
sufChild.push_back((sufChild.back() * ((dp[u][0] + dp[u][1]) % MOD)) % MOD);
}
if (col[v]) {
dp[v][0] = 0;
dp[v][1] = preChild.back();
return;
}
dp[v][0] = preChild.back();
dp[v][1] = 0;
preChild.push_back(1), sufChild.push_back(1);
for (int i = 0; i < child[v].size(); i++) {
int u = child[v][i];
dp[v][1] = (dp[v][1] + ((dp[u][1] * preChild[i] % MOD) *
(sufChild[sufChild.size() - 3 - i] % MOD) % MOD)) %
MOD;
}
return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 0; i < n - 1; i++) {
cin >> x;
child[x].push_back(i + 1);
}
for (int i = 0; i < n; i++) cin >> col[i];
dfs();
return cout << dp[0][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;
long long const N = 2e5 + 20, MOD = 1e9 + 7;
long long n, x, dp[N][2];
bool vis[N], col[N];
vector<long long> child[N];
void dfs(int v = 0) {
for (auto u : child[v]) dfs(u);
vector<long long> preChild, sufChild;
preChild.push_back(1), sufChild.push_back(1);
for (auto u : child[v]) {
preChild.push_back((preChild.back() * ((dp[u][0] + dp[u][1]) % MOD)) % MOD);
}
for (int i = child[v].size() - 1; i >= 0; i--) {
int u = child[v][i];
sufChild.push_back((sufChild.back() * ((dp[u][0] + dp[u][1]) % MOD)) % MOD);
}
if (col[v]) {
dp[v][0] = 0;
dp[v][1] = preChild.back();
return;
}
dp[v][0] = preChild.back();
dp[v][1] = 0;
preChild.push_back(1), sufChild.push_back(1);
for (int i = 0; i < child[v].size(); i++) {
int u = child[v][i];
dp[v][1] = (dp[v][1] + ((dp[u][1] * preChild[i] % MOD) *
(sufChild[sufChild.size() - 3 - i] % MOD) % MOD)) %
MOD;
}
return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 0; i < n - 1; i++) {
cin >> x;
child[x].push_back(i + 1);
}
for (int i = 0; i < n; i++) cin >> col[i];
dfs();
return cout << dp[0][1], 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
const long long int mod = 1000000007LL;
int n;
struct Edge {
int to, next;
} edge[3 * maxn];
int Adj[maxn], Size;
void init() {
Size = 0;
memset(Adj, -1, sizeof(Adj));
}
void Add_Edge(int u, int v) {
edge[Size].to = v;
edge[Size].next = Adj[u];
Adj[u] = Size++;
}
int c[maxn];
long long int dp[maxn][2];
void dfs(int u, int fa) {
dp[u][c[u]] = 1;
for (int i = Adj[u]; ~i; i = edge[i].next) {
int v = edge[i].to;
if (v == fa) continue;
dfs(v, u);
dp[u][1] = (dp[u][1] * dp[v][1] % mod + dp[u][0] * dp[v][1] % mod +
dp[u][1] * dp[v][0] % mod) %
mod;
dp[u][0] = (dp[u][0] * dp[v][0] % mod + dp[u][0] * dp[v][1] % mod) % mod;
}
}
int main() {
init();
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int v;
scanf("%d", &v);
Add_Edge(i, v);
Add_Edge(v, i);
}
for (int i = 0; i < n; i++) scanf("%d", c + i);
dfs(0, 0);
printf("%I64d\n", dp[0][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;
const int maxn = 100010;
const long long int mod = 1000000007LL;
int n;
struct Edge {
int to, next;
} edge[3 * maxn];
int Adj[maxn], Size;
void init() {
Size = 0;
memset(Adj, -1, sizeof(Adj));
}
void Add_Edge(int u, int v) {
edge[Size].to = v;
edge[Size].next = Adj[u];
Adj[u] = Size++;
}
int c[maxn];
long long int dp[maxn][2];
void dfs(int u, int fa) {
dp[u][c[u]] = 1;
for (int i = Adj[u]; ~i; i = edge[i].next) {
int v = edge[i].to;
if (v == fa) continue;
dfs(v, u);
dp[u][1] = (dp[u][1] * dp[v][1] % mod + dp[u][0] * dp[v][1] % mod +
dp[u][1] * dp[v][0] % mod) %
mod;
dp[u][0] = (dp[u][0] * dp[v][0] % mod + dp[u][0] * dp[v][1] % mod) % mod;
}
}
int main() {
init();
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int v;
scanf("%d", &v);
Add_Edge(i, v);
Add_Edge(v, i);
}
for (int i = 0; i < n; i++) scanf("%d", c + i);
dfs(0, 0);
printf("%I64d\n", dp[0][1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, m;
cin >> a >> m;
for (int i = 0; i <= m; i++) {
a += (a % m);
a %= m;
if (a == 0) {
printf("Yes\n");
return 0;
}
}
printf("No\n");
}
|
### Prompt
Generate a cpp solution to the following problem:
One industrial factory is reforming working plan. The director suggested to set a mythical detail production norm. If at the beginning of the day there were x details in the factory storage, then by the end of the day the factory has to produce <image> (remainder after dividing x by m) more details. Unfortunately, no customer has ever bought any mythical detail, so all the details produced stay on the factory.
The board of directors are worried that the production by the given plan may eventually stop (that means that there will be Π° moment when the current number of details on the factory is divisible by m).
Given the number of details a on the first day and number m check if the production stops at some moment.
Input
The first line contains two integers a and m (1 β€ a, m β€ 105).
Output
Print "Yes" (without quotes) if the production will eventually stop, otherwise print "No".
Examples
Input
1 5
Output
No
Input
3 6
Output
Yes
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, m;
cin >> a >> m;
for (int i = 0; i <= m; i++) {
a += (a % m);
a %= m;
if (a == 0) {
printf("Yes\n");
return 0;
}
}
printf("No\n");
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
bool isPrime(long long n) {
for (long long i = 2; i * i <= n; ++i) {
if (n % i == 0) {
return false;
}
}
return true;
}
long long factorial(long long n) {
return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
}
long long power(long long x, long long y) {
long long res = 1;
x = x % mod;
while (y > 0) {
if (y & 1) res = ((res % mod) * (x % mod)) % mod;
y = y >> 1;
x = ((x % mod) * (x % mod)) % mod;
}
return res;
}
long long ncr(long long n, long long r) {
long long res = 1;
if (r > n) return 0;
if (r > n - r) r = n - r;
for (long long i = 0; i < r; i++) {
res = ((res % mod) * ((n - i) % mod)) % mod;
res = ((res % mod) * (power(i + 1, mod - 2)) % mod) % mod;
}
return res;
}
long long gcd(long long a, long long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
long long lcm(long long a, long long b) { return (a / gcd(a, b) * b); }
long long max(long long a, long long b) {
long long ans = a > b ? a : b;
return ans;
}
long long min(long long a, long long b) {
long long ans = a < b ? a : b;
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long n, m;
long long val = 1;
map<long long, long long> make_pair;
cin >> n >> m;
while (val) {
if (n % m == 0) {
cout << "Yes\n";
return 0;
}
if (make_pair[n]) {
cout << "No\n";
return 0;
}
make_pair[n]++;
n = n + n % m;
n %= m;
}
}
|
### Prompt
In Cpp, your task is to solve the following problem:
One industrial factory is reforming working plan. The director suggested to set a mythical detail production norm. If at the beginning of the day there were x details in the factory storage, then by the end of the day the factory has to produce <image> (remainder after dividing x by m) more details. Unfortunately, no customer has ever bought any mythical detail, so all the details produced stay on the factory.
The board of directors are worried that the production by the given plan may eventually stop (that means that there will be Π° moment when the current number of details on the factory is divisible by m).
Given the number of details a on the first day and number m check if the production stops at some moment.
Input
The first line contains two integers a and m (1 β€ a, m β€ 105).
Output
Print "Yes" (without quotes) if the production will eventually stop, otherwise print "No".
Examples
Input
1 5
Output
No
Input
3 6
Output
Yes
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
bool isPrime(long long n) {
for (long long i = 2; i * i <= n; ++i) {
if (n % i == 0) {
return false;
}
}
return true;
}
long long factorial(long long n) {
return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
}
long long power(long long x, long long y) {
long long res = 1;
x = x % mod;
while (y > 0) {
if (y & 1) res = ((res % mod) * (x % mod)) % mod;
y = y >> 1;
x = ((x % mod) * (x % mod)) % mod;
}
return res;
}
long long ncr(long long n, long long r) {
long long res = 1;
if (r > n) return 0;
if (r > n - r) r = n - r;
for (long long i = 0; i < r; i++) {
res = ((res % mod) * ((n - i) % mod)) % mod;
res = ((res % mod) * (power(i + 1, mod - 2)) % mod) % mod;
}
return res;
}
long long gcd(long long a, long long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
long long lcm(long long a, long long b) { return (a / gcd(a, b) * b); }
long long max(long long a, long long b) {
long long ans = a > b ? a : b;
return ans;
}
long long min(long long a, long long b) {
long long ans = a < b ? a : b;
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long n, m;
long long val = 1;
map<long long, long long> make_pair;
cin >> n >> m;
while (val) {
if (n % m == 0) {
cout << "Yes\n";
return 0;
}
if (make_pair[n]) {
cout << "No\n";
return 0;
}
make_pair[n]++;
n = n + n % m;
n %= m;
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T>
inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
long long x[1001], y[1001];
int main() {
int a, m;
cin >> a >> m;
while (a % 2 == 0) a /= 2;
while (m % 2 == 0) m /= 2;
if (a % m == 0) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
}
|
### Prompt
Your task is to create a cpp solution to the following problem:
One industrial factory is reforming working plan. The director suggested to set a mythical detail production norm. If at the beginning of the day there were x details in the factory storage, then by the end of the day the factory has to produce <image> (remainder after dividing x by m) more details. Unfortunately, no customer has ever bought any mythical detail, so all the details produced stay on the factory.
The board of directors are worried that the production by the given plan may eventually stop (that means that there will be Π° moment when the current number of details on the factory is divisible by m).
Given the number of details a on the first day and number m check if the production stops at some moment.
Input
The first line contains two integers a and m (1 β€ a, m β€ 105).
Output
Print "Yes" (without quotes) if the production will eventually stop, otherwise print "No".
Examples
Input
1 5
Output
No
Input
3 6
Output
Yes
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T>
inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
long long x[1001], y[1001];
int main() {
int a, m;
cin >> a >> m;
while (a % 2 == 0) a /= 2;
while (m % 2 == 0) m /= 2;
if (a % m == 0) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
}
```
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.