output
stringlengths 52
181k
| instruction
stringlengths 296
182k
|
---|---|
#include <bits/stdc++.h>
using namespace std;
long long d[100005];
long long flag[100005];
long long c[100005];
long long r[100005];
vector<int> q[100005];
int mod = 1e9 + 7;
long long cf(long long x, int p) {
long long y = 1;
while (p) {
if (p % 2) {
y = y * x % mod;
p--;
} else {
p /= 2;
x = x * x % mod;
}
}
return y;
}
long long dfs(int n, int m, int x) {
if (flag[n] == x) return d[n];
long long &ans = d[n];
flag[n] = x;
if (n < m) return d[n] = 0;
ans = c[n - 1] * r[m - 1] % mod * r[n - m] % mod;
for (int i = 0; i < q[n].size(); i++) {
int y = q[n][i];
ans = (ans - dfs(y, m, x) + mod) % mod;
}
return ans;
}
int main() {
for (int i = 1; i <= 100000; i++) {
for (int j = i + i; j <= 100000; j += i) {
q[j].push_back(i);
}
}
int q;
cin >> q;
c[0] = 1;
c[1] = 1;
r[0] = 1;
r[1] = 1;
for (int i = 2; i <= 100000; i++) {
c[i] = c[i - 1] * i % mod;
r[i] = cf(c[i], mod - 2);
}
for (int i = 1; i <= q; i++) {
int n, m;
cin >> n >> m;
long long z = dfs(n, m, i);
cout << z << endl;
}
return 0;
}
|
### Prompt
Your task is to create a cpp solution to the following problem:
Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend.
He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai.
Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7).
To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7).
Input
The first line contains an integer q representing the number of queries (1 β€ q β€ 105). Each of the next q lines contains two space space-separated integers n, f (1 β€ f β€ n β€ 105).
Output
For each query, output a single integer in a line corresponding to the answer of each query.
Examples
Input
5
6 2
7 2
6 3
6 4
7 4
Output
2
6
9
10
20
Note
For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1].
For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long d[100005];
long long flag[100005];
long long c[100005];
long long r[100005];
vector<int> q[100005];
int mod = 1e9 + 7;
long long cf(long long x, int p) {
long long y = 1;
while (p) {
if (p % 2) {
y = y * x % mod;
p--;
} else {
p /= 2;
x = x * x % mod;
}
}
return y;
}
long long dfs(int n, int m, int x) {
if (flag[n] == x) return d[n];
long long &ans = d[n];
flag[n] = x;
if (n < m) return d[n] = 0;
ans = c[n - 1] * r[m - 1] % mod * r[n - m] % mod;
for (int i = 0; i < q[n].size(); i++) {
int y = q[n][i];
ans = (ans - dfs(y, m, x) + mod) % mod;
}
return ans;
}
int main() {
for (int i = 1; i <= 100000; i++) {
for (int j = i + i; j <= 100000; j += i) {
q[j].push_back(i);
}
}
int q;
cin >> q;
c[0] = 1;
c[1] = 1;
r[0] = 1;
r[1] = 1;
for (int i = 2; i <= 100000; i++) {
c[i] = c[i - 1] * i % mod;
r[i] = cf(c[i], mod - 2);
}
for (int i = 1; i <= q; i++) {
int n, m;
cin >> n >> m;
long long z = dfs(n, m, i);
cout << z << endl;
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 0, 1, -1, -1, -1, 1, 1};
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
template <class T>
inline T biton(T n, T pos) {
return n | ((T)1 << pos);
}
template <class T>
inline T bitoff(T n, T pos) {
return n & ~((T)1 << pos);
}
template <class T>
inline T ison(T n, T pos) {
return (bool)(n & ((T)1 << pos));
}
template <class T>
inline T gcd(T a, T b) {
while (b) {
a %= b;
swap(a, b);
}
return a;
}
template <typename T>
string NumberToString(T Number) {
ostringstream second;
second << Number;
return second.str();
}
inline int nxt() {
int aaa;
scanf("%d", &aaa);
return aaa;
}
inline long long int lxt() {
long long int aaa;
scanf("%lld", &aaa);
return aaa;
}
inline double dxt() {
double aaa;
scanf("%lf", &aaa);
return aaa;
}
template <class T>
inline T bigmod(T p, T e, T m) {
T ret = 1;
for (; e > 0; e >>= 1) {
if (e & 1) ret = (ret * p) % m;
p = (p * p) % m;
}
return (T)ret;
}
int ar[100010];
vector<int> Divisor[100010];
long long int fac[100010], finv[100010];
pair<long long int, long long int> extendedEuclid(long long int a,
long long int b) {
if (b == 0)
return pair<long long int, long long int>(1, 0);
else {
pair<long long int, long long int> d = extendedEuclid(b, a % b);
return pair<long long int, long long int>(d.second,
d.first - d.second * (a / b));
}
}
long long int modularInverse(long long int a, long long int n) {
pair<long long int, long long int> ret = extendedEuclid(a, n);
return ((ret.first % n) + n) % n;
}
void gen() {
fac[0] = 1;
for (long long int i = 1; i < 100010; i++)
fac[i] = (i * fac[i - 1]) % 1000000007;
finv[100010 - 1] = modularInverse(fac[100010 - 1], (long long int)1000000007);
for (long long int i = 100010 - 2; i >= 0; i--)
finv[i] = (finv[i + 1] * (i + 1)) % 1000000007;
}
long long int ncr(int n, int k) {
n--;
k--;
if (n < k) return 0;
return (((fac[n] * finv[k]) % 1000000007) * finv[n - k]) % 1000000007;
}
int miu[100010];
int mark[100010];
void mobius(int n) {
for (int i = 0; i <= n; i++) miu[i] = 1;
miu[0] = 0;
miu[1] = 1;
for (int i = 2; i <= n; i++) {
if (mark[i]) continue;
miu[i] = -1;
for (int j = i * 2; j <= n; j += i) {
if (miu[j] != 0) {
if (j % (i * i) == 0)
miu[j] = 0;
else
miu[j] *= -1;
mark[j] = 1;
}
}
}
}
long long int go(long long int n, long long int f) {
long long int res = 0;
for (int i = 0; i < Divisor[n].size(); i++) {
res += miu[Divisor[n][i]] * ncr(n / Divisor[n][i], f);
res %= 1000000007;
if (res < 0) res += 1000000007;
res %= 1000000007;
}
return res;
}
int main() {
gen();
mobius(100010 - 1);
for (int i = 1; i < 100010; i++) {
for (int j = i; j < 100010; j += i) {
Divisor[j].push_back(i);
}
}
int q = nxt();
while (q--) {
int n = nxt();
int f = nxt();
printf("%lld\n", go(n, f));
}
return 0;
}
|
### Prompt
Create a solution in Cpp for the following problem:
Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend.
He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai.
Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7).
To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7).
Input
The first line contains an integer q representing the number of queries (1 β€ q β€ 105). Each of the next q lines contains two space space-separated integers n, f (1 β€ f β€ n β€ 105).
Output
For each query, output a single integer in a line corresponding to the answer of each query.
Examples
Input
5
6 2
7 2
6 3
6 4
7 4
Output
2
6
9
10
20
Note
For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1].
For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 0, 1, -1, -1, -1, 1, 1};
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
template <class T>
inline T biton(T n, T pos) {
return n | ((T)1 << pos);
}
template <class T>
inline T bitoff(T n, T pos) {
return n & ~((T)1 << pos);
}
template <class T>
inline T ison(T n, T pos) {
return (bool)(n & ((T)1 << pos));
}
template <class T>
inline T gcd(T a, T b) {
while (b) {
a %= b;
swap(a, b);
}
return a;
}
template <typename T>
string NumberToString(T Number) {
ostringstream second;
second << Number;
return second.str();
}
inline int nxt() {
int aaa;
scanf("%d", &aaa);
return aaa;
}
inline long long int lxt() {
long long int aaa;
scanf("%lld", &aaa);
return aaa;
}
inline double dxt() {
double aaa;
scanf("%lf", &aaa);
return aaa;
}
template <class T>
inline T bigmod(T p, T e, T m) {
T ret = 1;
for (; e > 0; e >>= 1) {
if (e & 1) ret = (ret * p) % m;
p = (p * p) % m;
}
return (T)ret;
}
int ar[100010];
vector<int> Divisor[100010];
long long int fac[100010], finv[100010];
pair<long long int, long long int> extendedEuclid(long long int a,
long long int b) {
if (b == 0)
return pair<long long int, long long int>(1, 0);
else {
pair<long long int, long long int> d = extendedEuclid(b, a % b);
return pair<long long int, long long int>(d.second,
d.first - d.second * (a / b));
}
}
long long int modularInverse(long long int a, long long int n) {
pair<long long int, long long int> ret = extendedEuclid(a, n);
return ((ret.first % n) + n) % n;
}
void gen() {
fac[0] = 1;
for (long long int i = 1; i < 100010; i++)
fac[i] = (i * fac[i - 1]) % 1000000007;
finv[100010 - 1] = modularInverse(fac[100010 - 1], (long long int)1000000007);
for (long long int i = 100010 - 2; i >= 0; i--)
finv[i] = (finv[i + 1] * (i + 1)) % 1000000007;
}
long long int ncr(int n, int k) {
n--;
k--;
if (n < k) return 0;
return (((fac[n] * finv[k]) % 1000000007) * finv[n - k]) % 1000000007;
}
int miu[100010];
int mark[100010];
void mobius(int n) {
for (int i = 0; i <= n; i++) miu[i] = 1;
miu[0] = 0;
miu[1] = 1;
for (int i = 2; i <= n; i++) {
if (mark[i]) continue;
miu[i] = -1;
for (int j = i * 2; j <= n; j += i) {
if (miu[j] != 0) {
if (j % (i * i) == 0)
miu[j] = 0;
else
miu[j] *= -1;
mark[j] = 1;
}
}
}
}
long long int go(long long int n, long long int f) {
long long int res = 0;
for (int i = 0; i < Divisor[n].size(); i++) {
res += miu[Divisor[n][i]] * ncr(n / Divisor[n][i], f);
res %= 1000000007;
if (res < 0) res += 1000000007;
res %= 1000000007;
}
return res;
}
int main() {
gen();
mobius(100010 - 1);
for (int i = 1; i < 100010; i++) {
for (int j = i; j < 100010; j += i) {
Divisor[j].push_back(i);
}
}
int q = nxt();
while (q--) {
int n = nxt();
int f = nxt();
printf("%lld\n", go(n, f));
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
namespace Flandre_Scarlet {
long long I() {
long long x = 0;
char c = getchar();
long long f = 1;
while (c < '0' or c > '9') f = (c == '-') ? -1 : 1, c = getchar();
while (c >= '0' and c <= '9')
x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
return (x = (f == 1) ? x : -x);
}
void Rd(long long cnt, ...) {
va_list args;
va_start(args, cnt);
for (long long i = 1; i <= cnt; ++i) {
long long* x = va_arg(args, long long*);
(*x) = I();
}
va_end(args);
}
bool notp[155555];
long long primes[155555];
long long mu[155555];
long long fac[155555], ifac[155555], inv[155555];
void Init() {
long long n = 1e5;
notp[1] = 1;
mu[1] = 1;
long long& cnt = primes[0];
for (long long i = 2; i <= n; ++i) {
if (!notp[i]) primes[++cnt] = i, mu[i] = -1;
for (long long j = 1; j <= cnt and i * primes[j] <= n; ++j) {
long long u = primes[j];
notp[i * u] = 1;
if (i % u) {
mu[i * u] = -mu[i];
} else {
mu[i * u] = 0;
break;
}
}
}
fac[0] = 1;
for (long long i = 1; i <= n; ++i) fac[i] = fac[i - 1] * i % 1000000007;
inv[1] = 1;
for (long long i = 2; i <= n; ++i)
inv[i] = (1000000007 - 1000000007 / i) * inv[1000000007 % i] % 1000000007;
ifac[0] = 1;
for (long long i = 1; i <= n; ++i)
ifac[i] = ifac[i - 1] * inv[i] % 1000000007;
}
long long n, k;
void Input() { Rd(2, &n, &k); }
long long C(long long n, long long m) {
return m > n ? 0 : fac[n] * ifac[m] % 1000000007 * ifac[n - m] % 1000000007;
}
long long calc1(long long n, long long k) { return C(n - 1, k - 1); }
void Soviet() {
long long ans = 0;
for (long long i = 1; i * i <= n; ++i)
if (n % i == 0) {
ans += mu[i] * calc1(n / i, k), ans %= 1000000007;
if (i != n / i) ans += mu[n / i] * calc1(i, k), ans %= 1000000007;
}
printf("%lld\n", (ans % 1000000007 + 1000000007) % 1000000007);
}
void IsMyWife() {
Init();
long long t = I();
for (long long i = 1; i <= t; ++i) {
Input();
Soviet();
}
}
} // namespace Flandre_Scarlet
int main() {
Flandre_Scarlet::IsMyWife();
getchar();
getchar();
return 0;
}
|
### Prompt
Please provide a Cpp coded solution to the problem described below:
Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend.
He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai.
Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7).
To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7).
Input
The first line contains an integer q representing the number of queries (1 β€ q β€ 105). Each of the next q lines contains two space space-separated integers n, f (1 β€ f β€ n β€ 105).
Output
For each query, output a single integer in a line corresponding to the answer of each query.
Examples
Input
5
6 2
7 2
6 3
6 4
7 4
Output
2
6
9
10
20
Note
For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1].
For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
namespace Flandre_Scarlet {
long long I() {
long long x = 0;
char c = getchar();
long long f = 1;
while (c < '0' or c > '9') f = (c == '-') ? -1 : 1, c = getchar();
while (c >= '0' and c <= '9')
x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
return (x = (f == 1) ? x : -x);
}
void Rd(long long cnt, ...) {
va_list args;
va_start(args, cnt);
for (long long i = 1; i <= cnt; ++i) {
long long* x = va_arg(args, long long*);
(*x) = I();
}
va_end(args);
}
bool notp[155555];
long long primes[155555];
long long mu[155555];
long long fac[155555], ifac[155555], inv[155555];
void Init() {
long long n = 1e5;
notp[1] = 1;
mu[1] = 1;
long long& cnt = primes[0];
for (long long i = 2; i <= n; ++i) {
if (!notp[i]) primes[++cnt] = i, mu[i] = -1;
for (long long j = 1; j <= cnt and i * primes[j] <= n; ++j) {
long long u = primes[j];
notp[i * u] = 1;
if (i % u) {
mu[i * u] = -mu[i];
} else {
mu[i * u] = 0;
break;
}
}
}
fac[0] = 1;
for (long long i = 1; i <= n; ++i) fac[i] = fac[i - 1] * i % 1000000007;
inv[1] = 1;
for (long long i = 2; i <= n; ++i)
inv[i] = (1000000007 - 1000000007 / i) * inv[1000000007 % i] % 1000000007;
ifac[0] = 1;
for (long long i = 1; i <= n; ++i)
ifac[i] = ifac[i - 1] * inv[i] % 1000000007;
}
long long n, k;
void Input() { Rd(2, &n, &k); }
long long C(long long n, long long m) {
return m > n ? 0 : fac[n] * ifac[m] % 1000000007 * ifac[n - m] % 1000000007;
}
long long calc1(long long n, long long k) { return C(n - 1, k - 1); }
void Soviet() {
long long ans = 0;
for (long long i = 1; i * i <= n; ++i)
if (n % i == 0) {
ans += mu[i] * calc1(n / i, k), ans %= 1000000007;
if (i != n / i) ans += mu[n / i] * calc1(i, k), ans %= 1000000007;
}
printf("%lld\n", (ans % 1000000007 + 1000000007) % 1000000007);
}
void IsMyWife() {
Init();
long long t = I();
for (long long i = 1; i <= t; ++i) {
Input();
Soviet();
}
}
} // namespace Flandre_Scarlet
int main() {
Flandre_Scarlet::IsMyWife();
getchar();
getchar();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long jie[100010], ni[100010];
int miu[100010];
long long power(long long x, int y) {
if (y == 1)
return x;
else {
long long tmp = power(x, y / 2);
tmp = tmp * tmp % 1000000007;
if (y % 2 == 1)
return tmp * x % 1000000007;
else
return tmp;
}
}
void initc() {
int i;
jie[0] = ni[0] = 1;
for (i = 1; i < 100010; i++)
jie[i] = jie[i - 1] * i % 1000000007, ni[i] = power(jie[i], 1000000007 - 2);
}
void initf() {
int i, j, x;
miu[1] = 1;
long long y;
for (i = 2; i < 100010; i++) {
if (miu[i] != 0) continue;
for (x = 1; x <= 100010 / i; x++)
if ((miu[x] == 1 || miu[x] == -1) && (x % i != 0)) {
miu[x * i] = 0 - miu[x];
y = x * i;
for (j = 1; j < 30; j++) {
y = y * i;
if (y > 100010) break;
miu[y] = 2;
}
} else if (miu[x] == 2) {
y = x;
for (j = 1; j < 30; j++) {
y *= i;
if (y > 100010) break;
miu[y] = 2;
}
}
}
}
long long get(int x, int y) {
long long ans = 1;
if (x < y) return 0;
ans = jie[x - 1] * ni[x - y] % 1000000007 * ni[y - 1] % 1000000007;
return ans;
}
int main() {
int q, x, y, i, j;
initc();
initf();
scanf("%d", &q);
while (q--) {
scanf("%d%d", &x, &y);
long long ans = 0;
for (i = 1; i * i <= x; i++)
if (x % i == 0) {
if (miu[i] == 1)
ans = (ans + get(x / i, y)) % 1000000007;
else if (miu[i] == -1)
ans = (ans - get(x / i, y) + 1000000007) % 1000000007;
if (i * i != x) {
if (miu[x / i] == 1)
ans = (ans + get(i, y)) % 1000000007;
else if (miu[x / i] == -1)
ans = (ans - get(i, y) + 1000000007) % 1000000007;
}
}
printf("%d\n", ans);
}
return 0;
}
|
### Prompt
Generate a cpp solution to the following problem:
Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend.
He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai.
Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7).
To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7).
Input
The first line contains an integer q representing the number of queries (1 β€ q β€ 105). Each of the next q lines contains two space space-separated integers n, f (1 β€ f β€ n β€ 105).
Output
For each query, output a single integer in a line corresponding to the answer of each query.
Examples
Input
5
6 2
7 2
6 3
6 4
7 4
Output
2
6
9
10
20
Note
For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1].
For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long jie[100010], ni[100010];
int miu[100010];
long long power(long long x, int y) {
if (y == 1)
return x;
else {
long long tmp = power(x, y / 2);
tmp = tmp * tmp % 1000000007;
if (y % 2 == 1)
return tmp * x % 1000000007;
else
return tmp;
}
}
void initc() {
int i;
jie[0] = ni[0] = 1;
for (i = 1; i < 100010; i++)
jie[i] = jie[i - 1] * i % 1000000007, ni[i] = power(jie[i], 1000000007 - 2);
}
void initf() {
int i, j, x;
miu[1] = 1;
long long y;
for (i = 2; i < 100010; i++) {
if (miu[i] != 0) continue;
for (x = 1; x <= 100010 / i; x++)
if ((miu[x] == 1 || miu[x] == -1) && (x % i != 0)) {
miu[x * i] = 0 - miu[x];
y = x * i;
for (j = 1; j < 30; j++) {
y = y * i;
if (y > 100010) break;
miu[y] = 2;
}
} else if (miu[x] == 2) {
y = x;
for (j = 1; j < 30; j++) {
y *= i;
if (y > 100010) break;
miu[y] = 2;
}
}
}
}
long long get(int x, int y) {
long long ans = 1;
if (x < y) return 0;
ans = jie[x - 1] * ni[x - y] % 1000000007 * ni[y - 1] % 1000000007;
return ans;
}
int main() {
int q, x, y, i, j;
initc();
initf();
scanf("%d", &q);
while (q--) {
scanf("%d%d", &x, &y);
long long ans = 0;
for (i = 1; i * i <= x; i++)
if (x % i == 0) {
if (miu[i] == 1)
ans = (ans + get(x / i, y)) % 1000000007;
else if (miu[i] == -1)
ans = (ans - get(x / i, y) + 1000000007) % 1000000007;
if (i * i != x) {
if (miu[x / i] == 1)
ans = (ans + get(i, y)) % 1000000007;
else if (miu[x / i] == -1)
ans = (ans - get(i, y) + 1000000007) % 1000000007;
}
}
printf("%d\n", ans);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long sz = 1e5 + 10, mod = 1e9 + 7;
vector<long long> dv[sz];
long long dp[sz], fact[sz], inv[sz], totalWay[sz];
unordered_map<int, int> ret[sz];
long long fastPow(long long x, long long n, long long MOD) {
long long ret = 1;
while (n) {
if (n & 1) ret = (ret * x) % MOD;
x = (x * x) % MOD;
n >>= 1;
}
return ret % MOD;
}
inline long long starsBars(long long n, long long k) {
n--, k--;
long long den = (inv[k] * inv[n - k]) % mod;
return (fact[n] * den) % mod;
}
long long solve(long long n, long long f) {
if (n < f) return 0;
if (dp[n] != -1) return dp[n];
long long way = starsBars(n, f);
for (long long &g : dv[n]) {
way -= solve(n / g, f);
if (way < 0) way += mod;
}
return dp[n] = way;
}
int main() {
for (long long i = 2; i < sz; i++)
for (long long j = i + i; j < sz; j += i) dv[j].push_back(i);
fact[0] = 1;
for (long long i = 1; i <= sz - 1; ++i) fact[i] = (fact[i - 1] * i) % mod;
inv[sz - 1] = fastPow(fact[sz - 1], mod - 2, mod);
for (long long i = (sz - 1 - 1); i >= 0; --i)
inv[i] = (inv[i + 1] * (i + 1)) % mod;
long long q;
cin >> q;
while (q--) {
long long n, f;
scanf("%lld", &n), scanf("%lld", &f);
if (f == 1) {
if (n == 1)
printf("1\n");
else
printf("0\n");
continue;
}
if (ret[n].find(f) != ret[n].end()) {
printf("%d\n", ret[n][f]);
continue;
}
for (long long &d : dv[n]) dp[d] = -1;
dp[n] = -1;
printf("%d\n", ret[n][f] = solve(n, f));
}
return 0;
}
|
### Prompt
Your task is to create a Cpp solution to the following problem:
Today is Devu's birthday. For celebrating the occasion, he bought n sweets from the nearby market. He has invited his f friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend.
He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed n sweets to his friends such that ith friend is given ai sweets. He wants to make sure that there should not be any positive integer x > 1, which divides every ai.
Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109 + 7).
To make the problem more interesting, you are given q queries. Each query contains an n, f pair. For each query please output the required number of ways modulo 1000000007 (109 + 7).
Input
The first line contains an integer q representing the number of queries (1 β€ q β€ 105). Each of the next q lines contains two space space-separated integers n, f (1 β€ f β€ n β€ 105).
Output
For each query, output a single integer in a line corresponding to the answer of each query.
Examples
Input
5
6 2
7 2
6 3
6 4
7 4
Output
2
6
9
10
20
Note
For first query: n = 6, f = 2. Possible partitions are [1, 5] and [5, 1].
For second query: n = 7, f = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long sz = 1e5 + 10, mod = 1e9 + 7;
vector<long long> dv[sz];
long long dp[sz], fact[sz], inv[sz], totalWay[sz];
unordered_map<int, int> ret[sz];
long long fastPow(long long x, long long n, long long MOD) {
long long ret = 1;
while (n) {
if (n & 1) ret = (ret * x) % MOD;
x = (x * x) % MOD;
n >>= 1;
}
return ret % MOD;
}
inline long long starsBars(long long n, long long k) {
n--, k--;
long long den = (inv[k] * inv[n - k]) % mod;
return (fact[n] * den) % mod;
}
long long solve(long long n, long long f) {
if (n < f) return 0;
if (dp[n] != -1) return dp[n];
long long way = starsBars(n, f);
for (long long &g : dv[n]) {
way -= solve(n / g, f);
if (way < 0) way += mod;
}
return dp[n] = way;
}
int main() {
for (long long i = 2; i < sz; i++)
for (long long j = i + i; j < sz; j += i) dv[j].push_back(i);
fact[0] = 1;
for (long long i = 1; i <= sz - 1; ++i) fact[i] = (fact[i - 1] * i) % mod;
inv[sz - 1] = fastPow(fact[sz - 1], mod - 2, mod);
for (long long i = (sz - 1 - 1); i >= 0; --i)
inv[i] = (inv[i + 1] * (i + 1)) % mod;
long long q;
cin >> q;
while (q--) {
long long n, f;
scanf("%lld", &n), scanf("%lld", &f);
if (f == 1) {
if (n == 1)
printf("1\n");
else
printf("0\n");
continue;
}
if (ret[n].find(f) != ret[n].end()) {
printf("%d\n", ret[n][f]);
continue;
}
for (long long &d : dv[n]) dp[d] = -1;
dp[n] = -1;
printf("%d\n", ret[n][f] = solve(n, f));
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7, N_MAX = 1e5;
int n;
vector<int> gr[N_MAX + 2];
bool col[N_MAX + 2];
int dp[N_MAX + 2][2];
void dfs(int arg) {
dp[arg][0] = !col[arg];
dp[arg][1] = col[arg];
for (auto it : gr[arg]) {
dfs(it);
int aux0 = dp[arg][0], aux1 = dp[arg][1];
dp[arg][0] = (1LL * aux0 * dp[it][0] + 1LL * aux0 * dp[it][1]) % MOD;
dp[arg][1] = (1LL * aux0 * dp[it][1] + 1LL * aux1 * dp[it][0] +
1LL * aux1 * dp[it][1]) %
MOD;
}
}
int main() {
cin >> n;
for (int i = 1; i < n; i++) {
int x;
cin >> x;
gr[x].push_back(i);
}
for (int i = 0; i < n; i++) cin >> col[i];
dfs(0);
cout << 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, N_MAX = 1e5;
int n;
vector<int> gr[N_MAX + 2];
bool col[N_MAX + 2];
int dp[N_MAX + 2][2];
void dfs(int arg) {
dp[arg][0] = !col[arg];
dp[arg][1] = col[arg];
for (auto it : gr[arg]) {
dfs(it);
int aux0 = dp[arg][0], aux1 = dp[arg][1];
dp[arg][0] = (1LL * aux0 * dp[it][0] + 1LL * aux0 * dp[it][1]) % MOD;
dp[arg][1] = (1LL * aux0 * dp[it][1] + 1LL * aux1 * dp[it][0] +
1LL * aux1 * dp[it][1]) %
MOD;
}
}
int main() {
cin >> n;
for (int i = 1; i < n; i++) {
int x;
cin >> x;
gr[x].push_back(i);
}
for (int i = 0; i < n; i++) cin >> col[i];
dfs(0);
cout << dp[0][1];
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n, clr[100005];
vector<int> adj[100005];
long long dp[100005][2];
long long bigmod(long long x, int pow) {
long long ret = 1;
while (pow > 0) {
if (pow & 1) ret = (ret * x) % 1000000007;
x = (x * x) % 1000000007;
pow /= 2;
}
return ret;
}
long long dfs(int node, int par, int val) {
if (dp[node][val] != -1) return dp[node][val];
vector<int> v0, v1, Ts;
if (!val && clr[node]) return 0;
for (int i = 0; i < (int)adj[node].size(); i++) {
int nd = adj[node][i];
if (nd == par) continue;
v0.push_back(dfs(nd, node, 0));
v1.push_back(dfs(nd, node, 1));
}
long long ret = 0, whole = 1;
Ts.push_back(1);
for (int i = 0; i < (int)v0.size(); i++) {
whole = (v0[i] + v1[i]) * whole;
whole %= 1000000007;
Ts.push_back(whole);
}
if (val && !clr[node]) {
long long tmp = 1;
for (int i = (int)v0.size() - 1; i >= 0; i--) {
long long a = (Ts[i] * tmp) % 1000000007;
a = (a * v1[i]) % 1000000007;
ret = (ret + a) % 1000000007;
tmp = tmp * (v0[i] + v1[i]);
tmp %= 1000000007;
}
} else
ret = whole;
return dp[node][val] = ret;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < (int)n - 1; i++) {
int u;
cin >> u;
adj[u].push_back(i + 1);
adj[i + 1].push_back(u);
}
for (int i = 0; i < (int)n; i++) cin >> clr[i];
memset(dp, -1, sizeof(dp));
cout << dfs(0, -1, 1) << endl;
}
|
### Prompt
Please formulate a CPP solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, clr[100005];
vector<int> adj[100005];
long long dp[100005][2];
long long bigmod(long long x, int pow) {
long long ret = 1;
while (pow > 0) {
if (pow & 1) ret = (ret * x) % 1000000007;
x = (x * x) % 1000000007;
pow /= 2;
}
return ret;
}
long long dfs(int node, int par, int val) {
if (dp[node][val] != -1) return dp[node][val];
vector<int> v0, v1, Ts;
if (!val && clr[node]) return 0;
for (int i = 0; i < (int)adj[node].size(); i++) {
int nd = adj[node][i];
if (nd == par) continue;
v0.push_back(dfs(nd, node, 0));
v1.push_back(dfs(nd, node, 1));
}
long long ret = 0, whole = 1;
Ts.push_back(1);
for (int i = 0; i < (int)v0.size(); i++) {
whole = (v0[i] + v1[i]) * whole;
whole %= 1000000007;
Ts.push_back(whole);
}
if (val && !clr[node]) {
long long tmp = 1;
for (int i = (int)v0.size() - 1; i >= 0; i--) {
long long a = (Ts[i] * tmp) % 1000000007;
a = (a * v1[i]) % 1000000007;
ret = (ret + a) % 1000000007;
tmp = tmp * (v0[i] + v1[i]);
tmp %= 1000000007;
}
} else
ret = whole;
return dp[node][val] = ret;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < (int)n - 1; i++) {
int u;
cin >> u;
adj[u].push_back(i + 1);
adj[i + 1].push_back(u);
}
for (int i = 0; i < (int)n; i++) cin >> clr[i];
memset(dp, -1, sizeof(dp));
cout << dfs(0, -1, 1) << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int modul = 1000000007;
const int nmax = 100100;
const double e = 1e-8;
const double pi = acos(-1);
int n, m, stest, c[nmax], F[nmax][2], G[nmax];
vector<int> head[nmax];
void DFS(int u) {
F[u][0] = (c[u] == 0);
for (int i = 0; i <= (int)head[u].size() - 1; i++) {
int v = head[u][i];
DFS(v);
F[u][0] = ((long long)F[u][0] * (F[v][0] + F[v][1])) % modul;
}
G[(int)head[u].size()] = 1;
for (int i = (int)head[u].size() - 1; i >= 0; i--) {
int v = head[u][i];
G[i] = ((long long)G[i + 1] * (F[v][0] + F[v][1])) % modul;
}
F[u][1] = (c[u] == 1);
int cur = 1;
for (int i = 0; i <= (int)head[u].size() - 1; i++) {
int v = head[u][i];
if (c[u] == 1) {
F[u][1] = ((long long)F[u][1] * (F[v][0] + F[v][1])) % modul;
} else {
F[u][1] =
(F[u][1] + (((long long)cur * G[i + 1]) % modul) * F[v][1]) % modul;
cur = ((long long)cur * (F[v][0] + F[v][1])) % modul;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n - 1; i++) {
int j;
cin >> j;
head[j].push_back(i);
}
for (int i = 0; i <= n - 1; i++) cin >> c[i];
DFS(0);
cout << F[0][1];
return 0;
}
|
### Prompt
Your challenge is to write a CPP solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int modul = 1000000007;
const int nmax = 100100;
const double e = 1e-8;
const double pi = acos(-1);
int n, m, stest, c[nmax], F[nmax][2], G[nmax];
vector<int> head[nmax];
void DFS(int u) {
F[u][0] = (c[u] == 0);
for (int i = 0; i <= (int)head[u].size() - 1; i++) {
int v = head[u][i];
DFS(v);
F[u][0] = ((long long)F[u][0] * (F[v][0] + F[v][1])) % modul;
}
G[(int)head[u].size()] = 1;
for (int i = (int)head[u].size() - 1; i >= 0; i--) {
int v = head[u][i];
G[i] = ((long long)G[i + 1] * (F[v][0] + F[v][1])) % modul;
}
F[u][1] = (c[u] == 1);
int cur = 1;
for (int i = 0; i <= (int)head[u].size() - 1; i++) {
int v = head[u][i];
if (c[u] == 1) {
F[u][1] = ((long long)F[u][1] * (F[v][0] + F[v][1])) % modul;
} else {
F[u][1] =
(F[u][1] + (((long long)cur * G[i + 1]) % modul) * F[v][1]) % modul;
cur = ((long long)cur * (F[v][0] + F[v][1])) % modul;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n - 1; i++) {
int j;
cin >> j;
head[j].push_back(i);
}
for (int i = 0; i <= n - 1; i++) cin >> c[i];
DFS(0);
cout << F[0][1];
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n, x, cnt;
bool col[200010];
long long dp[200010][2], p[200010], a[200010], b[200010];
vector<int> vs[200010];
void dfs(int u, int fa) {
if (vs[u].size() == 1 && fa != 0) {
if (col[u]) {
dp[u][0] = 0;
dp[u][1] = 1;
} else {
dp[u][0] = 1;
dp[u][1] = 0;
}
return;
}
dp[u][0] = 1;
dp[u][1] = 0;
p[0] = 1;
for (int i = 0; i < vs[u].size(); i++) {
int v = vs[u][i];
if (v == fa) continue;
dfs(v, u);
dp[u][0] *= dp[v][0] + dp[v][1];
dp[u][0] %= 1000000007;
}
cnt = 0;
for (int i = 0; i < vs[u].size(); i++) {
int v = vs[u][i];
if (v == fa) continue;
b[++cnt] = v;
}
a[cnt + 1] = 1;
if (col[u]) {
dp[u][1] = dp[u][0];
dp[u][0] = 0;
return;
}
for (int i = 1; i <= cnt; i++) {
p[i] = p[i - 1] * (dp[b[i]][0] + dp[b[i]][1]);
p[i] %= 1000000007;
}
for (int i = cnt; i >= 1; i--) {
a[i] = a[i + 1] * (dp[b[i]][0] + dp[b[i]][1]);
a[i] %= 1000000007;
}
for (int i = 1; i <= cnt; i++) {
dp[u][1] += dp[b[i]][1] * ((p[i - 1] * a[i + 1]) % 1000000007);
dp[u][1] %= 1000000007;
}
return;
}
int main() {
scanf("%d", &n);
for (int i = 2; i <= n; i++) {
scanf("%d", &x);
x++;
vs[x].push_back(i);
vs[i].push_back(x);
}
for (int i = 1; i <= n; i++) {
cin >> col[i];
}
dfs(1, 0);
cout << dp[1][1];
return 0;
}
|
### Prompt
In Cpp, your task is to solve the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, x, cnt;
bool col[200010];
long long dp[200010][2], p[200010], a[200010], b[200010];
vector<int> vs[200010];
void dfs(int u, int fa) {
if (vs[u].size() == 1 && fa != 0) {
if (col[u]) {
dp[u][0] = 0;
dp[u][1] = 1;
} else {
dp[u][0] = 1;
dp[u][1] = 0;
}
return;
}
dp[u][0] = 1;
dp[u][1] = 0;
p[0] = 1;
for (int i = 0; i < vs[u].size(); i++) {
int v = vs[u][i];
if (v == fa) continue;
dfs(v, u);
dp[u][0] *= dp[v][0] + dp[v][1];
dp[u][0] %= 1000000007;
}
cnt = 0;
for (int i = 0; i < vs[u].size(); i++) {
int v = vs[u][i];
if (v == fa) continue;
b[++cnt] = v;
}
a[cnt + 1] = 1;
if (col[u]) {
dp[u][1] = dp[u][0];
dp[u][0] = 0;
return;
}
for (int i = 1; i <= cnt; i++) {
p[i] = p[i - 1] * (dp[b[i]][0] + dp[b[i]][1]);
p[i] %= 1000000007;
}
for (int i = cnt; i >= 1; i--) {
a[i] = a[i + 1] * (dp[b[i]][0] + dp[b[i]][1]);
a[i] %= 1000000007;
}
for (int i = 1; i <= cnt; i++) {
dp[u][1] += dp[b[i]][1] * ((p[i - 1] * a[i + 1]) % 1000000007);
dp[u][1] %= 1000000007;
}
return;
}
int main() {
scanf("%d", &n);
for (int i = 2; i <= n; i++) {
scanf("%d", &x);
x++;
vs[x].push_back(i);
vs[i].push_back(x);
}
for (int i = 1; i <= n; i++) {
cin >> col[i];
}
dfs(1, 0);
cout << dp[1][1];
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int abs(int x) {
if (x < 0) return -x;
return x;
}
int addmod(int v1, int v2) {
int v3 = v1 + v2;
if (v3 >= 1000000007) v3 -= 1000000007;
return v3;
}
long long dp[100005][2];
long long old[100005][2];
int color[100005];
vector<int> vv[100005];
void dfs(int pos) {
dp[pos][0] = !color[pos];
dp[pos][1] = color[pos];
for (int i = (0); i < (vv[pos].size()); i++) {
dfs(vv[pos][i]);
old[pos][0] = dp[pos][0];
old[pos][1] = dp[pos][1];
dp[pos][1] = dp[pos][0] = 0;
int v, u;
v = pos;
u = vv[pos][i];
dp[v][0] += (old[v][0] * dp[u][1]) % 1000000007;
dp[v][1] += (old[v][1] * dp[u][1]) % 1000000007;
dp[v][1] += (old[v][1] * dp[u][0]) % 1000000007;
dp[v][1] += (old[v][0] * dp[u][1]) % 1000000007;
dp[v][0] += (old[v][0] * dp[u][0]) % 1000000007;
dp[v][0] %= 1000000007;
dp[v][1] %= 1000000007;
}
}
int main() {
int n;
scanf("%d", &n);
int val;
for (int i = (0); i < (n - 1); i++) {
scanf("%d", &val);
vv[val].push_back(i + 1);
}
for (int i = (0); i < (n); i++) scanf("%d", &color[i]);
dfs(0);
cout << (dp[0][1]), cout << (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;
int abs(int x) {
if (x < 0) return -x;
return x;
}
int addmod(int v1, int v2) {
int v3 = v1 + v2;
if (v3 >= 1000000007) v3 -= 1000000007;
return v3;
}
long long dp[100005][2];
long long old[100005][2];
int color[100005];
vector<int> vv[100005];
void dfs(int pos) {
dp[pos][0] = !color[pos];
dp[pos][1] = color[pos];
for (int i = (0); i < (vv[pos].size()); i++) {
dfs(vv[pos][i]);
old[pos][0] = dp[pos][0];
old[pos][1] = dp[pos][1];
dp[pos][1] = dp[pos][0] = 0;
int v, u;
v = pos;
u = vv[pos][i];
dp[v][0] += (old[v][0] * dp[u][1]) % 1000000007;
dp[v][1] += (old[v][1] * dp[u][1]) % 1000000007;
dp[v][1] += (old[v][1] * dp[u][0]) % 1000000007;
dp[v][1] += (old[v][0] * dp[u][1]) % 1000000007;
dp[v][0] += (old[v][0] * dp[u][0]) % 1000000007;
dp[v][0] %= 1000000007;
dp[v][1] %= 1000000007;
}
}
int main() {
int n;
scanf("%d", &n);
int val;
for (int i = (0); i < (n - 1); i++) {
scanf("%d", &val);
vv[val].push_back(i + 1);
}
for (int i = (0); i < (n); i++) scanf("%d", &color[i]);
dfs(0);
cout << (dp[0][1]), cout << (endl);
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<int> graph[100005];
bool visited[100005];
bool black[100005];
long long int dp[100005][2];
long long int p = 1000000007;
void dfs(int n) {
visited[n] = 1;
if (black[n]) {
dp[n][1] = 1;
dp[n][0] = 0;
} else {
dp[n][0] = 1;
dp[n][1] = 0;
}
for (int i = 0; i < graph[n].size(); i++) {
if (visited[graph[n][i]]) {
continue;
} else {
dfs(graph[n][i]);
if (black[n] == 1) {
dp[n][1] = (dp[n][1] * (dp[graph[n][i]][0] + dp[graph[n][i]][1])) % p;
} else {
dp[n][1] = (dp[n][1] * (dp[graph[n][i]][0] + dp[graph[n][i]][1])) % p;
dp[n][1] += (dp[graph[n][i]][1] * dp[n][0]) % p;
dp[n][0] = (dp[n][0] * (dp[graph[n][i]][0] + dp[graph[n][i]][1])) % p;
}
}
}
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n - 1; i++) {
int z;
cin >> z;
graph[i + 1].push_back(z);
graph[z].push_back(i + 1);
}
for (int i = 0; i < n; i++) {
cin >> black[i];
}
dfs(0);
cout << ((dp[0][1]) % p);
}
|
### 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> graph[100005];
bool visited[100005];
bool black[100005];
long long int dp[100005][2];
long long int p = 1000000007;
void dfs(int n) {
visited[n] = 1;
if (black[n]) {
dp[n][1] = 1;
dp[n][0] = 0;
} else {
dp[n][0] = 1;
dp[n][1] = 0;
}
for (int i = 0; i < graph[n].size(); i++) {
if (visited[graph[n][i]]) {
continue;
} else {
dfs(graph[n][i]);
if (black[n] == 1) {
dp[n][1] = (dp[n][1] * (dp[graph[n][i]][0] + dp[graph[n][i]][1])) % p;
} else {
dp[n][1] = (dp[n][1] * (dp[graph[n][i]][0] + dp[graph[n][i]][1])) % p;
dp[n][1] += (dp[graph[n][i]][1] * dp[n][0]) % p;
dp[n][0] = (dp[n][0] * (dp[graph[n][i]][0] + dp[graph[n][i]][1])) % p;
}
}
}
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n - 1; i++) {
int z;
cin >> z;
graph[i + 1].push_back(z);
graph[z].push_back(i + 1);
}
for (int i = 0; i < n; i++) {
cin >> black[i];
}
dfs(0);
cout << ((dp[0][1]) % p);
}
```
|
#include <bits/stdc++.h>
using namespace std;
struct edge {
int v, nxt;
};
int n;
const int MOD = 1000000007;
edge e[200020];
int g[100010];
int nume;
int color[100010];
long long f[100010][2];
void addedge(int u, int v) {
e[++nume].v = v;
e[nume].nxt = g[u];
g[u] = nume;
e[++nume].v = u;
e[nume].nxt = g[v];
g[v] = nume;
}
void init() {
int x, i;
nume = 0;
memset(g, 0, sizeof(g));
for (i = 1; i < n; i++) {
scanf("%d", &x);
addedge(x, i);
}
for (i = 0; i < n; i++) {
scanf("%d", &color[i]);
}
}
void dfs(int x, int fa) {
int i;
int y;
if (color[x] == 1) {
f[x][1] = 1;
f[x][0] = 0;
} else {
f[x][1] = 0;
f[x][0] = 1;
}
for (i = g[x]; i; i = e[i].nxt)
if (e[i].v != fa) {
y = e[i].v;
dfs(y, x);
f[x][1] = (f[x][1] * (f[y][0] + f[y][1]) + f[x][0] * f[y][1]) % MOD;
f[x][0] = f[x][0] * (f[y][0] + f[y][1]) % MOD;
}
}
int main() {
scanf("%d", &n);
init();
dfs(0, -1);
printf("%I64d\n", f[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;
struct edge {
int v, nxt;
};
int n;
const int MOD = 1000000007;
edge e[200020];
int g[100010];
int nume;
int color[100010];
long long f[100010][2];
void addedge(int u, int v) {
e[++nume].v = v;
e[nume].nxt = g[u];
g[u] = nume;
e[++nume].v = u;
e[nume].nxt = g[v];
g[v] = nume;
}
void init() {
int x, i;
nume = 0;
memset(g, 0, sizeof(g));
for (i = 1; i < n; i++) {
scanf("%d", &x);
addedge(x, i);
}
for (i = 0; i < n; i++) {
scanf("%d", &color[i]);
}
}
void dfs(int x, int fa) {
int i;
int y;
if (color[x] == 1) {
f[x][1] = 1;
f[x][0] = 0;
} else {
f[x][1] = 0;
f[x][0] = 1;
}
for (i = g[x]; i; i = e[i].nxt)
if (e[i].v != fa) {
y = e[i].v;
dfs(y, x);
f[x][1] = (f[x][1] * (f[y][0] + f[y][1]) + f[x][0] * f[y][1]) % MOD;
f[x][0] = f[x][0] * (f[y][0] + f[y][1]) % MOD;
}
}
int main() {
scanf("%d", &n);
init();
dfs(0, -1);
printf("%I64d\n", f[0][1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 100000;
const long long MOD = 1000000007;
int n, p[N], x[N];
vector<int> graph[N];
long long colored[N], not_colored[N];
void dfs(int u) {
if (x[u] == 0) {
long long haveNone = 1;
long long haveOne = 0;
not_colored[u] = 1;
for (int ed = 0; ed < (int)graph[u].size(); ed++) {
int v = graph[u][ed];
dfs(v);
if (x[v] == 0) {
haveOne = (haveOne * (not_colored[v] + colored[v]) % MOD) % MOD +
(haveNone * colored[v]) % MOD;
haveOne = haveOne % MOD;
haveNone = haveNone * ((not_colored[v] + colored[v]) % MOD) % MOD;
haveNone = haveNone % MOD;
} else {
haveOne = (haveOne * colored[v]) % MOD + (haveNone * colored[v]) % MOD;
haveOne = haveOne % MOD;
haveNone = haveNone * ((not_colored[v] + colored[v]) % MOD) % MOD;
haveNone = haveNone % MOD;
}
}
colored[u] = haveOne;
not_colored[u] = haveNone;
} else {
not_colored[u] = 0;
colored[u] = 1;
for (int ed = 0; ed < (int)graph[u].size(); ed++) {
int v = graph[u][ed];
dfs(v);
if (x[v] == 0) {
colored[u] = colored[u] * ((not_colored[v] + colored[v]) % MOD) % MOD;
colored[u] = colored[u] % MOD;
} else {
colored[u] = colored[u] * colored[v] % MOD;
colored[u] = colored[u] % MOD;
}
}
}
}
int main(void) {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
scanf("%d", &p[i]);
graph[p[i]].push_back(i);
}
p[0] = -1;
for (int i = 0; i < n; i++) scanf("%d", &x[i]);
dfs(0);
printf("%I64d\n", colored[0]);
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 N = 100000;
const long long MOD = 1000000007;
int n, p[N], x[N];
vector<int> graph[N];
long long colored[N], not_colored[N];
void dfs(int u) {
if (x[u] == 0) {
long long haveNone = 1;
long long haveOne = 0;
not_colored[u] = 1;
for (int ed = 0; ed < (int)graph[u].size(); ed++) {
int v = graph[u][ed];
dfs(v);
if (x[v] == 0) {
haveOne = (haveOne * (not_colored[v] + colored[v]) % MOD) % MOD +
(haveNone * colored[v]) % MOD;
haveOne = haveOne % MOD;
haveNone = haveNone * ((not_colored[v] + colored[v]) % MOD) % MOD;
haveNone = haveNone % MOD;
} else {
haveOne = (haveOne * colored[v]) % MOD + (haveNone * colored[v]) % MOD;
haveOne = haveOne % MOD;
haveNone = haveNone * ((not_colored[v] + colored[v]) % MOD) % MOD;
haveNone = haveNone % MOD;
}
}
colored[u] = haveOne;
not_colored[u] = haveNone;
} else {
not_colored[u] = 0;
colored[u] = 1;
for (int ed = 0; ed < (int)graph[u].size(); ed++) {
int v = graph[u][ed];
dfs(v);
if (x[v] == 0) {
colored[u] = colored[u] * ((not_colored[v] + colored[v]) % MOD) % MOD;
colored[u] = colored[u] % MOD;
} else {
colored[u] = colored[u] * colored[v] % MOD;
colored[u] = colored[u] % MOD;
}
}
}
}
int main(void) {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
scanf("%d", &p[i]);
graph[p[i]].push_back(i);
}
p[0] = -1;
for (int i = 0; i < n; i++) scanf("%d", &x[i]);
dfs(0);
printf("%I64d\n", colored[0]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7, N = 1e5 + 5;
vector<int> adj[N], col(N);
long long dp[N][2];
void dfs(int u, int p = 0) {
dp[u][col[u]] = 1;
for (auto v : adj[u]) {
if (v == p) continue;
dfs(v, u);
dp[u][1] = (dp[u][0] * dp[v][1] + dp[u][1] * (dp[v][0] + dp[v][1])) % MOD;
dp[u][0] = (dp[u][0] * (dp[v][0] + dp[v][1])) % MOD;
}
}
void solve() {
int n;
cin >> n;
for (int i = 0; i < n - 1; i++) {
int x;
cin >> x;
adj[x].push_back(i + 1);
adj[i + 1].push_back(x);
}
for (int i = 0; i < n; i++) cin >> col[i];
dfs(0);
cout << dp[0][1];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
solve();
}
|
### Prompt
Develop a solution in cpp to the problem described below:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7, N = 1e5 + 5;
vector<int> adj[N], col(N);
long long dp[N][2];
void dfs(int u, int p = 0) {
dp[u][col[u]] = 1;
for (auto v : adj[u]) {
if (v == p) continue;
dfs(v, u);
dp[u][1] = (dp[u][0] * dp[v][1] + dp[u][1] * (dp[v][0] + dp[v][1])) % MOD;
dp[u][0] = (dp[u][0] * (dp[v][0] + dp[v][1])) % MOD;
}
}
void solve() {
int n;
cin >> n;
for (int i = 0; i < n - 1; i++) {
int x;
cin >> x;
adj[x].push_back(i + 1);
adj[i + 1].push_back(x);
}
for (int i = 0; i < n; i++) cin >> col[i];
dfs(0);
cout << dp[0][1];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
solve();
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long mXn = 1e5 + 10, M = 1e9 + 7;
vector<long long> ad[mXn];
bool b[mXn], mark[mXn];
long long dp0[mXn], dp1[mXn];
void dfs(long long v) {
if (b[v]) {
dp1[v] = 1;
} else {
dp0[v] = 1;
}
mark[v] = true;
for (long long i = 0; i < (long long)ad[v].size(); i++) {
long long u = ad[v][i];
if (!mark[u]) {
dfs(u);
dp1[v] = (dp1[v] * (dp1[u] + dp0[u]) + dp0[v] * dp1[u]) % M;
dp0[v] = (dp0[v] * (dp1[u] + dp0[u])) % M;
}
}
}
int32_t main() {
long long n;
cin >> n;
for (long long i = 0; i < n - 1; i++) {
long long x;
cin >> x;
ad[x].push_back(i + 1);
ad[i + 1].push_back(x);
}
for (long long i = 0; i < n; i++) {
long long f;
cin >> f;
b[i] = f;
}
dfs(0);
cout << dp1[0];
return 0;
}
|
### Prompt
Develop a solution in cpp to the problem described below:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long mXn = 1e5 + 10, M = 1e9 + 7;
vector<long long> ad[mXn];
bool b[mXn], mark[mXn];
long long dp0[mXn], dp1[mXn];
void dfs(long long v) {
if (b[v]) {
dp1[v] = 1;
} else {
dp0[v] = 1;
}
mark[v] = true;
for (long long i = 0; i < (long long)ad[v].size(); i++) {
long long u = ad[v][i];
if (!mark[u]) {
dfs(u);
dp1[v] = (dp1[v] * (dp1[u] + dp0[u]) + dp0[v] * dp1[u]) % M;
dp0[v] = (dp0[v] * (dp1[u] + dp0[u])) % M;
}
}
}
int32_t main() {
long long n;
cin >> n;
for (long long i = 0; i < n - 1; i++) {
long long x;
cin >> x;
ad[x].push_back(i + 1);
ad[i + 1].push_back(x);
}
for (long long i = 0; i < n; i++) {
long long f;
cin >> f;
b[i] = f;
}
dfs(0);
cout << dp1[0];
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long mod = 1000000007;
vector<int> adj[200000];
long color[200000];
long long dp[200000][2];
long n;
void dfs(long u) {
dp[u][color[u]] = 1;
long v;
for (int k = 0; k < adj[u].size(); k++) {
v = adj[u][k];
dfs(v);
dp[u][1] = ((dp[u][1] * dp[v][1]) % mod + (dp[u][1] * dp[v][0]) % mod +
(dp[u][0] * dp[v][1]) % mod) %
mod;
dp[u][0] =
((dp[u][0] * dp[v][0]) % mod + (dp[u][0] * dp[v][1]) % mod) % mod;
}
}
int main() {
long x;
cin >> n;
for (int i = 1; i < n; i++) {
cin >> x;
adj[x].push_back(i);
}
for (int i = 0; i < n; i++) cin >> color[i];
dfs(0);
cout << 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 long mod = 1000000007;
vector<int> adj[200000];
long color[200000];
long long dp[200000][2];
long n;
void dfs(long u) {
dp[u][color[u]] = 1;
long v;
for (int k = 0; k < adj[u].size(); k++) {
v = adj[u][k];
dfs(v);
dp[u][1] = ((dp[u][1] * dp[v][1]) % mod + (dp[u][1] * dp[v][0]) % mod +
(dp[u][0] * dp[v][1]) % mod) %
mod;
dp[u][0] =
((dp[u][0] * dp[v][0]) % mod + (dp[u][0] * dp[v][1]) % mod) % mod;
}
}
int main() {
long x;
cin >> n;
for (int i = 1; i < n; i++) {
cin >> x;
adj[x].push_back(i);
}
for (int i = 0; i < n; i++) cin >> color[i];
dfs(0);
cout << dp[0][1];
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void Cin(T &x) {
char c;
T sign = 1;
x = 0;
for (c = getchar(); c < '0' || c > '9'; c = getchar())
if (c == '-') sign = -1;
for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0';
x *= sign;
}
const int N = 1e5 + 7;
const int mod = 1e9 + 7;
vector<int> a[N];
int x, n, chk[N];
long long f[N], c[N], g[N];
void DFS(int u) {
chk[u] = g[u] = 1;
f[u] = 0;
for (int v : a[u]) {
if (chk[v]) continue;
DFS(v);
f[u] = (f[u] * g[v]) % mod;
f[u] = (f[u] + (g[u] * f[v] % mod)) % mod;
g[u] = (g[u] * g[v]) % mod;
}
if (c[u])
f[u] = g[u];
else
g[u] = (g[u] + f[u]) % mod;
}
void Enter() {
cin >> n;
for (int i = 1; i <= n - 1; ++i) cin >> x, a[x].push_back(i);
for (int i = 0; i <= n - 1; ++i) cin >> c[i];
}
void Solve() {
DFS(0);
cout << f[0] % mod;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
Enter();
Solve();
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;
template <typename T>
inline void Cin(T &x) {
char c;
T sign = 1;
x = 0;
for (c = getchar(); c < '0' || c > '9'; c = getchar())
if (c == '-') sign = -1;
for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0';
x *= sign;
}
const int N = 1e5 + 7;
const int mod = 1e9 + 7;
vector<int> a[N];
int x, n, chk[N];
long long f[N], c[N], g[N];
void DFS(int u) {
chk[u] = g[u] = 1;
f[u] = 0;
for (int v : a[u]) {
if (chk[v]) continue;
DFS(v);
f[u] = (f[u] * g[v]) % mod;
f[u] = (f[u] + (g[u] * f[v] % mod)) % mod;
g[u] = (g[u] * g[v]) % mod;
}
if (c[u])
f[u] = g[u];
else
g[u] = (g[u] + f[u]) % mod;
}
void Enter() {
cin >> n;
for (int i = 1; i <= n - 1; ++i) cin >> x, a[x].push_back(i);
for (int i = 0; i <= n - 1; ++i) cin >> c[i];
}
void Solve() {
DFS(0);
cout << f[0] % mod;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
Enter();
Solve();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<int> graph[100010];
int arr[100010] = {0};
int vis[100010] = {0};
long long dp[100010][2];
void dfs(int u) {
vis[u] = 1;
dp[u][0] = 1 - arr[u];
dp[u][1] = arr[u];
vector<int>::iterator vec;
for (vec = graph[u].begin(); vec != graph[u].end(); vec++) {
int i = (*vec);
if (vis[i])
continue;
else {
dfs(i);
long long zero, one;
zero = dp[u][0];
one = dp[u][1];
dp[u][0] = (zero * dp[i][1]) % 1000000007;
dp[u][1] = (one * dp[i][1]) % 1000000007;
dp[u][0] += (zero * dp[i][0]) % 1000000007;
dp[u][0] = dp[u][0] % 1000000007;
dp[u][1] += (zero * dp[i][1] + one * dp[i][0]) % 1000000007;
dp[u][1] = dp[u][1] % 1000000007;
}
}
}
int main() {
int n, v;
cin >> n;
for (int i = 1; i < n; i++) {
cin >> v;
graph[v].push_back(i);
graph[i].push_back(v);
}
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
dfs(0);
cout << dp[0][1] << endl;
}
|
### Prompt
Your task is to create a Cpp solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> graph[100010];
int arr[100010] = {0};
int vis[100010] = {0};
long long dp[100010][2];
void dfs(int u) {
vis[u] = 1;
dp[u][0] = 1 - arr[u];
dp[u][1] = arr[u];
vector<int>::iterator vec;
for (vec = graph[u].begin(); vec != graph[u].end(); vec++) {
int i = (*vec);
if (vis[i])
continue;
else {
dfs(i);
long long zero, one;
zero = dp[u][0];
one = dp[u][1];
dp[u][0] = (zero * dp[i][1]) % 1000000007;
dp[u][1] = (one * dp[i][1]) % 1000000007;
dp[u][0] += (zero * dp[i][0]) % 1000000007;
dp[u][0] = dp[u][0] % 1000000007;
dp[u][1] += (zero * dp[i][1] + one * dp[i][0]) % 1000000007;
dp[u][1] = dp[u][1] % 1000000007;
}
}
}
int main() {
int n, v;
cin >> n;
for (int i = 1; i < n; i++) {
cin >> v;
graph[v].push_back(i);
graph[i].push_back(v);
}
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
dfs(0);
cout << dp[0][1] << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int MAXN = 1e5 + 10;
vector<int> G[MAXN];
int color[MAXN];
long long dp[MAXN][2];
void init() {
for (int i = 0; i < MAXN; i++) {
G[i].clear();
}
}
long long qpow(long long x, long long k) {
long long res = 1;
while (k > 0) {
if (k & 1) {
res = (res * x) % MOD;
}
x = (x * x) % MOD;
k >>= 1;
}
return res;
}
void DFS(int u, int pa) {
dp[u][0] = 1;
dp[u][1] = 0;
long long tmp1 = 0, tmp2 = 1;
for (int v : G[u]) {
if (v != pa) {
DFS(v, u);
tmp1 = (tmp1 + (dp[v][1] * qpow(dp[v][0], MOD - 2)) % MOD) % MOD;
tmp2 = (tmp2 * dp[v][0]) % MOD;
dp[u][0] = (dp[u][0] * dp[v][0]) % MOD;
}
}
dp[u][1] = (tmp2 * tmp1) % MOD;
if (color[u] == 1) {
dp[u][1] = dp[u][0];
} else {
dp[u][0] += dp[u][1];
}
}
int main() {
int n;
while (~scanf("%d", &n)) {
init();
for (int i = 0; i <= n - 2; i++) {
int u;
scanf("%d", &u);
G[u].push_back(i + 1);
G[i + 1].push_back(u);
}
for (int i = 0; i < n; i++) {
scanf("%d", color + i);
}
DFS(0, -1);
printf("%I64d\n", dp[0][1] % MOD);
}
return 0;
}
|
### Prompt
Generate a cpp solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int MAXN = 1e5 + 10;
vector<int> G[MAXN];
int color[MAXN];
long long dp[MAXN][2];
void init() {
for (int i = 0; i < MAXN; i++) {
G[i].clear();
}
}
long long qpow(long long x, long long k) {
long long res = 1;
while (k > 0) {
if (k & 1) {
res = (res * x) % MOD;
}
x = (x * x) % MOD;
k >>= 1;
}
return res;
}
void DFS(int u, int pa) {
dp[u][0] = 1;
dp[u][1] = 0;
long long tmp1 = 0, tmp2 = 1;
for (int v : G[u]) {
if (v != pa) {
DFS(v, u);
tmp1 = (tmp1 + (dp[v][1] * qpow(dp[v][0], MOD - 2)) % MOD) % MOD;
tmp2 = (tmp2 * dp[v][0]) % MOD;
dp[u][0] = (dp[u][0] * dp[v][0]) % MOD;
}
}
dp[u][1] = (tmp2 * tmp1) % MOD;
if (color[u] == 1) {
dp[u][1] = dp[u][0];
} else {
dp[u][0] += dp[u][1];
}
}
int main() {
int n;
while (~scanf("%d", &n)) {
init();
for (int i = 0; i <= n - 2; i++) {
int u;
scanf("%d", &u);
G[u].push_back(i + 1);
G[i + 1].push_back(u);
}
for (int i = 0; i < n; i++) {
scanf("%d", color + i);
}
DFS(0, -1);
printf("%I64d\n", dp[0][1] % MOD);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int N;
vector<int> adj[100001];
int color[100001];
long long dp[100001][2];
void dfs(int node) {
dp[node][0] = 1;
dp[node][1] = 0;
for (int X : adj[node]) {
dfs(X);
dp[node][1] *= dp[X][0];
dp[node][1] += dp[node][0] * dp[X][1];
dp[node][0] *= dp[X][0];
dp[node][0] %= 1000000007;
dp[node][1] %= 1000000007;
}
if (color[node] == 1)
dp[node][1] = dp[node][0];
else
dp[node][0] += dp[node][1];
}
int main() {
cin >> N;
for (int i = 1; i < N; i++) {
int X;
cin >> X;
adj[X].push_back(i);
}
for (int i = 0; i < N; i++) cin >> color[i];
dfs(0);
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;
int N;
vector<int> adj[100001];
int color[100001];
long long dp[100001][2];
void dfs(int node) {
dp[node][0] = 1;
dp[node][1] = 0;
for (int X : adj[node]) {
dfs(X);
dp[node][1] *= dp[X][0];
dp[node][1] += dp[node][0] * dp[X][1];
dp[node][0] *= dp[X][0];
dp[node][0] %= 1000000007;
dp[node][1] %= 1000000007;
}
if (color[node] == 1)
dp[node][1] = dp[node][0];
else
dp[node][0] += dp[node][1];
}
int main() {
cin >> N;
for (int i = 1; i < N; i++) {
int X;
cin >> X;
adj[X].push_back(i);
}
for (int i = 0; i < N; i++) cin >> color[i];
dfs(0);
cout << dp[0][1] << '\n';
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long n, m, i, j, x, dp[123456][2], p[123456], mod = 1e9 + 7;
int main() {
ios::sync_with_stdio(false);
;
cin >> n;
for (i = 1; i < n; i++) {
cin >> p[i];
}
for (i = 0; i < n; i++) {
cin >> x;
dp[i][x] = 1;
}
for (i = n - 1; i; i--) {
dp[p[i]][1] = dp[i][0] * dp[p[i]][1] + dp[i][1] * dp[p[i]][1] +
dp[p[i]][0] * dp[i][1];
dp[p[i]][0] = dp[i][0] * dp[p[i]][0] + dp[i][1] * dp[p[i]][0];
dp[p[i]][0] %= mod;
dp[p[i]][1] %= mod;
}
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;
long long n, m, i, j, x, dp[123456][2], p[123456], mod = 1e9 + 7;
int main() {
ios::sync_with_stdio(false);
;
cin >> n;
for (i = 1; i < n; i++) {
cin >> p[i];
}
for (i = 0; i < n; i++) {
cin >> x;
dp[i][x] = 1;
}
for (i = n - 1; i; i--) {
dp[p[i]][1] = dp[i][0] * dp[p[i]][1] + dp[i][1] * dp[p[i]][1] +
dp[p[i]][0] * dp[i][1];
dp[p[i]][0] = dp[i][0] * dp[p[i]][0] + dp[i][1] * dp[p[i]][0];
dp[p[i]][0] %= mod;
dp[p[i]][1] %= mod;
}
cout << dp[0][1] << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
long long dp[100004][2];
int n;
vector<int> g[100004];
int v[100004];
void dfs(int x) {
dp[x][v[x]] = 1;
for (int i = 0; i < g[x].size(); i++) {
int y = g[x][i];
dfs(y);
dp[x][1] = dp[x][1] * (dp[y][0] + dp[y][1]) % MOD;
dp[x][1] = (dp[x][1] + dp[y][1] * dp[x][0]) % MOD;
dp[x][0] = dp[x][0] * (dp[y][0] + dp[y][1]) % MOD;
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int x;
scanf("%d", &x);
g[x].push_back(i);
}
for (int i = 0; i < n; i++) scanf("%d", &v[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;
const long long MOD = 1e9 + 7;
long long dp[100004][2];
int n;
vector<int> g[100004];
int v[100004];
void dfs(int x) {
dp[x][v[x]] = 1;
for (int i = 0; i < g[x].size(); i++) {
int y = g[x][i];
dfs(y);
dp[x][1] = dp[x][1] * (dp[y][0] + dp[y][1]) % MOD;
dp[x][1] = (dp[x][1] + dp[y][1] * dp[x][0]) % MOD;
dp[x][0] = dp[x][0] * (dp[y][0] + dp[y][1]) % MOD;
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int x;
scanf("%d", &x);
g[x].push_back(i);
}
for (int i = 0; i < n; i++) scanf("%d", &v[i]);
dfs(0);
cout << dp[0][1];
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const int M = 1e9 + 7;
enum Color { W, B };
int n, x[N], p[N], f[N][2];
vector<int> g[N];
void Dfs(int u) {
f[u][W] = (x[u] == W);
f[u][B] = (x[u] == B);
for (auto v : g[u]) {
int t[2];
memcpy(t, f[u], sizeof(t));
Dfs(v);
t[W] = (x[u] == W ? ((((1ll * (f[u][W]) * (f[v][W]) % M)) +
((1ll * (f[u][W]) * (f[v][B]) % M))) %
M)
: 0);
t[B] = ((((1ll * (f[u][B]) * (f[v][W]) % M)) +
((1ll * (f[u][B]) * (f[v][B]) % M))) %
M);
if (x[u] == W) t[B] = (((t[B]) + ((1ll * (f[u][W]) * (f[v][B]) % M))) % M);
memcpy(f[u], t, sizeof(t));
}
}
int main() {
cin.sync_with_stdio(0);
while (cin >> n) {
for (int i = 0; i < n; i++) g[i].clear();
for (int i = 0; i < n - 1; i++) {
int u;
cin >> u;
int v = i + 1;
g[u].push_back(v);
}
for (int i = 0; i < n; i++) cin >> x[i];
Dfs(0);
cout << f[0][B] << 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 N = 1e5 + 10;
const int M = 1e9 + 7;
enum Color { W, B };
int n, x[N], p[N], f[N][2];
vector<int> g[N];
void Dfs(int u) {
f[u][W] = (x[u] == W);
f[u][B] = (x[u] == B);
for (auto v : g[u]) {
int t[2];
memcpy(t, f[u], sizeof(t));
Dfs(v);
t[W] = (x[u] == W ? ((((1ll * (f[u][W]) * (f[v][W]) % M)) +
((1ll * (f[u][W]) * (f[v][B]) % M))) %
M)
: 0);
t[B] = ((((1ll * (f[u][B]) * (f[v][W]) % M)) +
((1ll * (f[u][B]) * (f[v][B]) % M))) %
M);
if (x[u] == W) t[B] = (((t[B]) + ((1ll * (f[u][W]) * (f[v][B]) % M))) % M);
memcpy(f[u], t, sizeof(t));
}
}
int main() {
cin.sync_with_stdio(0);
while (cin >> n) {
for (int i = 0; i < n; i++) g[i].clear();
for (int i = 0; i < n - 1; i++) {
int u;
cin >> u;
int v = i + 1;
g[u].push_back(v);
}
for (int i = 0; i < n; i++) cin >> x[i];
Dfs(0);
cout << f[0][B] << endl;
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 100005;
const long long MOD = 1000000007;
template <class T>
inline void read(T &x) {
x = 0;
int f = 0;
char ch = getchar();
while (ch < '0' || ch > '9') {
f |= (ch == '-');
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
x = f ? -x : x;
return;
}
int n, head[N], cnt, x;
long long f[N][2];
struct edge {
int v, nxt;
} e[N];
inline void add(int u, int v) { e[++cnt] = (edge){v, head[u]}, head[u] = cnt; }
void dfs(int u) {
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].v;
dfs(v);
f[u][1] = ((f[v][0] + f[v][1]) * f[u][1] % MOD + f[u][0] * f[v][1]) % MOD;
f[u][0] = f[u][0] * (f[v][1] + f[v][0]) % MOD;
}
}
int main() {
read(n);
for (int i = 2; i <= n; ++i) read(x), add(x + 1, i);
for (int i = 1; i <= n; ++i) read(x), f[i][x] = 1;
dfs(1);
printf("%lld", f[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>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 100005;
const long long MOD = 1000000007;
template <class T>
inline void read(T &x) {
x = 0;
int f = 0;
char ch = getchar();
while (ch < '0' || ch > '9') {
f |= (ch == '-');
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
x = f ? -x : x;
return;
}
int n, head[N], cnt, x;
long long f[N][2];
struct edge {
int v, nxt;
} e[N];
inline void add(int u, int v) { e[++cnt] = (edge){v, head[u]}, head[u] = cnt; }
void dfs(int u) {
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].v;
dfs(v);
f[u][1] = ((f[v][0] + f[v][1]) * f[u][1] % MOD + f[u][0] * f[v][1]) % MOD;
f[u][0] = f[u][0] * (f[v][1] + f[v][0]) % MOD;
}
}
int main() {
read(n);
for (int i = 2; i <= n; ++i) read(x), add(x + 1, i);
for (int i = 1; i <= n; ++i) read(x), f[i][x] = 1;
dfs(1);
printf("%lld", f[1][1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > G;
vector<int> COLOR;
vector<vector<long long> > DP;
const long long mod = 1e9 + 7;
void solve(int vId, int parentId) {
if (G[vId].size() > 1 || vId == 0) {
for (int i = 0; i < G[vId].size(); ++i) {
if (parentId != G[vId][i]) solve(G[vId][i], vId);
}
if (COLOR[vId] == 0) {
deque<long long> lRange;
deque<long long> center;
long long prev = 1;
for (int i = 0; i < G[vId].size(); ++i) {
int w = G[vId][i];
if (w != parentId) {
lRange.push_back(prev * (DP[w][0] + DP[w][1]));
lRange[lRange.size() - 1] %= mod;
prev = lRange[lRange.size() - 1];
center.push_back(DP[w][1]);
}
}
deque<long long> rRange;
prev = 1;
for (int i = (int)G[vId].size() - 1; i >= 0; --i) {
int w = G[vId][i];
if (w != parentId) {
rRange.push_front(prev * (DP[w][0] + DP[w][1]));
rRange[0] %= mod;
prev = rRange[0];
}
}
DP[vId][1] = 0;
for (int i = 0; i < center.size(); ++i) {
long long left = i == 0 ? 1 : lRange[i - 1];
long long right = i == center.size() - 1 ? 1 : rRange[i + 1];
DP[vId][1] += ((left * center[i]) % mod) * right;
DP[vId][1] %= mod;
}
long long su = 1;
for (int i = 0; i < G[vId].size(); ++i) {
int w = G[vId][i];
if (w != parentId) su = su * (DP[w][0] + DP[w][1]);
su %= mod;
}
DP[vId][0] = su;
} else {
DP[vId][0] = 0;
long long su = 1;
for (int i = 0; i < G[vId].size(); ++i) {
int w = G[vId][i];
if (w != parentId) su = su * (DP[w][0] + DP[w][1]);
su %= mod;
}
DP[vId][1] = su;
}
} else {
if (COLOR[vId] == 1) {
DP[vId][1] = 1;
DP[vId][0] = 0;
} else {
DP[vId][1] = 0;
DP[vId][0] = 1;
}
}
}
int main() {
int n;
cin >> n;
DP = vector<vector<long long> >(n, vector<long long>(2, 0));
vector<vector<int> > graph(n, vector<int>());
for (int i = 0; i < n - 1; ++i) {
int pi;
cin >> pi;
graph[i + 1].push_back(pi);
graph[pi].push_back(i + 1);
}
G = graph;
vector<int> color(n);
for (int i = 0; i < n; ++i) cin >> color[i];
COLOR = color;
solve(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;
vector<vector<int> > G;
vector<int> COLOR;
vector<vector<long long> > DP;
const long long mod = 1e9 + 7;
void solve(int vId, int parentId) {
if (G[vId].size() > 1 || vId == 0) {
for (int i = 0; i < G[vId].size(); ++i) {
if (parentId != G[vId][i]) solve(G[vId][i], vId);
}
if (COLOR[vId] == 0) {
deque<long long> lRange;
deque<long long> center;
long long prev = 1;
for (int i = 0; i < G[vId].size(); ++i) {
int w = G[vId][i];
if (w != parentId) {
lRange.push_back(prev * (DP[w][0] + DP[w][1]));
lRange[lRange.size() - 1] %= mod;
prev = lRange[lRange.size() - 1];
center.push_back(DP[w][1]);
}
}
deque<long long> rRange;
prev = 1;
for (int i = (int)G[vId].size() - 1; i >= 0; --i) {
int w = G[vId][i];
if (w != parentId) {
rRange.push_front(prev * (DP[w][0] + DP[w][1]));
rRange[0] %= mod;
prev = rRange[0];
}
}
DP[vId][1] = 0;
for (int i = 0; i < center.size(); ++i) {
long long left = i == 0 ? 1 : lRange[i - 1];
long long right = i == center.size() - 1 ? 1 : rRange[i + 1];
DP[vId][1] += ((left * center[i]) % mod) * right;
DP[vId][1] %= mod;
}
long long su = 1;
for (int i = 0; i < G[vId].size(); ++i) {
int w = G[vId][i];
if (w != parentId) su = su * (DP[w][0] + DP[w][1]);
su %= mod;
}
DP[vId][0] = su;
} else {
DP[vId][0] = 0;
long long su = 1;
for (int i = 0; i < G[vId].size(); ++i) {
int w = G[vId][i];
if (w != parentId) su = su * (DP[w][0] + DP[w][1]);
su %= mod;
}
DP[vId][1] = su;
}
} else {
if (COLOR[vId] == 1) {
DP[vId][1] = 1;
DP[vId][0] = 0;
} else {
DP[vId][1] = 0;
DP[vId][0] = 1;
}
}
}
int main() {
int n;
cin >> n;
DP = vector<vector<long long> >(n, vector<long long>(2, 0));
vector<vector<int> > graph(n, vector<int>());
for (int i = 0; i < n - 1; ++i) {
int pi;
cin >> pi;
graph[i + 1].push_back(pi);
graph[pi].push_back(i + 1);
}
G = graph;
vector<int> color(n);
for (int i = 0; i < n; ++i) cin >> color[i];
COLOR = color;
solve(0, -1);
cout << DP[0][1] << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using dd = long double;
namespace {
static bool constexpr dbg = 0;
ll constexpr N = 1.1e5, MO = 1e9 + 7;
ll n, d[N][2];
bool bl[N];
vector<ll> adj[N];
void init() {
cin >> n;
for (ll i = 1; i <= (ll)(n - 1); ++i) {
ll p;
cin >> p;
adj[i].push_back(p);
adj[p].push_back(i);
}
for (ll i = 0; i < (ll)(n); ++i) cin >> bl[i];
}
void dfs(ll c, ll p) {
d[c][0] = 1;
d[c][1] = 0;
for (auto ch : adj[c])
if (ch != p) {
dfs(ch, c);
d[c][1] = (d[c][1] * d[ch][0] + d[c][0] * d[ch][1]) % MO;
d[c][0] = (d[c][0] * d[ch][0]) % MO;
}
if (bl[c])
d[c][1] = d[c][0];
else
d[c][0] = (d[c][0] + d[c][1]) % MO;
}
void solve() {
dfs(0, -1);
cout << d[0][1];
}
} // namespace
int main() {
if (!dbg) {
std::ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
cout << fixed << setprecision(20);
init();
solve();
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;
using ll = long long;
using pll = pair<ll, ll>;
using dd = long double;
namespace {
static bool constexpr dbg = 0;
ll constexpr N = 1.1e5, MO = 1e9 + 7;
ll n, d[N][2];
bool bl[N];
vector<ll> adj[N];
void init() {
cin >> n;
for (ll i = 1; i <= (ll)(n - 1); ++i) {
ll p;
cin >> p;
adj[i].push_back(p);
adj[p].push_back(i);
}
for (ll i = 0; i < (ll)(n); ++i) cin >> bl[i];
}
void dfs(ll c, ll p) {
d[c][0] = 1;
d[c][1] = 0;
for (auto ch : adj[c])
if (ch != p) {
dfs(ch, c);
d[c][1] = (d[c][1] * d[ch][0] + d[c][0] * d[ch][1]) % MO;
d[c][0] = (d[c][0] * d[ch][0]) % MO;
}
if (bl[c])
d[c][1] = d[c][0];
else
d[c][0] = (d[c][0] + d[c][1]) % MO;
}
void solve() {
dfs(0, -1);
cout << d[0][1];
}
} // namespace
int main() {
if (!dbg) {
std::ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
cout << fixed << setprecision(20);
init();
solve();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
const int mod = 1e9 + 7;
int n, col[N];
long long dp[N][2];
vector<int> adj[N];
void dfs(int u) {
dp[u][0] = 1LL;
dp[u][1] = 0LL;
for (auto v : adj[u]) {
dfs(v);
dp[u][1] = (dp[u][1] * dp[v][0]) % mod;
dp[u][1] = (dp[u][1] + (dp[v][1] * dp[u][0]) % mod) % mod;
dp[u][0] = (dp[u][0] * dp[v][0]) % mod;
}
if (col[u] == 1) {
dp[u][1] = dp[u][0];
} else
dp[u][0] = (dp[u][0] + dp[u][1]) % mod;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 2; i <= n; ++i) {
int x;
cin >> x;
adj[x + 1].push_back(i);
}
for (int i = 1; i <= n; ++i) cin >> col[i];
dfs(1);
cout << dp[1][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;
const int N = 100010;
const int mod = 1e9 + 7;
int n, col[N];
long long dp[N][2];
vector<int> adj[N];
void dfs(int u) {
dp[u][0] = 1LL;
dp[u][1] = 0LL;
for (auto v : adj[u]) {
dfs(v);
dp[u][1] = (dp[u][1] * dp[v][0]) % mod;
dp[u][1] = (dp[u][1] + (dp[v][1] * dp[u][0]) % mod) % mod;
dp[u][0] = (dp[u][0] * dp[v][0]) % mod;
}
if (col[u] == 1) {
dp[u][1] = dp[u][0];
} else
dp[u][0] = (dp[u][0] + dp[u][1]) % mod;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 2; i <= n; ++i) {
int x;
cin >> x;
adj[x + 1].push_back(i);
}
for (int i = 1; i <= n; ++i) cin >> col[i];
dfs(1);
cout << dp[1][1] << '\n';
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MaxN = 1000005;
const double eps = 1e-8;
const double DINF = 1e100;
const int INF = 1000000006;
const long long LINF = 1000000000000000005ll;
const int mod = (int)1e9 + 7;
int n;
int color[MaxN];
vector<int> edges[MaxN];
long long f[MaxN][2];
long long tmp[MaxN][2];
bool vis[MaxN];
void dfs(int u) {
vector<int> sons;
for (auto v : edges[u]) {
if (vis[v]) continue;
vis[v] = 1;
dfs(v);
sons.push_back(v);
}
for (int i = 0; i <= sons.size(); ++i) tmp[i][0] = tmp[i][1] = 0;
if (color[u] == 1)
tmp[0][1] = 1;
else
tmp[0][0] = 1;
for (int i = 0; i < sons.size(); ++i) {
int v = sons[i];
tmp[i + 1][1] = (tmp[i + 1][1] + tmp[i][0] * f[v][1] % mod) % mod;
tmp[i + 1][1] =
(tmp[i + 1][1] + tmp[i][1] * (f[v][0] + f[v][1]) % mod) % mod;
tmp[i + 1][0] =
(tmp[i + 1][0] + tmp[i][0] * (f[v][0] + f[v][1]) % mod) % mod;
}
f[u][0] = tmp[sons.size()][0];
f[u][1] = tmp[sons.size()][1];
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; ++i) {
int x;
scanf("%d", &x);
edges[x].push_back(i);
edges[i].push_back(x);
}
for (int i = 0; i < n; ++i) scanf("%d", color + i);
vis[0] = 1;
dfs(0);
printf("%d\n", (int)f[0][1]);
return 0;
}
|
### Prompt
Develop a solution in Cpp to the problem described below:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MaxN = 1000005;
const double eps = 1e-8;
const double DINF = 1e100;
const int INF = 1000000006;
const long long LINF = 1000000000000000005ll;
const int mod = (int)1e9 + 7;
int n;
int color[MaxN];
vector<int> edges[MaxN];
long long f[MaxN][2];
long long tmp[MaxN][2];
bool vis[MaxN];
void dfs(int u) {
vector<int> sons;
for (auto v : edges[u]) {
if (vis[v]) continue;
vis[v] = 1;
dfs(v);
sons.push_back(v);
}
for (int i = 0; i <= sons.size(); ++i) tmp[i][0] = tmp[i][1] = 0;
if (color[u] == 1)
tmp[0][1] = 1;
else
tmp[0][0] = 1;
for (int i = 0; i < sons.size(); ++i) {
int v = sons[i];
tmp[i + 1][1] = (tmp[i + 1][1] + tmp[i][0] * f[v][1] % mod) % mod;
tmp[i + 1][1] =
(tmp[i + 1][1] + tmp[i][1] * (f[v][0] + f[v][1]) % mod) % mod;
tmp[i + 1][0] =
(tmp[i + 1][0] + tmp[i][0] * (f[v][0] + f[v][1]) % mod) % mod;
}
f[u][0] = tmp[sons.size()][0];
f[u][1] = tmp[sons.size()][1];
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; ++i) {
int x;
scanf("%d", &x);
edges[x].push_back(i);
edges[i].push_back(x);
}
for (int i = 0; i < n; ++i) scanf("%d", color + i);
vis[0] = 1;
dfs(0);
printf("%d\n", (int)f[0][1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n;
const int mo = 1000000007;
const int N = 200000;
long long f[N][2];
int t[N], s[N];
int main() {
scanf("%d", &n);
for (int i = 0; i < n - 1; i++) scanf("%d", t + i + 1);
for (int i = 0; i < n; i++) {
scanf("%d", &s[i]);
f[i][s[i]] = 1;
}
for (int i = n - 1; i > 0; i--) {
f[t[i]][1] = (f[t[i]][1] * (f[i][0] + f[i][1]) + f[t[i]][0] * f[i][1]) % mo;
f[t[i]][0] = (f[t[i]][0] * (f[i][0] + f[i][1])) % mo;
}
cout << f[0][1];
}
|
### Prompt
Construct a Cpp code solution to the problem outlined:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n;
const int mo = 1000000007;
const int N = 200000;
long long f[N][2];
int t[N], s[N];
int main() {
scanf("%d", &n);
for (int i = 0; i < n - 1; i++) scanf("%d", t + i + 1);
for (int i = 0; i < n; i++) {
scanf("%d", &s[i]);
f[i][s[i]] = 1;
}
for (int i = n - 1; i > 0; i--) {
f[t[i]][1] = (f[t[i]][1] * (f[i][0] + f[i][1]) + f[t[i]][0] * f[i][1]) % mo;
f[t[i]][0] = (f[t[i]][0] * (f[i][0] + f[i][1])) % mo;
}
cout << f[0][1];
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void smin(T &a, U b) {
if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, U b) {
if (a < b) a = b;
}
int power(int a, int b, int m, int ans = 1) {
for (; b; b >>= 1, a = 1LL * a * a % m)
if (b & 1) ans = 1LL * ans * a % m;
return ans;
}
int st[100100];
vector<int> adj[100100];
long long dp[100100][3];
void dfs(int u, int fa = 0) {
if (st[u] == 1)
dp[u][1] = 1;
else
dp[u][0] = 1;
for (int i = 0; i < adj[u].size(); i++) {
int v = adj[u][i];
if (v == fa) continue;
dfs(v, u);
dp[u][1] = dp[u][1] * (dp[v][0] + dp[v][1]) % 1000000007 +
dp[u][0] * dp[v][1] % 1000000007;
dp[u][1] %= 1000000007;
dp[u][0] = (dp[u][0] * dp[v][0] + dp[u][0] * dp[v][1]) % 1000000007;
}
}
int main() {
int n;
scanf("%d", &n);
for (int i = 2; i <= n; i++) {
int p;
scanf("%d", &p);
adj[i].push_back(p + 1);
adj[p + 1].push_back(i);
}
for (int i = 1; i <= n; i++) {
scanf("%d", &st[i]);
}
dfs(1);
cout << dp[1][1] << endl;
}
|
### Prompt
Generate a CPP solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void smin(T &a, U b) {
if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, U b) {
if (a < b) a = b;
}
int power(int a, int b, int m, int ans = 1) {
for (; b; b >>= 1, a = 1LL * a * a % m)
if (b & 1) ans = 1LL * ans * a % m;
return ans;
}
int st[100100];
vector<int> adj[100100];
long long dp[100100][3];
void dfs(int u, int fa = 0) {
if (st[u] == 1)
dp[u][1] = 1;
else
dp[u][0] = 1;
for (int i = 0; i < adj[u].size(); i++) {
int v = adj[u][i];
if (v == fa) continue;
dfs(v, u);
dp[u][1] = dp[u][1] * (dp[v][0] + dp[v][1]) % 1000000007 +
dp[u][0] * dp[v][1] % 1000000007;
dp[u][1] %= 1000000007;
dp[u][0] = (dp[u][0] * dp[v][0] + dp[u][0] * dp[v][1]) % 1000000007;
}
}
int main() {
int n;
scanf("%d", &n);
for (int i = 2; i <= n; i++) {
int p;
scanf("%d", &p);
adj[i].push_back(p + 1);
adj[p + 1].push_back(i);
}
for (int i = 1; i <= n; i++) {
scanf("%d", &st[i]);
}
dfs(1);
cout << dp[1][1] << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)1e5 + 10;
const int mod = (int)1e9 + 7;
int p[maxn];
long long d[maxn][2];
int n, x;
int main() {
scanf("%d", &n);
for (int i = 0; i < n - 1; i++) scanf("%d", p + i + 1);
for (int i = 0; i < n; i++) {
scanf("%d", &x);
d[i][x] = 1;
}
for (int i = n - 1; i > 0; i--) {
d[p[i]][1] =
(d[p[i]][1] * (d[i][0] + d[i][1]) + d[p[i]][0] * d[i][1]) % mod;
d[p[i]][0] = (d[p[i]][0] * (d[i][0] + d[i][1])) % mod;
}
cout << d[0][1] << endl;
}
|
### Prompt
Please create a solution in cpp to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)1e5 + 10;
const int mod = (int)1e9 + 7;
int p[maxn];
long long d[maxn][2];
int n, x;
int main() {
scanf("%d", &n);
for (int i = 0; i < n - 1; i++) scanf("%d", p + i + 1);
for (int i = 0; i < n; i++) {
scanf("%d", &x);
d[i][x] = 1;
}
for (int i = n - 1; i > 0; i--) {
d[p[i]][1] =
(d[p[i]][1] * (d[i][0] + d[i][1]) + d[p[i]][0] * d[i][1]) % mod;
d[p[i]][0] = (d[p[i]][0] * (d[i][0] + d[i][1])) % mod;
}
cout << d[0][1] << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[100050];
int b[100050];
long long dp[100050][2];
const int m = 1e9 + 7;
void dfs(int v, int p) {
long long old[2] = {0};
dp[v][0] = 1 - b[v];
dp[v][1] = b[v];
for (auto u : adj[v]) {
if (u == p) continue;
old[0] = dp[v][0];
old[1] = dp[v][1];
dp[v][0] = 0, dp[v][1] = 0;
dfs(u, v);
dp[v][0] += (dp[u][1] * old[0]) % m;
dp[v][1] += (dp[u][1] * old[1]) % m;
dp[v][1] += (old[1] * dp[u][0]) % m;
dp[v][1] += (old[0] * dp[u][1]) % m;
dp[v][0] += (old[0] * dp[u][0]) % m;
}
}
int main() {
int n;
scanf("%d", &n);
for (int(i) = 0; (i) < (n - 1); (i)++) {
int a;
scanf("%d", &a);
adj[a + 1].push_back(i + 2);
adj[i + 2].push_back(a + 1);
}
for (int(i) = 0; (i) < (n); (i)++) scanf("%d", &b[i + 1]);
dfs(1, 0);
cout << dp[1][1] % m;
return 0;
}
|
### Prompt
Please formulate a cpp solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[100050];
int b[100050];
long long dp[100050][2];
const int m = 1e9 + 7;
void dfs(int v, int p) {
long long old[2] = {0};
dp[v][0] = 1 - b[v];
dp[v][1] = b[v];
for (auto u : adj[v]) {
if (u == p) continue;
old[0] = dp[v][0];
old[1] = dp[v][1];
dp[v][0] = 0, dp[v][1] = 0;
dfs(u, v);
dp[v][0] += (dp[u][1] * old[0]) % m;
dp[v][1] += (dp[u][1] * old[1]) % m;
dp[v][1] += (old[1] * dp[u][0]) % m;
dp[v][1] += (old[0] * dp[u][1]) % m;
dp[v][0] += (old[0] * dp[u][0]) % m;
}
}
int main() {
int n;
scanf("%d", &n);
for (int(i) = 0; (i) < (n - 1); (i)++) {
int a;
scanf("%d", &a);
adj[a + 1].push_back(i + 2);
adj[i + 2].push_back(a + 1);
}
for (int(i) = 0; (i) < (n); (i)++) scanf("%d", &b[i + 1]);
dfs(1, 0);
cout << dp[1][1] % m;
return 0;
}
```
|
#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)];
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;
adj[x + 1].push_back(i + 2);
}
for (int i = 1; i <= n; i++) {
cin >> col[i];
}
adj[0].push_back(1);
memset(dp, -1, sizeof dp);
cout << go(0, 0, 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;
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)];
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;
adj[x + 1].push_back(i + 2);
}
for (int i = 1; i <= n; i++) {
cin >> col[i];
}
adj[0].push_back(1);
memset(dp, -1, sizeof dp);
cout << go(0, 0, 1) % 1000000007 << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7, N = 1e5 + 5;
int n, c[N], x;
long long dp[N][2];
vector<int> g[N];
void dfs(int v) {
dp[v][0] = 1LL;
for (int u : g[v]) {
dfs(u);
(dp[u][0] += dp[u][1]) %= mod;
dp[v][1] = (dp[v][1] * dp[u][0] + dp[v][0] * dp[u][1]) % mod;
dp[v][0] = (dp[v][0] * dp[u][0]) % mod;
}
if (c[v]) {
dp[v][1] = dp[v][0];
dp[v][0] = 0;
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n;
for (int i = 1; i < n; i++) {
cin >> x;
g[x].push_back(i);
}
for (int i = 0; i < n; i++) cin >> c[i];
dfs(0);
cout << dp[0][1];
}
|
### 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 mod = 1e9 + 7, N = 1e5 + 5;
int n, c[N], x;
long long dp[N][2];
vector<int> g[N];
void dfs(int v) {
dp[v][0] = 1LL;
for (int u : g[v]) {
dfs(u);
(dp[u][0] += dp[u][1]) %= mod;
dp[v][1] = (dp[v][1] * dp[u][0] + dp[v][0] * dp[u][1]) % mod;
dp[v][0] = (dp[v][0] * dp[u][0]) % mod;
}
if (c[v]) {
dp[v][1] = dp[v][0];
dp[v][0] = 0;
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n;
for (int i = 1; i < n; i++) {
cin >> x;
g[x].push_back(i);
}
for (int i = 0; i < n; i++) cin >> c[i];
dfs(0);
cout << dp[0][1];
}
```
|
#include <bits/stdc++.h>
using namespace std;
int sgn(double x) { return (x > 1e-8) - (x < -1e-8); }
int count_bit(int x) { return x == 0 ? 0 : count_bit(x >> 1) + (x & 1); }
template <class T>
inline void ckmin(T &a, const T b) {
if (b < a) a = b;
}
template <class T>
inline void ckmax(T &a, const T b) {
if (b > a) a = b;
}
const int mod = 1000000007;
int mul(int x, int y) { return (long long)x * y % mod; }
int fmod(int a, int p) {
int ret = 1;
while (p) {
if (p & 1) ret = mul(ret, a);
a = mul(a, a);
p >>= 1;
}
return ret;
}
int get_inv(int x) { return fmod(x, mod - 2); }
const int MAXN = 100000 + 10;
vector<int> g[MAXN];
int n, col[MAXN];
int dp[MAXN][2];
void dfs(int x, int f) {
int allmul = 1;
for (__typeof((g[x]).begin()) it = (g[x]).begin(); it != (g[x]).end(); ++it) {
if (*it != f) {
dfs(*it, x);
allmul = mul(allmul, dp[*it][0] + dp[*it][1]);
}
}
if (col[x]) {
dp[x][1] = allmul;
} else {
dp[x][0] = allmul;
for (__typeof((g[x]).begin()) it = (g[x]).begin(); it != (g[x]).end();
++it) {
if (*it != f) {
if (dp[*it][1]) {
dp[x][1] +=
mul(dp[*it][1], mul(allmul, get_inv(dp[*it][0] + dp[*it][1])));
dp[x][1] %= mod;
}
}
}
}
}
int main() {
while (scanf("%d", &n) != EOF) {
for (int i = 0; i < (n); ++i) {
g[i].clear();
}
for (int i = (1); i <= (n - 1); ++i) {
int pa;
scanf("%d", &pa);
g[pa].push_back(i);
g[i].push_back(pa);
}
for (int i = 0; i < (n); ++i) {
scanf("%d", &col[i]);
}
memset(dp, 0, sizeof(dp));
dfs(0, -1);
printf("%d\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;
int sgn(double x) { return (x > 1e-8) - (x < -1e-8); }
int count_bit(int x) { return x == 0 ? 0 : count_bit(x >> 1) + (x & 1); }
template <class T>
inline void ckmin(T &a, const T b) {
if (b < a) a = b;
}
template <class T>
inline void ckmax(T &a, const T b) {
if (b > a) a = b;
}
const int mod = 1000000007;
int mul(int x, int y) { return (long long)x * y % mod; }
int fmod(int a, int p) {
int ret = 1;
while (p) {
if (p & 1) ret = mul(ret, a);
a = mul(a, a);
p >>= 1;
}
return ret;
}
int get_inv(int x) { return fmod(x, mod - 2); }
const int MAXN = 100000 + 10;
vector<int> g[MAXN];
int n, col[MAXN];
int dp[MAXN][2];
void dfs(int x, int f) {
int allmul = 1;
for (__typeof((g[x]).begin()) it = (g[x]).begin(); it != (g[x]).end(); ++it) {
if (*it != f) {
dfs(*it, x);
allmul = mul(allmul, dp[*it][0] + dp[*it][1]);
}
}
if (col[x]) {
dp[x][1] = allmul;
} else {
dp[x][0] = allmul;
for (__typeof((g[x]).begin()) it = (g[x]).begin(); it != (g[x]).end();
++it) {
if (*it != f) {
if (dp[*it][1]) {
dp[x][1] +=
mul(dp[*it][1], mul(allmul, get_inv(dp[*it][0] + dp[*it][1])));
dp[x][1] %= mod;
}
}
}
}
}
int main() {
while (scanf("%d", &n) != EOF) {
for (int i = 0; i < (n); ++i) {
g[i].clear();
}
for (int i = (1); i <= (n - 1); ++i) {
int pa;
scanf("%d", &pa);
g[pa].push_back(i);
g[i].push_back(pa);
}
for (int i = 0; i < (n); ++i) {
scanf("%d", &col[i]);
}
memset(dp, 0, sizeof(dp));
dfs(0, -1);
printf("%d\n", dp[0][1]);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1000000007;
const long long BIG = 1446803456761533460LL;
const int Big = 336860180;
const long long int INF = LONG_LONG_MAX;
const vector<vector<long long int> > adj4({{0, 1}, {0, -1}, {1, 0}, {-1, 0}});
const vector<vector<long long int> > adj8(
{{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {-1, -1}, {1, -1}, {-1, 1}, {1, 1}});
long long int gcd(long long int a, long long int b) {
return b == 0 ? a : gcd(b, a % b);
}
bool isprime(long long int n) {
if (n == 2 || n == 3) return true;
if (n < 2 || n % 2 == 0 || n % 3 == 0) return false;
for (long long int i = 6; (i - 1) * (i - 1) <= n; i += 6) {
if (n % (i - 1) == 0 || n % (i + 1) == 0) {
return false;
}
}
return true;
}
vector<bool> sprime;
void genPrime(long long int sz) {
sprime = vector<bool>(sz, true);
sprime[0] = false;
sprime[1] = false;
for (long long int i = (2); i < (sz); ++i) {
if (sprime[i]) {
for (long long int j = i * i; j < sz; j += i) {
sprime[j] = false;
}
}
}
}
long long int powMod(long long int a, long long int b, long long int mod) {
long long int n = 1;
long long int p = a;
while (b > 0) {
if (b % 2 == 1) {
n *= p;
n %= mod;
}
p *= p;
p %= mod;
b /= 2;
}
return n;
}
long long int modularInverse(long long int a, long long int mod) {
return powMod(a, mod - 2, mod);
}
long long int binarysearch(long long int l, long long int r,
bool (*bsfunction)(long long int)) {
while (r - l > 1) {
long long int mid = (l + r) / 2;
bool val = bsfunction(mid);
if (val) {
r = mid;
} else {
l = mid;
}
}
return l;
}
stringstream sss;
const long long int maxn = 100010;
vector<long long int> g[maxn];
long long int col[maxn];
long long int dp[maxn][2];
void dfs(long long int x) {
long long int adjn = g[x].size();
for (auto y : g[x]) {
dfs(y);
}
vector<long long int> lt(adjn + 1), rt(adjn + 1);
lt[0] = 1;
rt[adjn] = 1;
for (long long int i = 0; i < (adjn); ++i) {
long long int y = g[x][i];
lt[i + 1] = lt[i] * (dp[y][0] + dp[y][1]) % MOD;
}
for (long long int i = (adjn)-1; i >= 0; --i) {
long long int y = g[x][i];
rt[i] = rt[i + 1] * (dp[y][0] + dp[y][1]) % MOD;
}
if (col[x]) {
dp[x][0] = 0;
dp[x][1] = lt[adjn];
} else {
dp[x][0] = lt[adjn];
for (long long int i = 0; i < (adjn); ++i) {
long long int y = g[x][i];
dp[x][1] += dp[y][1] * lt[i] % MOD * rt[i + 1] % MOD;
dp[x][1] %= MOD;
}
}
}
void MAIN() {
long long int n;
cin >> n;
for (long long int i = 0; i < (n - 1); ++i) {
long long int x;
cin >> x;
g[x].push_back(i + 1);
}
for (long long int i = 0; i < (n); ++i) {
cin >> col[i];
}
dfs(0);
cout << (dp[0][1]) << endl;
}
long long int TESTCASEN = 1;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(10);
sss << R"(
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
)";
for (long long int test = 0; test < (TESTCASEN); ++test) {
MAIN();
}
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 MOD = 1000000007;
const long long BIG = 1446803456761533460LL;
const int Big = 336860180;
const long long int INF = LONG_LONG_MAX;
const vector<vector<long long int> > adj4({{0, 1}, {0, -1}, {1, 0}, {-1, 0}});
const vector<vector<long long int> > adj8(
{{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {-1, -1}, {1, -1}, {-1, 1}, {1, 1}});
long long int gcd(long long int a, long long int b) {
return b == 0 ? a : gcd(b, a % b);
}
bool isprime(long long int n) {
if (n == 2 || n == 3) return true;
if (n < 2 || n % 2 == 0 || n % 3 == 0) return false;
for (long long int i = 6; (i - 1) * (i - 1) <= n; i += 6) {
if (n % (i - 1) == 0 || n % (i + 1) == 0) {
return false;
}
}
return true;
}
vector<bool> sprime;
void genPrime(long long int sz) {
sprime = vector<bool>(sz, true);
sprime[0] = false;
sprime[1] = false;
for (long long int i = (2); i < (sz); ++i) {
if (sprime[i]) {
for (long long int j = i * i; j < sz; j += i) {
sprime[j] = false;
}
}
}
}
long long int powMod(long long int a, long long int b, long long int mod) {
long long int n = 1;
long long int p = a;
while (b > 0) {
if (b % 2 == 1) {
n *= p;
n %= mod;
}
p *= p;
p %= mod;
b /= 2;
}
return n;
}
long long int modularInverse(long long int a, long long int mod) {
return powMod(a, mod - 2, mod);
}
long long int binarysearch(long long int l, long long int r,
bool (*bsfunction)(long long int)) {
while (r - l > 1) {
long long int mid = (l + r) / 2;
bool val = bsfunction(mid);
if (val) {
r = mid;
} else {
l = mid;
}
}
return l;
}
stringstream sss;
const long long int maxn = 100010;
vector<long long int> g[maxn];
long long int col[maxn];
long long int dp[maxn][2];
void dfs(long long int x) {
long long int adjn = g[x].size();
for (auto y : g[x]) {
dfs(y);
}
vector<long long int> lt(adjn + 1), rt(adjn + 1);
lt[0] = 1;
rt[adjn] = 1;
for (long long int i = 0; i < (adjn); ++i) {
long long int y = g[x][i];
lt[i + 1] = lt[i] * (dp[y][0] + dp[y][1]) % MOD;
}
for (long long int i = (adjn)-1; i >= 0; --i) {
long long int y = g[x][i];
rt[i] = rt[i + 1] * (dp[y][0] + dp[y][1]) % MOD;
}
if (col[x]) {
dp[x][0] = 0;
dp[x][1] = lt[adjn];
} else {
dp[x][0] = lt[adjn];
for (long long int i = 0; i < (adjn); ++i) {
long long int y = g[x][i];
dp[x][1] += dp[y][1] * lt[i] % MOD * rt[i + 1] % MOD;
dp[x][1] %= MOD;
}
}
}
void MAIN() {
long long int n;
cin >> n;
for (long long int i = 0; i < (n - 1); ++i) {
long long int x;
cin >> x;
g[x].push_back(i + 1);
}
for (long long int i = 0; i < (n); ++i) {
cin >> col[i];
}
dfs(0);
cout << (dp[0][1]) << endl;
}
long long int TESTCASEN = 1;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(10);
sss << R"(
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
)";
for (long long int test = 0; test < (TESTCASEN); ++test) {
MAIN();
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 1;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const int mod = 1e9 + 7;
int a[maxn];
long long dp[maxn][2];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
scanf("%d", &a[i]);
}
for (int i = 0; i < n; i++) {
int v;
scanf("%d", &v);
dp[i][v] = 1;
}
for (int i = n - 1; i > 0; i--) {
dp[a[i]][1] =
(dp[a[i]][1] * (dp[i][0] + dp[i][1]) + dp[a[i]][0] * dp[i][1]) % mod;
dp[a[i]][0] = (dp[a[i]][0] * (dp[i][0] + dp[i][1])) % mod;
}
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 maxn = 1e5 + 1;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const int mod = 1e9 + 7;
int a[maxn];
long long dp[maxn][2];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
scanf("%d", &a[i]);
}
for (int i = 0; i < n; i++) {
int v;
scanf("%d", &v);
dp[i][v] = 1;
}
for (int i = n - 1; i > 0; i--) {
dp[a[i]][1] =
(dp[a[i]][1] * (dp[i][0] + dp[i][1]) + dp[a[i]][0] * dp[i][1]) % mod;
dp[a[i]][0] = (dp[a[i]][0] * (dp[i][0] + dp[i][1])) % mod;
}
printf("%I64d\n", dp[0][1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<long long int> adj[100005];
long long int n;
long long int bw[100005];
long long int dp[100005][2];
void dfs(long long int x) {
dp[x][1] = 0;
dp[x][0] = 1;
for (auto it : adj[x]) {
dfs(it);
dp[x][1] = (dp[it][0] % 1000000007 * dp[x][1] % 1000000007) % 1000000007;
dp[x][1] = (dp[x][1] % 1000000007 +
(dp[x][0] % 1000000007 * dp[it][1] % 1000000007) % 1000000007) %
1000000007;
dp[x][0] = (dp[it][0] % 1000000007 * dp[x][0] % 1000000007) % 1000000007;
}
if (bw[x])
dp[x][1] = dp[x][0];
else
dp[x][0] = (dp[x][0] % 1000000007 + dp[x][1] % 1000000007) % 1000000007;
return;
}
int main() {
cin >> n;
memset((dp), (0), sizeof((dp)));
for (long long int i = 1; i < n; i++) {
long long int x;
cin >> x;
adj[x].push_back(i);
}
for (long long int i = 0; i < n; i++) cin >> bw[i];
dfs(0);
cout << dp[0][1];
}
|
### Prompt
Your challenge is to write a cpp solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<long long int> adj[100005];
long long int n;
long long int bw[100005];
long long int dp[100005][2];
void dfs(long long int x) {
dp[x][1] = 0;
dp[x][0] = 1;
for (auto it : adj[x]) {
dfs(it);
dp[x][1] = (dp[it][0] % 1000000007 * dp[x][1] % 1000000007) % 1000000007;
dp[x][1] = (dp[x][1] % 1000000007 +
(dp[x][0] % 1000000007 * dp[it][1] % 1000000007) % 1000000007) %
1000000007;
dp[x][0] = (dp[it][0] % 1000000007 * dp[x][0] % 1000000007) % 1000000007;
}
if (bw[x])
dp[x][1] = dp[x][0];
else
dp[x][0] = (dp[x][0] % 1000000007 + dp[x][1] % 1000000007) % 1000000007;
return;
}
int main() {
cin >> n;
memset((dp), (0), sizeof((dp)));
for (long long int i = 1; i < n; i++) {
long long int x;
cin >> x;
adj[x].push_back(i);
}
for (long long int i = 0; i < n; i++) cin >> bw[i];
dfs(0);
cout << dp[0][1];
}
```
|
#include <bits/stdc++.h>
using namespace std;
int x[100000];
vector<int> adj[100000];
void add(int &a, int b) { a = (a + b) % 1000000007; }
int mul(int a, int b) { return (long long)a * b % 1000000007; }
pair<int, int> dfs(int u) {
pair<int, int> ret = {1, 0};
for (int v : adj[u]) {
pair<int, int> p = dfs(v);
ret.second = mul(ret.second, p.first);
add(ret.second, mul(ret.first, p.second));
ret.first = mul(ret.first, p.first);
}
if (x[u])
ret.second = ret.first;
else
add(ret.first, ret.second);
return ret;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int p;
cin >> p;
adj[p].push_back(i);
}
for (int i = 0; i < n; i++) cin >> x[i];
cout << dfs(0).second << "\n";
return 0;
}
|
### Prompt
Please provide a Cpp coded solution to the problem described below:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int x[100000];
vector<int> adj[100000];
void add(int &a, int b) { a = (a + b) % 1000000007; }
int mul(int a, int b) { return (long long)a * b % 1000000007; }
pair<int, int> dfs(int u) {
pair<int, int> ret = {1, 0};
for (int v : adj[u]) {
pair<int, int> p = dfs(v);
ret.second = mul(ret.second, p.first);
add(ret.second, mul(ret.first, p.second));
ret.first = mul(ret.first, p.first);
}
if (x[u])
ret.second = ret.first;
else
add(ret.first, ret.second);
return ret;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int p;
cin >> p;
adj[p].push_back(i);
}
for (int i = 0; i < n; i++) cin >> x[i];
cout << dfs(0).second << "\n";
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long int dp[2][200000];
int p[200000];
vector<int> v[200000];
int col[200000];
long long int modexp(long long int a, long long int b) {
long long int ret = 1;
while (b) {
if ((b & 1)) {
ret *= a;
ret %= 1000000007;
}
a *= a;
a %= 1000000007;
b >>= 1;
}
return ret;
}
long long int getans(int use_black, int cnode, int par = -1) {
long long int ret = dp[use_black][cnode];
if (ret == -1) {
int n = v[cnode].size(), i;
long long int &ret1 = dp[0][cnode];
long long int &ret2 = dp[1][cnode];
ret1 = 1;
ret2 = 0;
for (i = 0; i < n; i++) {
int child = v[cnode][i];
if (child == par) continue;
ret1 *= (getans(1, child, cnode) + getans(0, child, cnode)) % 1000000007;
ret1 %= 1000000007;
}
if (col[cnode] == 0) {
for (i = 0; i < n; i++) {
int child = v[cnode][i];
if (child == par) continue;
long long int mul =
(getans(1, child, cnode) *
modexp(getans(0, child, cnode) + getans(1, child, cnode),
1000000007 - 2)) %
1000000007;
ret2 += (mul * ret1) % 1000000007;
ret2 %= 1000000007;
}
} else {
ret2 = ret1;
ret1 = 0;
}
}
return dp[use_black][cnode];
}
int main() {
memset(dp, -1, sizeof(dp));
int i, n, x;
cin >> n;
for (i = 0; i < n - 1; i++) {
cin >> x;
v[i + 1].push_back(x);
v[x].push_back(i + 1);
}
for (i = 0; i < n; i++) cin >> col[i];
cout << getans(1, 0) << 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;
long long int dp[2][200000];
int p[200000];
vector<int> v[200000];
int col[200000];
long long int modexp(long long int a, long long int b) {
long long int ret = 1;
while (b) {
if ((b & 1)) {
ret *= a;
ret %= 1000000007;
}
a *= a;
a %= 1000000007;
b >>= 1;
}
return ret;
}
long long int getans(int use_black, int cnode, int par = -1) {
long long int ret = dp[use_black][cnode];
if (ret == -1) {
int n = v[cnode].size(), i;
long long int &ret1 = dp[0][cnode];
long long int &ret2 = dp[1][cnode];
ret1 = 1;
ret2 = 0;
for (i = 0; i < n; i++) {
int child = v[cnode][i];
if (child == par) continue;
ret1 *= (getans(1, child, cnode) + getans(0, child, cnode)) % 1000000007;
ret1 %= 1000000007;
}
if (col[cnode] == 0) {
for (i = 0; i < n; i++) {
int child = v[cnode][i];
if (child == par) continue;
long long int mul =
(getans(1, child, cnode) *
modexp(getans(0, child, cnode) + getans(1, child, cnode),
1000000007 - 2)) %
1000000007;
ret2 += (mul * ret1) % 1000000007;
ret2 %= 1000000007;
}
} else {
ret2 = ret1;
ret1 = 0;
}
}
return dp[use_black][cnode];
}
int main() {
memset(dp, -1, sizeof(dp));
int i, n, x;
cin >> n;
for (i = 0; i < n - 1; i++) {
cin >> x;
v[i + 1].push_back(x);
v[x].push_back(i + 1);
}
for (i = 0; i < n; i++) cin >> col[i];
cout << getans(1, 0) << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int c[100009];
vector<int> v[100009];
long long dp[100009][2];
void dfs(int x) {
dp[x][c[x]] = 1;
for (int i = 0; i < v[x].size(); i++) {
int y = v[x][i];
dfs(y);
dp[x][1] = ((dp[x][0] * dp[y][1]) % 1000000007 +
(dp[x][1] * dp[y][1]) % 1000000007 +
(dp[x][1] * dp[y][0]) % 1000000007) %
1000000007;
dp[x][0] = ((dp[x][0] * dp[y][0]) % 1000000007 +
(dp[x][0] * dp[y][1]) % 1000000007) %
1000000007;
}
}
int main() {
int n, x;
cin >> n;
for (int i = 2; i <= n; i++) {
cin >> x;
x++;
v[x].push_back(i);
}
for (int i = 1; i <= n; i++) scanf("%d", &c[i]);
dfs(1);
cout << dp[1][1] << endl;
}
|
### Prompt
In CPP, your task is to solve the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int c[100009];
vector<int> v[100009];
long long dp[100009][2];
void dfs(int x) {
dp[x][c[x]] = 1;
for (int i = 0; i < v[x].size(); i++) {
int y = v[x][i];
dfs(y);
dp[x][1] = ((dp[x][0] * dp[y][1]) % 1000000007 +
(dp[x][1] * dp[y][1]) % 1000000007 +
(dp[x][1] * dp[y][0]) % 1000000007) %
1000000007;
dp[x][0] = ((dp[x][0] * dp[y][0]) % 1000000007 +
(dp[x][0] * dp[y][1]) % 1000000007) %
1000000007;
}
}
int main() {
int n, x;
cin >> n;
for (int i = 2; i <= n; i++) {
cin >> x;
x++;
v[x].push_back(i);
}
for (int i = 1; i <= n; i++) scanf("%d", &c[i]);
dfs(1);
cout << dp[1][1] << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int oo = 2000000009;
const double eps = 1e-9;
list<int> adj[100005];
int label[100005];
long long dp[100005][2];
const long long MOD = 1000000007;
void numWays(int v, int p) {
dp[v][0] = 1 - label[v];
dp[v][1] = label[v];
for (typeof(adj[v].begin()) u = adj[v].begin(); u != adj[v].end(); u++)
if (*u != p) {
long long cum[2];
numWays(*u, v);
cum[0] = dp[v][0];
cum[1] = dp[v][1];
dp[v][0] = dp[v][1] = 0;
dp[v][1] += dp[*u][0] * cum[1];
dp[v][1] %= MOD;
dp[v][1] += dp[*u][1] * cum[0];
dp[v][1] %= MOD;
dp[v][0] += dp[*u][0] * cum[0];
dp[v][0] %= MOD;
dp[v][1] += dp[*u][1] * cum[1];
dp[v][1] %= MOD;
dp[v][0] += dp[*u][1] * cum[0];
dp[v][0] %= MOD;
}
}
int main() {
int n, temp;
scanf("%d", &n);
for (int i = 0; i < (n - 1); ++i) {
scanf("%d", &temp);
adj[i + 1].push_back(temp);
adj[temp].push_back(i + 1);
}
for (int i = 0; i < (n); ++i) {
scanf("%d", &label[i]);
}
numWays(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;
const int oo = 2000000009;
const double eps = 1e-9;
list<int> adj[100005];
int label[100005];
long long dp[100005][2];
const long long MOD = 1000000007;
void numWays(int v, int p) {
dp[v][0] = 1 - label[v];
dp[v][1] = label[v];
for (typeof(adj[v].begin()) u = adj[v].begin(); u != adj[v].end(); u++)
if (*u != p) {
long long cum[2];
numWays(*u, v);
cum[0] = dp[v][0];
cum[1] = dp[v][1];
dp[v][0] = dp[v][1] = 0;
dp[v][1] += dp[*u][0] * cum[1];
dp[v][1] %= MOD;
dp[v][1] += dp[*u][1] * cum[0];
dp[v][1] %= MOD;
dp[v][0] += dp[*u][0] * cum[0];
dp[v][0] %= MOD;
dp[v][1] += dp[*u][1] * cum[1];
dp[v][1] %= MOD;
dp[v][0] += dp[*u][1] * cum[0];
dp[v][0] %= MOD;
}
}
int main() {
int n, temp;
scanf("%d", &n);
for (int i = 0; i < (n - 1); ++i) {
scanf("%d", &temp);
adj[i + 1].push_back(temp);
adj[temp].push_back(i + 1);
}
for (int i = 0; i < (n); ++i) {
scanf("%d", &label[i]);
}
numWays(0, -1);
cout << dp[0][1] << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const int MAX = 100005;
int B[MAX], N;
long long DP[MAX][2];
vector<int> E[MAX];
void dfs(int n, int p) {
DP[n][1] = B[n];
DP[n][0] = 1 - B[n];
for (int v : E[n])
if (v != p) {
long long old[2];
old[0] = DP[n][0];
old[1] = DP[n][1];
DP[n][0] = DP[n][1] = 0;
dfs(v, n);
DP[n][0] += DP[v][0] * old[0];
DP[n][0] %= MOD;
DP[n][1] += DP[v][1] * old[0];
DP[n][1] %= MOD;
DP[n][1] += DP[v][0] * old[1];
DP[n][1] %= MOD;
DP[n][0] += DP[v][1] * old[0];
DP[n][0] %= MOD;
DP[n][1] += DP[v][1] * old[1];
DP[n][1] %= MOD;
}
}
int main() {
cin >> N;
for (int i = (2); i <= (N); i++) {
int p;
cin >> p;
E[p + 1].push_back(i), E[i].push_back(p + 1);
}
for (int i = (1); i <= (N); i++) cin >> B[i];
dfs(1, 0);
cout << DP[1][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>
using namespace std;
const long long MOD = 1000000007;
const int MAX = 100005;
int B[MAX], N;
long long DP[MAX][2];
vector<int> E[MAX];
void dfs(int n, int p) {
DP[n][1] = B[n];
DP[n][0] = 1 - B[n];
for (int v : E[n])
if (v != p) {
long long old[2];
old[0] = DP[n][0];
old[1] = DP[n][1];
DP[n][0] = DP[n][1] = 0;
dfs(v, n);
DP[n][0] += DP[v][0] * old[0];
DP[n][0] %= MOD;
DP[n][1] += DP[v][1] * old[0];
DP[n][1] %= MOD;
DP[n][1] += DP[v][0] * old[1];
DP[n][1] %= MOD;
DP[n][0] += DP[v][1] * old[0];
DP[n][0] %= MOD;
DP[n][1] += DP[v][1] * old[1];
DP[n][1] %= MOD;
}
}
int main() {
cin >> N;
for (int i = (2); i <= (N); i++) {
int p;
cin >> p;
E[p + 1].push_back(i), E[i].push_back(p + 1);
}
for (int i = (1); i <= (N); i++) cin >> B[i];
dfs(1, 0);
cout << DP[1][1];
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int M = 100005;
long long dp[M][2];
vector<int> G[M];
int color[M];
void dfs(int x) {
long long ones, zeros;
ones = color[x];
zeros = 1 - color[x];
int u;
for (int i = 0; i < G[x].size(); i++) {
u = G[x][i];
dfs(u);
ones = (ones * dp[u][1] + ones * dp[u][0] + zeros * dp[u][1]) % 1000000007;
zeros = (zeros * dp[u][1] + zeros * dp[u][0]) % 1000000007;
}
dp[x][1] = ones;
dp[x][0] = zeros;
}
int main() {
int n;
scanf("%d", &n);
int x;
for (int i = 0; i < n - 1; i++) {
scanf("%d", &x);
G[x].push_back(i + 1);
}
for (int i = 0; i < n; i++) {
scanf("%d", &color[i]);
}
dfs(0);
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 M = 100005;
long long dp[M][2];
vector<int> G[M];
int color[M];
void dfs(int x) {
long long ones, zeros;
ones = color[x];
zeros = 1 - color[x];
int u;
for (int i = 0; i < G[x].size(); i++) {
u = G[x][i];
dfs(u);
ones = (ones * dp[u][1] + ones * dp[u][0] + zeros * dp[u][1]) % 1000000007;
zeros = (zeros * dp[u][1] + zeros * dp[u][0]) % 1000000007;
}
dp[x][1] = ones;
dp[x][0] = zeros;
}
int main() {
int n;
scanf("%d", &n);
int x;
for (int i = 0; i < n - 1; i++) {
scanf("%d", &x);
G[x].push_back(i + 1);
}
for (int i = 0; i < n; i++) {
scanf("%d", &color[i]);
}
dfs(0);
printf("%d\n", dp[0][1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
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 = 0; i < 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]) % 1000000007 +
(dp[v][0] * dp[u][1]) % 1000000007) %
1000000007;
dp[v][0] = (dp[v][0] * dp[u][0]) % 1000000007;
}
if (c[v])
dp[v][1] = dp[v][0];
else
dp[v][0] = (dp[v][0] + dp[v][1]) % 1000000007;
}
int main() {
int tmp;
int n;
scanf("%d", &n);
for (int i = 1; i < n; ++i) {
scanf("%d", &tmp);
x[i].push_back(tmp);
x[tmp].push_back(i);
}
for (int i = 0; i < n; ++i) {
scanf("%d", &c[i]);
}
dfs(0, -1);
printf("%I64d", dp[0][1]);
return 0;
}
|
### Prompt
Develop a solution in CPP to the problem described below:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
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 = 0; i < 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]) % 1000000007 +
(dp[v][0] * dp[u][1]) % 1000000007) %
1000000007;
dp[v][0] = (dp[v][0] * dp[u][0]) % 1000000007;
}
if (c[v])
dp[v][1] = dp[v][0];
else
dp[v][0] = (dp[v][0] + dp[v][1]) % 1000000007;
}
int main() {
int tmp;
int n;
scanf("%d", &n);
for (int i = 1; i < n; ++i) {
scanf("%d", &tmp);
x[i].push_back(tmp);
x[tmp].push_back(i);
}
for (int i = 0; i < n; ++i) {
scanf("%d", &c[i]);
}
dfs(0, -1);
printf("%I64d", dp[0][1]);
return 0;
}
```
|
#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]]) {
if (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;
ret = (ret + go(n, p + 1, 1) * go(adj[n][p], 0, 1)) % 1000000007;
} else {
ret = (ret + go(n, p + 1, 0) * go(adj[n][p], 0, 0)) % 1000000007;
ret = (ret + go(n, p + 1, 0) * go(adj[n][p], 0, 1)) % 1000000007;
}
} else {
if (b) {
ret = (ret + go(n, p + 1, 0) * go(adj[n][p], 0, 0)) % 1000000007;
ret = (ret + go(n, p + 1, 1) * go(adj[n][p], 0, 0)) % 1000000007;
} else {
ret = (ret + go(n, p + 1, 0) * go(adj[n][p], 0, 0)) % 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
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 <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]]) {
if (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;
ret = (ret + go(n, p + 1, 1) * go(adj[n][p], 0, 1)) % 1000000007;
} else {
ret = (ret + go(n, p + 1, 0) * go(adj[n][p], 0, 0)) % 1000000007;
ret = (ret + go(n, p + 1, 0) * go(adj[n][p], 0, 1)) % 1000000007;
}
} else {
if (b) {
ret = (ret + go(n, p + 1, 0) * go(adj[n][p], 0, 0)) % 1000000007;
ret = (ret + go(n, p + 1, 1) * go(adj[n][p], 0, 0)) % 1000000007;
} else {
ret = (ret + go(n, p + 1, 0) * go(adj[n][p], 0, 0)) % 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;
const long long N = 3e5;
const long long P = 1e9 + 7;
long long n;
long long col[N], f[N], g[N];
vector<long long> a[N];
long long read(void) {
long long s = 0, w = 0;
char c = getchar();
while (!isdigit(c)) w |= c == '-', c = getchar();
while (isdigit(c)) s = s * 10 + c - 48, c = getchar();
return w ? -s : s;
}
void Dfs(long long x, long long fa) {
col[x] ? f[x] = 1 : g[x] = 1;
for (long long i = 0; i < a[x].size(); ++i) {
long long y = a[x][i];
if (y == fa) continue;
Dfs(y, x);
f[x] = (f[x] * (f[y] + g[y]) + g[x] * f[y]) % P;
g[x] = (g[x] * (f[y] + g[y])) % P;
}
return;
}
signed main(void) {
n = read();
for (long long i = 1; i < n; ++i) a[read()].push_back(i);
for (long long i = 0; i < n; ++i) col[i] = read();
Dfs(0, 0);
cout << f[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 N = 3e5;
const long long P = 1e9 + 7;
long long n;
long long col[N], f[N], g[N];
vector<long long> a[N];
long long read(void) {
long long s = 0, w = 0;
char c = getchar();
while (!isdigit(c)) w |= c == '-', c = getchar();
while (isdigit(c)) s = s * 10 + c - 48, c = getchar();
return w ? -s : s;
}
void Dfs(long long x, long long fa) {
col[x] ? f[x] = 1 : g[x] = 1;
for (long long i = 0; i < a[x].size(); ++i) {
long long y = a[x][i];
if (y == fa) continue;
Dfs(y, x);
f[x] = (f[x] * (f[y] + g[y]) + g[x] * f[y]) % P;
g[x] = (g[x] * (f[y] + g[y])) % P;
}
return;
}
signed main(void) {
n = read();
for (long long i = 1; i < n; ++i) a[read()].push_back(i);
for (long long i = 0; i < n; ++i) col[i] = read();
Dfs(0, 0);
cout << f[0] << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
string alpha = "abcdefghijklmnopqrstuvwxyz";
const long long mod = 1e9 + 7;
const long long inf = 2e18 + 5;
const long long nax = 100010;
vector<vector<long long>> g;
bool x[nax];
long long dp[nax][2];
void dfs(long long u) {
dp[u][0] = 1;
dp[u][1] = 0;
for (auto v : g[u]) {
dfs(v);
dp[u][1] *= dp[v][0];
dp[u][1] %= mod;
dp[u][1] += dp[v][1] * dp[u][0];
dp[u][1] %= mod;
dp[u][0] *= dp[v][0];
dp[u][0] %= mod;
}
if (x[u]) {
dp[u][1] = dp[u][0];
dp[u][0] = 0;
}
dp[u][0] += dp[u][1];
dp[u][0] %= mod;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
g = vector<vector<long long>>(n);
for (long long i = (1); i < (n); i++) {
long long p;
cin >> p;
g[p].push_back(i);
}
for (long long i = (0); i < (n); i++) cin >> x[i];
dfs(0);
cout << 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;
string alpha = "abcdefghijklmnopqrstuvwxyz";
const long long mod = 1e9 + 7;
const long long inf = 2e18 + 5;
const long long nax = 100010;
vector<vector<long long>> g;
bool x[nax];
long long dp[nax][2];
void dfs(long long u) {
dp[u][0] = 1;
dp[u][1] = 0;
for (auto v : g[u]) {
dfs(v);
dp[u][1] *= dp[v][0];
dp[u][1] %= mod;
dp[u][1] += dp[v][1] * dp[u][0];
dp[u][1] %= mod;
dp[u][0] *= dp[v][0];
dp[u][0] %= mod;
}
if (x[u]) {
dp[u][1] = dp[u][0];
dp[u][0] = 0;
}
dp[u][0] += dp[u][1];
dp[u][0] %= mod;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
g = vector<vector<long long>>(n);
for (long long i = (1); i < (n); i++) {
long long p;
cin >> p;
g[p].push_back(i);
}
for (long long i = (0); i < (n); i++) cin >> x[i];
dfs(0);
cout << dp[0][1];
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long pow(long long a, long long b) {
if (b == 0) return 1;
long long res = pow(a, b / 2);
res *= res;
res %= 1000000007;
if (b % 2 == 1) res *= a;
return res % 1000000007;
}
long long rev(long long x) { return pow(x, 1000000007 - 2); }
long long color[108000], p[108000];
long long dp[108000][2];
vector<int> edge[108000];
long long dfs(int x) {
for (int i = 0; i < edge[x].size(); i++) {
int to = edge[x][i];
dfs(to);
}
if (color[x] == 1) {
dp[x][0] = 0;
dp[x][1] = 1;
for (int i = 0; i < edge[x].size(); i++) {
int to = edge[x][i];
dp[x][1] *= dp[to][0] + dp[to][1];
dp[x][1] %= 1000000007;
}
} else {
dp[x][0] = 1;
dp[x][1] = 0;
for (int i = 0; i < edge[x].size(); i++) {
int to = edge[x][i];
dp[x][0] *= dp[to][0] + dp[to][1];
dp[x][0] %= 1000000007;
}
for (int i = 0; i < edge[x].size(); i++) {
int to = edge[x][i];
dp[x][1] +=
(dp[x][0] * dp[to][1]) % 1000000007 * rev(dp[to][0] + dp[to][1]);
dp[x][1] %= 1000000007;
}
}
return dp[x][1];
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n - 1; i++) {
cin >> p[i];
edge[p[i]].push_back(i);
}
for (int i = 0; i < n; i++) cin >> color[i];
cout << dfs(0) << endl;
return 0;
}
|
### Prompt
In CPP, your task is to solve the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long pow(long long a, long long b) {
if (b == 0) return 1;
long long res = pow(a, b / 2);
res *= res;
res %= 1000000007;
if (b % 2 == 1) res *= a;
return res % 1000000007;
}
long long rev(long long x) { return pow(x, 1000000007 - 2); }
long long color[108000], p[108000];
long long dp[108000][2];
vector<int> edge[108000];
long long dfs(int x) {
for (int i = 0; i < edge[x].size(); i++) {
int to = edge[x][i];
dfs(to);
}
if (color[x] == 1) {
dp[x][0] = 0;
dp[x][1] = 1;
for (int i = 0; i < edge[x].size(); i++) {
int to = edge[x][i];
dp[x][1] *= dp[to][0] + dp[to][1];
dp[x][1] %= 1000000007;
}
} else {
dp[x][0] = 1;
dp[x][1] = 0;
for (int i = 0; i < edge[x].size(); i++) {
int to = edge[x][i];
dp[x][0] *= dp[to][0] + dp[to][1];
dp[x][0] %= 1000000007;
}
for (int i = 0; i < edge[x].size(); i++) {
int to = edge[x][i];
dp[x][1] +=
(dp[x][0] * dp[to][1]) % 1000000007 * rev(dp[to][0] + dp[to][1]);
dp[x][1] %= 1000000007;
}
}
return dp[x][1];
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n - 1; i++) {
cin >> p[i];
edge[p[i]].push_back(i);
}
for (int i = 0; i < n; i++) cin >> color[i];
cout << dfs(0) << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long MX = 2e5;
vector<long long> adj[MX];
const long long MOD = 1e9 + 7;
long long mult(long long x, long long y) { return (x * y) % MOD; }
long long add(long long x, long long y) { return (x + y) % MOD; }
long long fastPow(long long a, long long b) {
long long ans = 1;
while (b > 0) {
if (b & 1 == 1) {
ans = mult(ans, a);
}
a = mult(a, a);
b >>= 1;
}
return ans;
}
long long modInverse(long long x) { return fastPow(x, MOD - 2); }
long long dp[MX][2];
bool used[MX][2];
bool isBlack[MX];
long long cnt[MX];
long long count(long long u, bool black) {
if (used[u][black]) return dp[u][black];
if (isBlack[u] && black) return 0;
used[u][black] = true;
long long ans = 1;
if (isBlack[u] || black) {
for (long long v : adj[u]) {
ans = mult(ans, add(count(v, true), count(v, false)));
}
} else {
if (cnt[u] == 0) {
ans = 0;
} else {
long long prod = 1;
for (long long v : adj[u]) {
long long x = add(count(v, false), count(v, true));
prod = mult(prod, x);
}
ans = 0;
for (long long i = 0; i < adj[u].size(); i++) {
long long v = adj[u][i];
long long partial = mult(prod, count(v, false));
long long x = add(count(v, false), count(v, true));
partial = mult(partial, modInverse(x));
ans = add(ans, partial);
}
}
}
return dp[u][black] = ans;
}
long long dfs(long long u) {
if (isBlack[u]) cnt[u]++;
for (long long v : adj[u]) {
cnt[u] += dfs(v);
}
return cnt[u];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n;
cin >> n;
for (long long i = 1; i < n; i++) {
long long p;
cin >> p;
adj[p].push_back(i);
}
for (long long i = 0; i < n; i++) {
long long x;
cin >> x;
isBlack[i] = x;
}
dfs(0);
cout << count(0, false) << "\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 MX = 2e5;
vector<long long> adj[MX];
const long long MOD = 1e9 + 7;
long long mult(long long x, long long y) { return (x * y) % MOD; }
long long add(long long x, long long y) { return (x + y) % MOD; }
long long fastPow(long long a, long long b) {
long long ans = 1;
while (b > 0) {
if (b & 1 == 1) {
ans = mult(ans, a);
}
a = mult(a, a);
b >>= 1;
}
return ans;
}
long long modInverse(long long x) { return fastPow(x, MOD - 2); }
long long dp[MX][2];
bool used[MX][2];
bool isBlack[MX];
long long cnt[MX];
long long count(long long u, bool black) {
if (used[u][black]) return dp[u][black];
if (isBlack[u] && black) return 0;
used[u][black] = true;
long long ans = 1;
if (isBlack[u] || black) {
for (long long v : adj[u]) {
ans = mult(ans, add(count(v, true), count(v, false)));
}
} else {
if (cnt[u] == 0) {
ans = 0;
} else {
long long prod = 1;
for (long long v : adj[u]) {
long long x = add(count(v, false), count(v, true));
prod = mult(prod, x);
}
ans = 0;
for (long long i = 0; i < adj[u].size(); i++) {
long long v = adj[u][i];
long long partial = mult(prod, count(v, false));
long long x = add(count(v, false), count(v, true));
partial = mult(partial, modInverse(x));
ans = add(ans, partial);
}
}
}
return dp[u][black] = ans;
}
long long dfs(long long u) {
if (isBlack[u]) cnt[u]++;
for (long long v : adj[u]) {
cnt[u] += dfs(v);
}
return cnt[u];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n;
cin >> n;
for (long long i = 1; i < n; i++) {
long long p;
cin >> p;
adj[p].push_back(i);
}
for (long long i = 0; i < n; i++) {
long long x;
cin >> x;
isBlack[i] = x;
}
dfs(0);
cout << count(0, false) << "\n";
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int sz = 200010;
vector<int> g[sz];
long long int a[sz], dp[sz][2];
void dfs(int u, int p) {
dp[u][0] = 1;
dp[u][1] = 0;
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (v != p) {
dfs(v, u);
dp[u][1] = (dp[u][1] * dp[v][0] % 1000000007 +
dp[u][0] * dp[v][1] % 1000000007) %
1000000007;
dp[u][0] = dp[u][0] * dp[v][0] % 1000000007;
}
}
if (a[u] == 1) {
dp[u][1] = dp[u][0];
} else {
dp[u][0] += dp[u][1];
}
}
int main() {
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int u;
cin >> u;
g[i].push_back(u);
g[u].push_back(i);
}
for (int i = 0; i < n; i++) {
cin >> a[i];
}
dfs(1, -1);
cout << dp[1][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 int sz = 200010;
vector<int> g[sz];
long long int a[sz], dp[sz][2];
void dfs(int u, int p) {
dp[u][0] = 1;
dp[u][1] = 0;
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (v != p) {
dfs(v, u);
dp[u][1] = (dp[u][1] * dp[v][0] % 1000000007 +
dp[u][0] * dp[v][1] % 1000000007) %
1000000007;
dp[u][0] = dp[u][0] * dp[v][0] % 1000000007;
}
}
if (a[u] == 1) {
dp[u][1] = dp[u][0];
} else {
dp[u][0] += dp[u][1];
}
}
int main() {
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int u;
cin >> u;
g[i].push_back(u);
g[u].push_back(i);
}
for (int i = 0; i < n; i++) {
cin >> a[i];
}
dfs(1, -1);
cout << dp[1][1] << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> graph;
vector<int> isBlack;
vector<long long> dp[2];
long long previous[2];
void dfs(int curr) {
dp[0][curr] = 1 - isBlack[curr];
dp[1][curr] = isBlack[curr];
for (int child : graph[curr]) {
dfs(child);
previous[0] = dp[0][curr];
previous[1] = dp[1][curr];
dp[0][curr] = 0;
dp[1][curr] = 0;
dp[1][curr] =
(previous[1] * (dp[0][child] + dp[1][child]) % 1000000007) % 1000000007;
if (isBlack[curr] == 0) {
dp[0][curr] = (previous[0] * (dp[0][child] + dp[1][child]) % 1000000007) %
1000000007;
dp[1][curr] = (dp[1][curr] + (previous[0] * dp[1][child]) % 1000000007) %
1000000007;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
graph.resize(n);
isBlack.resize(n);
dp[1].resize(n);
dp[0].resize(n);
for (int i = 0; i < n - 1; i++) {
int p;
cin >> p;
graph[p].push_back(i + 1);
}
for (int i = 0; i < n; i++) {
cin >> isBlack[i];
}
dfs(0);
cout << dp[1][0] << '\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;
vector<vector<int>> graph;
vector<int> isBlack;
vector<long long> dp[2];
long long previous[2];
void dfs(int curr) {
dp[0][curr] = 1 - isBlack[curr];
dp[1][curr] = isBlack[curr];
for (int child : graph[curr]) {
dfs(child);
previous[0] = dp[0][curr];
previous[1] = dp[1][curr];
dp[0][curr] = 0;
dp[1][curr] = 0;
dp[1][curr] =
(previous[1] * (dp[0][child] + dp[1][child]) % 1000000007) % 1000000007;
if (isBlack[curr] == 0) {
dp[0][curr] = (previous[0] * (dp[0][child] + dp[1][child]) % 1000000007) %
1000000007;
dp[1][curr] = (dp[1][curr] + (previous[0] * dp[1][child]) % 1000000007) %
1000000007;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
graph.resize(n);
isBlack.resize(n);
dp[1].resize(n);
dp[0].resize(n);
for (int i = 0; i < n - 1; i++) {
int p;
cin >> p;
graph[p].push_back(i + 1);
}
for (int i = 0; i < n; i++) {
cin >> isBlack[i];
}
dfs(0);
cout << dp[1][0] << '\n';
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int mod;
int n;
map<int, vector<int> > ch;
int col[100001];
bool seen[100001];
int dp[100001][2];
void dfs(int r) {
if (seen[r]) return;
seen[r] = true;
dp[r][0] = 1;
dp[r][1] = 0;
for (int i = 0; i < ch[r].size(); ++i) {
int u = ch[r][i];
if (seen[u]) continue;
dfs(u);
dp[r][1] = ((dp[r][1] * 1ll) * dp[ch[r][i]][0]) % mod;
dp[r][1] =
(dp[r][1] * 1ll + ((1ll * dp[ch[r][i]][1]) * dp[r][0]) % mod) % mod;
dp[r][0] = ((dp[r][0] * 1ll) * dp[ch[r][i]][0]) % mod;
}
if (col[r] == 1) {
dp[r][1] = dp[r][0];
} else {
dp[r][0] = (dp[r][0] * 1ll + dp[r][1]) % mod;
}
}
int main() {
mod = 1000000007;
memset(dp, 0, sizeof dp);
memset(seen, false, sizeof seen);
scanf("%d\n", &n);
int tmp;
for (int i = 0; i < n - 1; ++i) {
cin >> tmp;
ch[tmp].push_back(i + 1);
ch[i + 1].push_back(tmp);
}
cin.get();
for (int i = 0; i < n; ++i) {
cin >> col[i];
}
dfs(0);
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;
int mod;
int n;
map<int, vector<int> > ch;
int col[100001];
bool seen[100001];
int dp[100001][2];
void dfs(int r) {
if (seen[r]) return;
seen[r] = true;
dp[r][0] = 1;
dp[r][1] = 0;
for (int i = 0; i < ch[r].size(); ++i) {
int u = ch[r][i];
if (seen[u]) continue;
dfs(u);
dp[r][1] = ((dp[r][1] * 1ll) * dp[ch[r][i]][0]) % mod;
dp[r][1] =
(dp[r][1] * 1ll + ((1ll * dp[ch[r][i]][1]) * dp[r][0]) % mod) % mod;
dp[r][0] = ((dp[r][0] * 1ll) * dp[ch[r][i]][0]) % mod;
}
if (col[r] == 1) {
dp[r][1] = dp[r][0];
} else {
dp[r][0] = (dp[r][0] * 1ll + dp[r][1]) % mod;
}
}
int main() {
mod = 1000000007;
memset(dp, 0, sizeof dp);
memset(seen, false, sizeof seen);
scanf("%d\n", &n);
int tmp;
for (int i = 0; i < n - 1; ++i) {
cin >> tmp;
ch[tmp].push_back(i + 1);
ch[i + 1].push_back(tmp);
}
cin.get();
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 N = 100005;
vector<int> g[N];
const int mod = 1e9 + 7;
long long dp[N][2];
int color[N];
int n;
void dfs(int x) {
dp[x][0] = 1;
dp[x][1] = 0;
for (int i = 0; i < g[x].size(); i++) {
dfs(g[x][i]);
dp[x][1] = (dp[x][1] * dp[g[x][i]][0] + dp[x][0] * dp[g[x][i]][1]) % mod;
dp[x][0] = dp[x][0] * dp[g[x][i]][0] % mod;
}
if (color[x] == 1)
dp[x][1] = dp[x][0];
else
dp[x][0] = (dp[x][0] + dp[x][1]) % mod;
}
int main() {
scanf("%d", &n);
for (int i = 1, p; i < n; i++) {
scanf("%d", &p);
g[p].push_back(i);
}
for (int i = 0; i < n; i++) scanf("%d", color + i);
dfs(0);
printf("%I64d\n", dp[0][1]);
return 0;
}
|
### Prompt
Your 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 N = 100005;
vector<int> g[N];
const int mod = 1e9 + 7;
long long dp[N][2];
int color[N];
int n;
void dfs(int x) {
dp[x][0] = 1;
dp[x][1] = 0;
for (int i = 0; i < g[x].size(); i++) {
dfs(g[x][i]);
dp[x][1] = (dp[x][1] * dp[g[x][i]][0] + dp[x][0] * dp[g[x][i]][1]) % mod;
dp[x][0] = dp[x][0] * dp[g[x][i]][0] % mod;
}
if (color[x] == 1)
dp[x][1] = dp[x][0];
else
dp[x][0] = (dp[x][0] + dp[x][1]) % mod;
}
int main() {
scanf("%d", &n);
for (int i = 1, p; i < n; i++) {
scanf("%d", &p);
g[p].push_back(i);
}
for (int i = 0; i < n; i++) scanf("%d", color + i);
dfs(0);
printf("%I64d\n", dp[0][1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
bool c[100000];
vector<int> v[100000];
long long a[2][100000];
int const md = 1000000007;
void dfs(int n) {
if (c[n]) {
a[1][n] = 1;
a[0][n] = 0;
for (int i = 0; i < v[n].size(); ++i) {
int h = v[n][i];
dfs(h);
a[1][n] *= (a[0][h] + a[1][h]);
a[1][n] %= md;
}
} else {
a[1][n] = 0;
a[0][n] = 1;
for (int i = 0; i < v[n].size(); ++i) {
int h = v[n][i];
dfs(h);
a[1][n] *= a[0][h] + a[1][h];
a[1][n] %= md;
a[1][n] += a[1][h] * a[0][n];
a[1][n] %= md;
a[0][n] *= a[0][h] + a[1][h];
a[0][n] %= md;
}
}
}
int main() {
int n, t;
scanf("%d", &n);
for (int i = 1; i < n; ++i) {
scanf("%d", &t);
v[t].push_back(i);
}
for (int i = 0; i < n; ++i) scanf("%d", &c[i]);
dfs(0);
cout << a[1][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;
bool c[100000];
vector<int> v[100000];
long long a[2][100000];
int const md = 1000000007;
void dfs(int n) {
if (c[n]) {
a[1][n] = 1;
a[0][n] = 0;
for (int i = 0; i < v[n].size(); ++i) {
int h = v[n][i];
dfs(h);
a[1][n] *= (a[0][h] + a[1][h]);
a[1][n] %= md;
}
} else {
a[1][n] = 0;
a[0][n] = 1;
for (int i = 0; i < v[n].size(); ++i) {
int h = v[n][i];
dfs(h);
a[1][n] *= a[0][h] + a[1][h];
a[1][n] %= md;
a[1][n] += a[1][h] * a[0][n];
a[1][n] %= md;
a[0][n] *= a[0][h] + a[1][h];
a[0][n] %= md;
}
}
}
int main() {
int n, t;
scanf("%d", &n);
for (int i = 1; i < n; ++i) {
scanf("%d", &t);
v[t].push_back(i);
}
for (int i = 0; i < n; ++i) scanf("%d", &c[i]);
dfs(0);
cout << a[1][0];
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long getpow(long long a, long long b) {
long long ret = 1LL;
long long inter = a % 1000000007;
while (b) {
if (b & 1) {
ret *= inter;
ret %= 1000000007;
}
inter *= inter;
inter %= 1000000007;
b = b >> 1;
}
return ret;
}
int n, p;
vector<int> edges[1000000];
int brr[1000000];
int vis[1000000] = {0};
void in() {
cin >> n;
for (int i = 0; i < n - 1; i++) {
cin >> p;
edges[p].push_back(i + 1);
edges[i + 1].push_back(p);
}
for (int i = 0; i < n; i++) {
scanf("%d", &brr[i]);
}
}
pair<long long, long long> dfs(int u) {
vis[u] = 1;
vector<pair<long long, long long> > ret;
long long s = 1;
for (int i = 0; i < edges[u].size(); i++) {
int v = edges[u][i];
if (vis[v] == 0) {
ret.push_back(dfs(v));
}
}
if (ret.size() == 0) {
if (brr[u] == 1) {
return make_pair(1, 0);
} else {
return make_pair(0, 1);
}
}
for (int i = 0; i < ret.size(); i++) {
s *= (ret[i].first + ret[i].second);
s %= 1000000007;
}
if (brr[u] == 1) {
return make_pair(s, 0);
}
long long a = 0, b;
for (int i = 0; i < ret.size(); i++) {
b = getpow(ret[i].first + ret[i].second, 1000000007 - 2);
b *= s;
b %= 1000000007;
b *= ret[i].first;
b %= 1000000007;
a += b;
a %= 1000000007;
}
return make_pair(a, s);
}
void solve() { cout << (dfs(0).first) % 1000000007 << endl; }
int main() {
in();
solve();
}
|
### Prompt
In CPP, your task is to solve the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long getpow(long long a, long long b) {
long long ret = 1LL;
long long inter = a % 1000000007;
while (b) {
if (b & 1) {
ret *= inter;
ret %= 1000000007;
}
inter *= inter;
inter %= 1000000007;
b = b >> 1;
}
return ret;
}
int n, p;
vector<int> edges[1000000];
int brr[1000000];
int vis[1000000] = {0};
void in() {
cin >> n;
for (int i = 0; i < n - 1; i++) {
cin >> p;
edges[p].push_back(i + 1);
edges[i + 1].push_back(p);
}
for (int i = 0; i < n; i++) {
scanf("%d", &brr[i]);
}
}
pair<long long, long long> dfs(int u) {
vis[u] = 1;
vector<pair<long long, long long> > ret;
long long s = 1;
for (int i = 0; i < edges[u].size(); i++) {
int v = edges[u][i];
if (vis[v] == 0) {
ret.push_back(dfs(v));
}
}
if (ret.size() == 0) {
if (brr[u] == 1) {
return make_pair(1, 0);
} else {
return make_pair(0, 1);
}
}
for (int i = 0; i < ret.size(); i++) {
s *= (ret[i].first + ret[i].second);
s %= 1000000007;
}
if (brr[u] == 1) {
return make_pair(s, 0);
}
long long a = 0, b;
for (int i = 0; i < ret.size(); i++) {
b = getpow(ret[i].first + ret[i].second, 1000000007 - 2);
b *= s;
b %= 1000000007;
b *= ret[i].first;
b %= 1000000007;
a += b;
a %= 1000000007;
}
return make_pair(a, s);
}
void solve() { cout << (dfs(0).first) % 1000000007 << endl; }
int main() {
in();
solve();
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 100001;
int n, k;
vector<vector<int> > adjList(N);
vector<int> nxt(N, -1);
bool color[N];
int dp[N][2][2];
void dfs(int node) {
bool leaf = true;
for (int i = 0; i < adjList[node].size(); i++) {
leaf = false;
int ch = adjList[node][i];
int nextCh = i == adjList[node].size() - 1 ? -1 : adjList[node][i + 1];
nxt[ch] = nextCh;
dfs(ch);
}
if (leaf) adjList[node].push_back(-1);
}
long long solve(int node, bool root, bool black) {
if (node == -1) return black ? 0 : 1;
if (dp[node][root][black] != -1) return dp[node][root][black];
long long ans = 0;
if (root) {
ans = solve(adjList[node][0], false, !color[node]);
} else {
ans = solve(node, true, true) * solve(nxt[node], root, black) % 1000000007;
if (black) {
if (color[node]) {
ans = (ans + solve(adjList[node][0], false, false) *
solve(nxt[node], false, false) % 1000000007) %
1000000007;
} else {
ans = (ans + solve(adjList[node][0], false, true) *
solve(nxt[node], false, false) % 1000000007) %
1000000007;
ans = (ans + solve(adjList[node][0], false, false) *
solve(nxt[node], false, true) % 1000000007) %
1000000007;
}
} else {
if (!color[node]) {
ans = (ans + solve(adjList[node][0], false, false) *
solve(nxt[node], false, false) % 1000000007) %
1000000007;
}
}
}
return dp[node][root][black] = ans;
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int x;
scanf("%d", &x);
adjList[x].push_back(i);
}
for (int i = 0; i < n; i++) scanf("%d", &color[i]);
dfs(0);
memset(dp, -1, sizeof dp);
printf("%d", solve(0, true, true));
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 N = 100001;
int n, k;
vector<vector<int> > adjList(N);
vector<int> nxt(N, -1);
bool color[N];
int dp[N][2][2];
void dfs(int node) {
bool leaf = true;
for (int i = 0; i < adjList[node].size(); i++) {
leaf = false;
int ch = adjList[node][i];
int nextCh = i == adjList[node].size() - 1 ? -1 : adjList[node][i + 1];
nxt[ch] = nextCh;
dfs(ch);
}
if (leaf) adjList[node].push_back(-1);
}
long long solve(int node, bool root, bool black) {
if (node == -1) return black ? 0 : 1;
if (dp[node][root][black] != -1) return dp[node][root][black];
long long ans = 0;
if (root) {
ans = solve(adjList[node][0], false, !color[node]);
} else {
ans = solve(node, true, true) * solve(nxt[node], root, black) % 1000000007;
if (black) {
if (color[node]) {
ans = (ans + solve(adjList[node][0], false, false) *
solve(nxt[node], false, false) % 1000000007) %
1000000007;
} else {
ans = (ans + solve(adjList[node][0], false, true) *
solve(nxt[node], false, false) % 1000000007) %
1000000007;
ans = (ans + solve(adjList[node][0], false, false) *
solve(nxt[node], false, true) % 1000000007) %
1000000007;
}
} else {
if (!color[node]) {
ans = (ans + solve(adjList[node][0], false, false) *
solve(nxt[node], false, false) % 1000000007) %
1000000007;
}
}
}
return dp[node][root][black] = ans;
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int x;
scanf("%d", &x);
adjList[x].push_back(i);
}
for (int i = 0; i < n; i++) scanf("%d", &color[i]);
dfs(0);
memset(dp, -1, sizeof dp);
printf("%d", solve(0, true, true));
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int MAX = 1e9 + 5;
long long q[100005], prefix[100005], x, postfix[100005], dp[100005][2],
col[100005], i, j, k, n, m;
vector<int> v[100005], tv[100005];
void dfs(int node, int root) {
if (root != -1) v[root].push_back(node);
for (__typeof(tv[node].begin()) it = tv[node].begin(); it != tv[node].end();
it++)
if ((*it) != root) dfs(*it, node);
}
long long fu(int x, int y) {
if (!y) return 1;
long long t = fu(x, y / 2);
if (y & 1) return (((t * t) % 1000000007) * x) % 1000000007;
return (t * t) % 1000000007;
}
int f(int x, bool t) {
long long &r = dp[x][t];
if (r != -1) return dp[x][t];
r = 0;
if (col[x]) {
r = 1;
for (__typeof(v[x].begin()) it = v[x].begin(); it != v[x].end(); it++)
r = (r * f(*it, 1)) % 1000000007;
return r;
}
long long s = 1;
if (t) {
for (__typeof(v[x].begin()) it = v[x].begin(); it != v[x].end(); it++)
s = (s * f(*it, 1)) % 1000000007;
r = s;
s = 1;
}
for (__typeof(v[x].begin()) it = v[x].begin(); it != v[x].end(); it++)
s = (s * f(*it, 1)) % 1000000007;
for (__typeof(v[x].begin()) it = v[x].begin(); it != v[x].end(); it++) {
r += ((fu(f(*it, 1), 1000000007 - 2) * s) % 1000000007) * f(*it, 0) %
1000000007;
r %= 1000000007;
}
return r;
}
int main() {
memset(dp, -1, sizeof dp);
scanf("%lld", &n);
for (int i = 1; i <= n - 1; i++) {
scanf("%lld", &x);
tv[x + 1].push_back(i + 1);
tv[i + 1].push_back(x + 1);
}
for (int i = 1; i <= n; i++) scanf("%lld", &col[i]);
dfs(1, 0);
cout << f(1, 0);
return 0;
}
|
### Prompt
Construct a cpp code solution to the problem outlined:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int MAX = 1e9 + 5;
long long q[100005], prefix[100005], x, postfix[100005], dp[100005][2],
col[100005], i, j, k, n, m;
vector<int> v[100005], tv[100005];
void dfs(int node, int root) {
if (root != -1) v[root].push_back(node);
for (__typeof(tv[node].begin()) it = tv[node].begin(); it != tv[node].end();
it++)
if ((*it) != root) dfs(*it, node);
}
long long fu(int x, int y) {
if (!y) return 1;
long long t = fu(x, y / 2);
if (y & 1) return (((t * t) % 1000000007) * x) % 1000000007;
return (t * t) % 1000000007;
}
int f(int x, bool t) {
long long &r = dp[x][t];
if (r != -1) return dp[x][t];
r = 0;
if (col[x]) {
r = 1;
for (__typeof(v[x].begin()) it = v[x].begin(); it != v[x].end(); it++)
r = (r * f(*it, 1)) % 1000000007;
return r;
}
long long s = 1;
if (t) {
for (__typeof(v[x].begin()) it = v[x].begin(); it != v[x].end(); it++)
s = (s * f(*it, 1)) % 1000000007;
r = s;
s = 1;
}
for (__typeof(v[x].begin()) it = v[x].begin(); it != v[x].end(); it++)
s = (s * f(*it, 1)) % 1000000007;
for (__typeof(v[x].begin()) it = v[x].begin(); it != v[x].end(); it++) {
r += ((fu(f(*it, 1), 1000000007 - 2) * s) % 1000000007) * f(*it, 0) %
1000000007;
r %= 1000000007;
}
return r;
}
int main() {
memset(dp, -1, sizeof dp);
scanf("%lld", &n);
for (int i = 1; i <= n - 1; i++) {
scanf("%lld", &x);
tv[x + 1].push_back(i + 1);
tv[i + 1].push_back(x + 1);
}
for (int i = 1; i <= n; i++) scanf("%lld", &col[i]);
dfs(1, 0);
cout << f(1, 0);
return 0;
}
```
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
long long max(long long a, long long b) {
if (a > b) {
return a;
} else {
return b;
}
}
long long min(long long a, long long b) {
if (a < b) {
return a;
} else {
return b;
}
}
long long power(long long b, long long e) {
if (e == 0) return 1;
if (e % 2)
return ((b * power((b) * (b), (e - 1) / 2)));
else
return power((b) * (b), e / 2);
}
long long modpower(long long b, long long e, long long q) {
long long MOD = q;
if (e == 0) return 1;
if (e % 2)
return ((b % MOD) * modpower((b % MOD) * (b % MOD), (e - 1) / 2, q)) % MOD;
else
return modpower((b % MOD) * (b % MOD), e / 2, q) % MOD;
}
void dpv(vector<long long> v) {
for (long long i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
cout << '\n';
}
void dpv(vector<pair<long long, long long> > v) {
for (long long i = 0; i < v.size(); i++) {
cout << v[i].first << " " << v[i].second << '\n';
}
}
void dpv(set<long long> v) {
for (auto i : v) {
cout << i << " ";
}
cout << '\n';
}
long long md = 1e9 + 7;
long long dp[100005][2];
vector<vector<long long> > adj(100005);
long long color[100005];
void dfs(long long x, long long p) {
if (color[x] == 1) {
dp[x][1] = 1;
dp[x][0] = 0;
} else {
dp[x][0] = 1;
dp[x][1] = 0;
}
for (auto u : adj[x]) {
if (u != p) {
dfs(u, x);
long long one = dp[x][1];
long long zero = dp[x][0];
dp[x][1] = 0;
dp[x][0] = 0;
dp[x][0] += dp[u][0] * zero;
dp[x][0] %= md;
dp[x][1] += dp[u][1] * zero + one * dp[u][0];
dp[x][1] %= md;
dp[x][1] += one * dp[u][1];
dp[x][1] %= md;
dp[x][0] += zero * dp[u][1];
dp[x][0] %= md;
}
}
}
void oblivious() {
memset(dp, 0, sizeof(dp));
long long n;
cin >> n;
for (long long i = 0; i < n - 1; i++) {
long long x;
cin >> x;
adj[i + 1].push_back(x);
adj[x].push_back(i + 1);
}
for (long long i = 0; i < n; i++) {
cin >> color[i];
}
dfs(0, -1);
cout << dp[0][1] << '\n';
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t = 1;
while (t--) {
oblivious();
}
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>
#pragma GCC optimize("Ofast")
using namespace std;
long long max(long long a, long long b) {
if (a > b) {
return a;
} else {
return b;
}
}
long long min(long long a, long long b) {
if (a < b) {
return a;
} else {
return b;
}
}
long long power(long long b, long long e) {
if (e == 0) return 1;
if (e % 2)
return ((b * power((b) * (b), (e - 1) / 2)));
else
return power((b) * (b), e / 2);
}
long long modpower(long long b, long long e, long long q) {
long long MOD = q;
if (e == 0) return 1;
if (e % 2)
return ((b % MOD) * modpower((b % MOD) * (b % MOD), (e - 1) / 2, q)) % MOD;
else
return modpower((b % MOD) * (b % MOD), e / 2, q) % MOD;
}
void dpv(vector<long long> v) {
for (long long i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
cout << '\n';
}
void dpv(vector<pair<long long, long long> > v) {
for (long long i = 0; i < v.size(); i++) {
cout << v[i].first << " " << v[i].second << '\n';
}
}
void dpv(set<long long> v) {
for (auto i : v) {
cout << i << " ";
}
cout << '\n';
}
long long md = 1e9 + 7;
long long dp[100005][2];
vector<vector<long long> > adj(100005);
long long color[100005];
void dfs(long long x, long long p) {
if (color[x] == 1) {
dp[x][1] = 1;
dp[x][0] = 0;
} else {
dp[x][0] = 1;
dp[x][1] = 0;
}
for (auto u : adj[x]) {
if (u != p) {
dfs(u, x);
long long one = dp[x][1];
long long zero = dp[x][0];
dp[x][1] = 0;
dp[x][0] = 0;
dp[x][0] += dp[u][0] * zero;
dp[x][0] %= md;
dp[x][1] += dp[u][1] * zero + one * dp[u][0];
dp[x][1] %= md;
dp[x][1] += one * dp[u][1];
dp[x][1] %= md;
dp[x][0] += zero * dp[u][1];
dp[x][0] %= md;
}
}
}
void oblivious() {
memset(dp, 0, sizeof(dp));
long long n;
cin >> n;
for (long long i = 0; i < n - 1; i++) {
long long x;
cin >> x;
adj[i + 1].push_back(x);
adj[x].push_back(i + 1);
}
for (long long i = 0; i < n; i++) {
cin >> color[i];
}
dfs(0, -1);
cout << dp[0][1] << '\n';
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t = 1;
while (t--) {
oblivious();
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<long long> whs;
vector<long long> bs;
vector<vector<int> > T;
vector<bool> color;
long long m = 1000000007;
void fill_dp(int v) {
if (T[v].size() == 0) {
if (color[v]) {
whs[v] = 0;
bs[v] = 1;
} else {
bs[v] = 0;
whs[v] = 1;
}
return;
}
for (int i = 0; i < T[v].size(); i++) {
fill_dp(T[v][i]);
}
if (color[v]) {
whs[v] = 0;
bs[v] = 1;
for (int i = 0; i < T[v].size(); i++) {
bs[v] = (bs[v] * (bs[T[v][i]] + whs[T[v][i]])) % m;
}
} else {
vector<long long> dp0(T[v].size());
vector<long long> dp1(T[v].size());
if (color[T[v][0]]) {
dp0[0] = bs[T[v][0]];
dp1[0] = bs[T[v][0]];
whs[v] = bs[T[v][0]];
} else {
dp0[0] = (whs[T[v][0]] + bs[T[v][0]]) % m;
dp1[0] = bs[T[v][0]];
whs[v] = (whs[T[v][0]] + bs[T[v][0]]) % m;
}
for (int i = 1; i < T[v].size(); i++) {
if (color[T[v][i]]) {
whs[v] = (whs[v] * bs[T[v][i]]) % m;
dp0[i] = (dp0[i - 1] * bs[T[v][i]]) % m;
dp1[i] = ((dp0[i - 1] + dp1[i - 1]) * bs[T[v][i]]) % m;
} else {
whs[v] = (whs[T[v][i]] + bs[T[v][i]]) * whs[v] % m;
dp0[i] = (dp0[i - 1] * (whs[T[v][i]] + bs[T[v][i]])) % m;
dp1[i] = (((dp1[i - 1] * (whs[T[v][i]] + bs[T[v][i]])) % m) +
((dp0[i - 1] * bs[T[v][i]]) % m)) %
m;
}
}
bs[v] = dp1[T[v].size() - 1];
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
whs.resize(n, 0);
bs.resize(n, 0);
T.resize(n);
color.resize(n);
for (int i = 0; i < n - 1; i++) {
int pi;
cin >> pi;
T[pi].push_back(i + 1);
}
for (int i = 0; i < n; i++) {
int xi;
cin >> xi;
if (xi == 1) {
color[i] = true;
} else {
color[i] = false;
}
}
fill_dp(0);
cout << bs[0] << "\n";
}
|
### Prompt
Please provide a cpp coded solution to the problem described below:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<long long> whs;
vector<long long> bs;
vector<vector<int> > T;
vector<bool> color;
long long m = 1000000007;
void fill_dp(int v) {
if (T[v].size() == 0) {
if (color[v]) {
whs[v] = 0;
bs[v] = 1;
} else {
bs[v] = 0;
whs[v] = 1;
}
return;
}
for (int i = 0; i < T[v].size(); i++) {
fill_dp(T[v][i]);
}
if (color[v]) {
whs[v] = 0;
bs[v] = 1;
for (int i = 0; i < T[v].size(); i++) {
bs[v] = (bs[v] * (bs[T[v][i]] + whs[T[v][i]])) % m;
}
} else {
vector<long long> dp0(T[v].size());
vector<long long> dp1(T[v].size());
if (color[T[v][0]]) {
dp0[0] = bs[T[v][0]];
dp1[0] = bs[T[v][0]];
whs[v] = bs[T[v][0]];
} else {
dp0[0] = (whs[T[v][0]] + bs[T[v][0]]) % m;
dp1[0] = bs[T[v][0]];
whs[v] = (whs[T[v][0]] + bs[T[v][0]]) % m;
}
for (int i = 1; i < T[v].size(); i++) {
if (color[T[v][i]]) {
whs[v] = (whs[v] * bs[T[v][i]]) % m;
dp0[i] = (dp0[i - 1] * bs[T[v][i]]) % m;
dp1[i] = ((dp0[i - 1] + dp1[i - 1]) * bs[T[v][i]]) % m;
} else {
whs[v] = (whs[T[v][i]] + bs[T[v][i]]) * whs[v] % m;
dp0[i] = (dp0[i - 1] * (whs[T[v][i]] + bs[T[v][i]])) % m;
dp1[i] = (((dp1[i - 1] * (whs[T[v][i]] + bs[T[v][i]])) % m) +
((dp0[i - 1] * bs[T[v][i]]) % m)) %
m;
}
}
bs[v] = dp1[T[v].size() - 1];
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
whs.resize(n, 0);
bs.resize(n, 0);
T.resize(n);
color.resize(n);
for (int i = 0; i < n - 1; i++) {
int pi;
cin >> pi;
T[pi].push_back(i + 1);
}
for (int i = 0; i < n; i++) {
int xi;
cin >> xi;
if (xi == 1) {
color[i] = true;
} else {
color[i] = false;
}
}
fill_dp(0);
cout << bs[0] << "\n";
}
```
|
#include <bits/stdc++.h>
const int MAX_N = 2e5 + 5;
const int MODULO = 1000000007;
int N;
std::vector<int> children[MAX_N];
std::bitset<MAX_N> isBlack;
long long memo[MAX_N][2];
long long f(int vertex, bool blackAbove);
int gcdExtended(int a, int b, int *x, int *y) {
if (a == 0) {
*x = 0, *y = 1;
return b;
}
int x1, y1;
int gcd = gcdExtended(b % a, a, &x1, &y1);
*x = y1 - (b / a) * x1;
*y = x1;
return gcd;
}
int modInverse(int b, int m) {
int x, y;
int g = gcdExtended(b, m, &x, &y);
if (g != 1) return -1;
return (x % m + m) % m;
}
long long blackBlackAbove(int vertex) { return 0; }
long long blackWhiteAbove(int vertex) {
long long result = 1;
for (int child : children[vertex]) {
result *=
(f(child, false) + (isBlack[child] ? 0 : f(child, true))) % MODULO;
result %= MODULO;
}
return result;
}
long long whiteBlackAbove(int vertex) {
long long result = 1;
for (int child : children[vertex]) {
result *= f(child, false) + (isBlack[child] ? 0 : f(child, true));
result %= MODULO;
}
return result;
}
long long whiteWhiteAbove(int vertex) {
long long productChildren = 1;
for (int child : children[vertex]) {
productChildren *=
(f(child, false) + (isBlack[child] ? 0 : f(child, true))) % MODULO;
productChildren %= MODULO;
}
long long result = 0;
for (int child : children[vertex]) {
int fChild =
(f(child, false) + (isBlack[child] ? 0 : f(child, true))) % MODULO;
fChild = modInverse(fChild, MODULO);
result += ((productChildren * fChild) % MODULO) * f(child, false);
result %= MODULO;
}
return result;
}
long long f(int vertex, bool blackAbove) {
if (isBlack[vertex]) {
if (blackAbove) {
if (memo[vertex][1] != -1) return memo[vertex][1];
return memo[vertex][1] = blackBlackAbove(vertex);
} else {
if (memo[vertex][0] != -1) return memo[vertex][0];
return memo[vertex][0] = blackWhiteAbove(vertex);
}
} else {
if (blackAbove) {
if (memo[vertex][1] != -1) return memo[vertex][1];
return memo[vertex][1] = whiteBlackAbove(vertex);
} else {
if (memo[vertex][0] != -1) return memo[vertex][0];
return memo[vertex][0] = whiteWhiteAbove(vertex);
}
}
}
int main() {
std::cin >> N;
for (int i = 1; i < N; i++) {
int tmp;
std::cin >> tmp;
children[tmp].push_back(i);
}
for (int i = 0; i < N; i++) {
int tmp;
std::cin >> tmp;
isBlack[i] = tmp ? true : false;
}
for (int i = 0; i < MAX_N; i++) memo[i][0] = memo[i][1] = -1;
std::cout << f(0, false) << std::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>
const int MAX_N = 2e5 + 5;
const int MODULO = 1000000007;
int N;
std::vector<int> children[MAX_N];
std::bitset<MAX_N> isBlack;
long long memo[MAX_N][2];
long long f(int vertex, bool blackAbove);
int gcdExtended(int a, int b, int *x, int *y) {
if (a == 0) {
*x = 0, *y = 1;
return b;
}
int x1, y1;
int gcd = gcdExtended(b % a, a, &x1, &y1);
*x = y1 - (b / a) * x1;
*y = x1;
return gcd;
}
int modInverse(int b, int m) {
int x, y;
int g = gcdExtended(b, m, &x, &y);
if (g != 1) return -1;
return (x % m + m) % m;
}
long long blackBlackAbove(int vertex) { return 0; }
long long blackWhiteAbove(int vertex) {
long long result = 1;
for (int child : children[vertex]) {
result *=
(f(child, false) + (isBlack[child] ? 0 : f(child, true))) % MODULO;
result %= MODULO;
}
return result;
}
long long whiteBlackAbove(int vertex) {
long long result = 1;
for (int child : children[vertex]) {
result *= f(child, false) + (isBlack[child] ? 0 : f(child, true));
result %= MODULO;
}
return result;
}
long long whiteWhiteAbove(int vertex) {
long long productChildren = 1;
for (int child : children[vertex]) {
productChildren *=
(f(child, false) + (isBlack[child] ? 0 : f(child, true))) % MODULO;
productChildren %= MODULO;
}
long long result = 0;
for (int child : children[vertex]) {
int fChild =
(f(child, false) + (isBlack[child] ? 0 : f(child, true))) % MODULO;
fChild = modInverse(fChild, MODULO);
result += ((productChildren * fChild) % MODULO) * f(child, false);
result %= MODULO;
}
return result;
}
long long f(int vertex, bool blackAbove) {
if (isBlack[vertex]) {
if (blackAbove) {
if (memo[vertex][1] != -1) return memo[vertex][1];
return memo[vertex][1] = blackBlackAbove(vertex);
} else {
if (memo[vertex][0] != -1) return memo[vertex][0];
return memo[vertex][0] = blackWhiteAbove(vertex);
}
} else {
if (blackAbove) {
if (memo[vertex][1] != -1) return memo[vertex][1];
return memo[vertex][1] = whiteBlackAbove(vertex);
} else {
if (memo[vertex][0] != -1) return memo[vertex][0];
return memo[vertex][0] = whiteWhiteAbove(vertex);
}
}
}
int main() {
std::cin >> N;
for (int i = 1; i < N; i++) {
int tmp;
std::cin >> tmp;
children[tmp].push_back(i);
}
for (int i = 0; i < N; i++) {
int tmp;
std::cin >> tmp;
isBlack[i] = tmp ? true : false;
}
for (int i = 0; i < MAX_N; i++) memo[i][0] = memo[i][1] = -1;
std::cout << f(0, false) << std::endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MM = 300005;
int n;
int x[MM];
long long dp[MM][2];
vector<int> a[MM];
void DP(int u) {
dp[u][0] = 1;
dp[u][1] = 0;
for (int i = 0; i < a[u].size(); i++) {
int v = a[u][i];
DP(v);
dp[u][1] = (dp[u][0] * dp[v][1] + dp[u][1] * dp[v][0]) % 1000000007;
dp[u][0] = (dp[u][0] * dp[v][0]) % 1000000007;
}
if (x[u] == 1)
dp[u][1] = dp[u][0];
else
dp[u][0] = (dp[u][1] + dp[u][0]) % 1000000007;
}
int main() {
while (~scanf("%d", &n)) {
for (int i = 0; i <= n; i++) a[i].clear();
for (int i = 0; i < n - 1; i++) {
int u;
scanf("%d", &u);
a[u].push_back(i + 1);
}
for (int i = 0; i < n; i++) scanf("%d", &x[i]);
DP(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 MM = 300005;
int n;
int x[MM];
long long dp[MM][2];
vector<int> a[MM];
void DP(int u) {
dp[u][0] = 1;
dp[u][1] = 0;
for (int i = 0; i < a[u].size(); i++) {
int v = a[u][i];
DP(v);
dp[u][1] = (dp[u][0] * dp[v][1] + dp[u][1] * dp[v][0]) % 1000000007;
dp[u][0] = (dp[u][0] * dp[v][0]) % 1000000007;
}
if (x[u] == 1)
dp[u][1] = dp[u][0];
else
dp[u][0] = (dp[u][1] + dp[u][0]) % 1000000007;
}
int main() {
while (~scanf("%d", &n)) {
for (int i = 0; i <= n; i++) a[i].clear();
for (int i = 0; i < n - 1; i++) {
int u;
scanf("%d", &u);
a[u].push_back(i + 1);
}
for (int i = 0; i < n; i++) scanf("%d", &x[i]);
DP(0);
printf("%I64d\n", dp[0][1]);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const double eps(1e-8);
const double pi(3.14159265358979);
const int N = 100100, mod = 1000000007;
int n;
int color[N] = {};
long long f[N][2] = {};
vector<int> v[N];
void init() {
scanf("%d", &n);
for (int i = 2, x = 0; i <= n; ++i) {
scanf("%d", &x);
v[x + 1].push_back(i);
}
for (int i = 1; i <= n; ++i) scanf("%d", &color[i]);
}
void dfs(int s) {
f[s][color[s]] = 1;
long long x1 = 0, x2 = 0;
for (vector<int>::iterator it = v[s].begin(); it != v[s].end(); ++it) {
dfs(*it);
x1 = f[s][0] * f[*it][0];
x2 = f[s][1] * f[*it][0] + f[s][0] * f[*it][1];
f[s][0] = x1 % mod;
f[s][1] = x2 % mod;
}
f[s][0] = (f[s][0] + f[s][1]) % mod;
}
void work() {
dfs(1);
cout << f[1][1] << endl;
}
int main() {
init();
work();
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 double eps(1e-8);
const double pi(3.14159265358979);
const int N = 100100, mod = 1000000007;
int n;
int color[N] = {};
long long f[N][2] = {};
vector<int> v[N];
void init() {
scanf("%d", &n);
for (int i = 2, x = 0; i <= n; ++i) {
scanf("%d", &x);
v[x + 1].push_back(i);
}
for (int i = 1; i <= n; ++i) scanf("%d", &color[i]);
}
void dfs(int s) {
f[s][color[s]] = 1;
long long x1 = 0, x2 = 0;
for (vector<int>::iterator it = v[s].begin(); it != v[s].end(); ++it) {
dfs(*it);
x1 = f[s][0] * f[*it][0];
x2 = f[s][1] * f[*it][0] + f[s][0] * f[*it][1];
f[s][0] = x1 % mod;
f[s][1] = x2 % mod;
}
f[s][0] = (f[s][0] + f[s][1]) % mod;
}
void work() {
dfs(1);
cout << f[1][1] << endl;
}
int main() {
init();
work();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<int> e[100007];
long long val_array[100007];
long long val_array2[100007];
int color[100007];
int borw[100007];
int count1[100007];
void count_black(int v) {
if (color[v]) return;
color[v] = 1;
if (borw[v]) count1[v] = 1;
for (int i = 0; i < e[v].size(); i++) {
if (!color[e[v][i]]) {
count_black(e[v][i]);
count1[v] += count1[e[v][i]];
}
}
}
long long dfs(int v) {
color[v] = 1;
if (!borw[v]) {
val_array[v] = 0;
val_array2[v] = 1;
for (int i = 0; i < e[v].size(); i++) {
if (!color[e[v][i]]) {
long long temp = dfs(e[v][i]);
if (count1[e[v][i]] && val_array[v])
val_array[v] =
((temp)*val_array2[v] + val_array2[e[v][i]] * val_array[v] +
val_array[v] * val_array[e[v][i]]) %
1000000007;
else if ((!val_array[v]) && count1[e[v][i]])
val_array[v] = temp % 1000000007;
val_array2[v] = ((val_array2[v] * val_array[e[v][i]]) +
val_array2[v] * val_array2[e[v][i]]) %
1000000007;
}
}
} else {
val_array2[v] = 0;
val_array[v] = 1;
for (int i = 0; i < e[v].size(); i++) {
if (!color[e[v][i]]) {
long long temp = dfs(e[v][i]);
if (count1[e[v][i]]) {
val_array[v] =
(val_array[v] * temp + val_array[v] * val_array2[e[v][i]] +
val_array2[v] * val_array[e[v][i]]) %
1000000007;
}
}
}
}
return val_array[v];
}
int main() {
int n;
cin >> n;
int a;
for (int i = 1; i <= n - 1; i++) {
cin >> a;
e[i].push_back(a);
e[a].push_back(i);
}
for (int i = 0; i < n; i++) {
cin >> borw[i];
}
count_black(0);
memset(color, 0, sizeof(color));
cout << dfs(0);
}
|
### Prompt
Please create a solution in Cpp to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> e[100007];
long long val_array[100007];
long long val_array2[100007];
int color[100007];
int borw[100007];
int count1[100007];
void count_black(int v) {
if (color[v]) return;
color[v] = 1;
if (borw[v]) count1[v] = 1;
for (int i = 0; i < e[v].size(); i++) {
if (!color[e[v][i]]) {
count_black(e[v][i]);
count1[v] += count1[e[v][i]];
}
}
}
long long dfs(int v) {
color[v] = 1;
if (!borw[v]) {
val_array[v] = 0;
val_array2[v] = 1;
for (int i = 0; i < e[v].size(); i++) {
if (!color[e[v][i]]) {
long long temp = dfs(e[v][i]);
if (count1[e[v][i]] && val_array[v])
val_array[v] =
((temp)*val_array2[v] + val_array2[e[v][i]] * val_array[v] +
val_array[v] * val_array[e[v][i]]) %
1000000007;
else if ((!val_array[v]) && count1[e[v][i]])
val_array[v] = temp % 1000000007;
val_array2[v] = ((val_array2[v] * val_array[e[v][i]]) +
val_array2[v] * val_array2[e[v][i]]) %
1000000007;
}
}
} else {
val_array2[v] = 0;
val_array[v] = 1;
for (int i = 0; i < e[v].size(); i++) {
if (!color[e[v][i]]) {
long long temp = dfs(e[v][i]);
if (count1[e[v][i]]) {
val_array[v] =
(val_array[v] * temp + val_array[v] * val_array2[e[v][i]] +
val_array2[v] * val_array[e[v][i]]) %
1000000007;
}
}
}
}
return val_array[v];
}
int main() {
int n;
cin >> n;
int a;
for (int i = 1; i <= n - 1; i++) {
cin >> a;
e[i].push_back(a);
e[a].push_back(i);
}
for (int i = 0; i < n; i++) {
cin >> borw[i];
}
count_black(0);
memset(color, 0, sizeof(color));
cout << dfs(0);
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<int> g[100010];
long long dp[100010][2];
long long num = 1e9 + 7;
int col[100010];
void dfs(int cur, int par) {
dp[cur][col[cur]] = 1;
for (int i = 0; i < g[cur].size(); i++) {
if (g[cur][i] != par) {
dfs(g[cur][i], cur);
dp[cur][1] = (dp[cur][1] * (dp[g[cur][i]][0] + dp[g[cur][i]][1]) % num +
(dp[cur][0] * dp[g[cur][i]][1]) % num) %
num;
dp[cur][0] = (dp[cur][0] * (dp[g[cur][i]][0] + dp[g[cur][i]][1])) % num;
}
}
}
int main() {
int n, t;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
scanf("%d", &t);
g[i].push_back(t);
g[t].push_back(i);
}
for (int i = 0; i < n; i++) scanf("%d", &col[i]);
dfs(0, -1);
printf("%I64d\n", dp[0][1]);
}
|
### Prompt
Develop a solution in Cpp to the problem described below:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
vector<int> g[100010];
long long dp[100010][2];
long long num = 1e9 + 7;
int col[100010];
void dfs(int cur, int par) {
dp[cur][col[cur]] = 1;
for (int i = 0; i < g[cur].size(); i++) {
if (g[cur][i] != par) {
dfs(g[cur][i], cur);
dp[cur][1] = (dp[cur][1] * (dp[g[cur][i]][0] + dp[g[cur][i]][1]) % num +
(dp[cur][0] * dp[g[cur][i]][1]) % num) %
num;
dp[cur][0] = (dp[cur][0] * (dp[g[cur][i]][0] + dp[g[cur][i]][1])) % num;
}
}
}
int main() {
int n, t;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
scanf("%d", &t);
g[i].push_back(t);
g[t].push_back(i);
}
for (int i = 0; i < n; i++) scanf("%d", &col[i]);
dfs(0, -1);
printf("%I64d\n", dp[0][1]);
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long int power(long long int a, long long int b) {
a %= 1000000007;
long long int res = 1;
while (b > 0) {
if (b & 1) res = res * a % 1000000007;
a = a * a % 1000000007;
b >>= 1;
}
return res;
}
long long int modinv(long long int p) { return power(p, 1000000007 - 2); }
vector<long long int> arr[200000];
long long int dp[100005][2] = {0};
void dfs(long long int s, long long int p, long long int a[]) {
if (a[s] == 0) {
long long int su = 1;
dp[s][1] = 0;
for (auto it : arr[s]) {
if (it != p) {
dfs(it, s, a);
su *= (dp[it][0] + dp[it][1]);
su %= 1000000007;
}
}
dp[s][0] = su % 1000000007;
for (auto it : arr[s]) {
if (it != p) {
if (dp[it][0] + dp[it][1] > 0) {
dp[s][1] += (dp[it][1] *
((su * modinv(dp[it][0] + dp[it][1])) % 1000000007)) %
1000000007;
dp[s][1] %= 1000000007;
}
}
}
} else {
dp[s][0] = 0;
dp[s][1] = 1;
for (auto it : arr[s]) {
if (it != p) {
dfs(it, s, a);
dp[s][1] *= (dp[it][0] + dp[it][1]) % 1000000007;
dp[s][1] %= 1000000007;
}
}
}
}
int main() {
long long int n;
cin >> n;
long long int q;
for (long long int x = 1; x < n; x++) {
cin >> q;
arr[q].push_back(x);
arr[x].push_back(q);
}
long long int a[n];
for (int x = 0; x < n; x++) {
cin >> a[x];
}
dfs(0, -1, a);
cout << dp[0][1] % 1000000007 << "\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;
long long int power(long long int a, long long int b) {
a %= 1000000007;
long long int res = 1;
while (b > 0) {
if (b & 1) res = res * a % 1000000007;
a = a * a % 1000000007;
b >>= 1;
}
return res;
}
long long int modinv(long long int p) { return power(p, 1000000007 - 2); }
vector<long long int> arr[200000];
long long int dp[100005][2] = {0};
void dfs(long long int s, long long int p, long long int a[]) {
if (a[s] == 0) {
long long int su = 1;
dp[s][1] = 0;
for (auto it : arr[s]) {
if (it != p) {
dfs(it, s, a);
su *= (dp[it][0] + dp[it][1]);
su %= 1000000007;
}
}
dp[s][0] = su % 1000000007;
for (auto it : arr[s]) {
if (it != p) {
if (dp[it][0] + dp[it][1] > 0) {
dp[s][1] += (dp[it][1] *
((su * modinv(dp[it][0] + dp[it][1])) % 1000000007)) %
1000000007;
dp[s][1] %= 1000000007;
}
}
}
} else {
dp[s][0] = 0;
dp[s][1] = 1;
for (auto it : arr[s]) {
if (it != p) {
dfs(it, s, a);
dp[s][1] *= (dp[it][0] + dp[it][1]) % 1000000007;
dp[s][1] %= 1000000007;
}
}
}
}
int main() {
long long int n;
cin >> n;
long long int q;
for (long long int x = 1; x < n; x++) {
cin >> q;
arr[q].push_back(x);
arr[x].push_back(q);
}
long long int a[n];
for (int x = 0; x < n; x++) {
cin >> a[x];
}
dfs(0, -1, a);
cout << dp[0][1] % 1000000007 << "\n";
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const int mod = 1000000007;
long long dp[maxn][2];
int x[maxn];
int n;
vector<int> G[maxn];
void DP(int u) {
dp[u][x[u]] = 1;
for (int i = 0; i < G[u].size(); i++) {
int son = G[u][i];
DP(son);
dp[u][1] = (dp[u][1] * dp[son][1] % mod + dp[u][0] * dp[son][1] % mod +
dp[u][1] * dp[son][0] % mod) %
mod;
dp[u][0] =
(dp[u][0] * dp[son][1] % mod + dp[u][0] * dp[son][0] % mod) % mod;
}
}
int main() {
for (int i = 0; i < maxn; i++) {
G[i].clear();
}
memset(dp, 0, sizeof(dp));
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int p;
scanf("%d", &p);
G[p].push_back(i);
}
for (int i = 0; i < n; i++) {
scanf("%d", &x[i]);
}
DP(0);
printf("%lld\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;
const int maxn = 1e5 + 5;
const int mod = 1000000007;
long long dp[maxn][2];
int x[maxn];
int n;
vector<int> G[maxn];
void DP(int u) {
dp[u][x[u]] = 1;
for (int i = 0; i < G[u].size(); i++) {
int son = G[u][i];
DP(son);
dp[u][1] = (dp[u][1] * dp[son][1] % mod + dp[u][0] * dp[son][1] % mod +
dp[u][1] * dp[son][0] % mod) %
mod;
dp[u][0] =
(dp[u][0] * dp[son][1] % mod + dp[u][0] * dp[son][0] % mod) % mod;
}
}
int main() {
for (int i = 0; i < maxn; i++) {
G[i].clear();
}
memset(dp, 0, sizeof(dp));
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int p;
scanf("%d", &p);
G[p].push_back(i);
}
for (int i = 0; i < n; i++) {
scanf("%d", &x[i]);
}
DP(0);
printf("%lld\n", dp[0][1]);
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long xyp(long long x, long long y) {
if (y == 0) return 1LL;
if (y == 1) return x;
if (y % 2) {
long long p = xyp(x, y - 1);
return (x * p) % 1000000007;
}
long long p = xyp(x, y / 2);
return (p * p) % 1000000007;
}
long long inv(long long x) { return xyp(x, 1000000007 - 2); }
long long fact[10000007];
long long pr[10000007];
vector<long long> rp;
void init() {
fact[0] = 1;
for (long long i = 1; i < 10000007; i++) {
fact[i] = (fact[i - 1] * i) % 1000000007;
}
}
long long ncr(long long n, long long r) {
if (r == 0 || n == r) return 1LL;
if (r == 1 || ((n - r) == 1)) return n;
long long p =
(((fact[n] * inv(fact[r])) % 1000000007) * inv(fact[n - r])) % 1000000007;
return p;
}
void sieve() {
memset(pr, 1, sizeof(pr));
pr[0] = pr[1] = 0;
for (long long i = 2; i < 10000007; i++) {
if (pr[i]) {
rp.push_back(i);
for (long long j = i * i; j < 10000007; j += i) {
pr[j] = 0;
}
}
}
}
vector<long long> a[100005];
long long bl[100005];
long long n;
long long dp[100005][2];
void dfs(long long v, long long pv) {
dp[v][0] = 1 - bl[v];
dp[v][1] = bl[v];
for (auto x : a[v]) {
if (x == pv) continue;
dfs(x, v);
long long wd = dp[v][0];
dp[v][0] = 0;
long long bd = dp[v][1];
dp[v][1] = 0;
dp[v][0] = ((wd * dp[x][1]) % 1000000007 + (wd * dp[x][0]) % 1000000007) %
1000000007;
dp[v][1] = (((bd * dp[x][1]) % 1000000007 + (bd * dp[x][0]) % 1000000007) %
1000000007 +
(wd * dp[x][1]) % 1000000007) %
1000000007;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n;
for (long long i = 1; i < n; i++) {
long long x;
cin >> x;
a[i].push_back(x);
a[x].push_back(i);
}
for (long long i = 0; i < n; i++) {
cin >> bl[i];
}
dfs(0, 0);
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 xyp(long long x, long long y) {
if (y == 0) return 1LL;
if (y == 1) return x;
if (y % 2) {
long long p = xyp(x, y - 1);
return (x * p) % 1000000007;
}
long long p = xyp(x, y / 2);
return (p * p) % 1000000007;
}
long long inv(long long x) { return xyp(x, 1000000007 - 2); }
long long fact[10000007];
long long pr[10000007];
vector<long long> rp;
void init() {
fact[0] = 1;
for (long long i = 1; i < 10000007; i++) {
fact[i] = (fact[i - 1] * i) % 1000000007;
}
}
long long ncr(long long n, long long r) {
if (r == 0 || n == r) return 1LL;
if (r == 1 || ((n - r) == 1)) return n;
long long p =
(((fact[n] * inv(fact[r])) % 1000000007) * inv(fact[n - r])) % 1000000007;
return p;
}
void sieve() {
memset(pr, 1, sizeof(pr));
pr[0] = pr[1] = 0;
for (long long i = 2; i < 10000007; i++) {
if (pr[i]) {
rp.push_back(i);
for (long long j = i * i; j < 10000007; j += i) {
pr[j] = 0;
}
}
}
}
vector<long long> a[100005];
long long bl[100005];
long long n;
long long dp[100005][2];
void dfs(long long v, long long pv) {
dp[v][0] = 1 - bl[v];
dp[v][1] = bl[v];
for (auto x : a[v]) {
if (x == pv) continue;
dfs(x, v);
long long wd = dp[v][0];
dp[v][0] = 0;
long long bd = dp[v][1];
dp[v][1] = 0;
dp[v][0] = ((wd * dp[x][1]) % 1000000007 + (wd * dp[x][0]) % 1000000007) %
1000000007;
dp[v][1] = (((bd * dp[x][1]) % 1000000007 + (bd * dp[x][0]) % 1000000007) %
1000000007 +
(wd * dp[x][1]) % 1000000007) %
1000000007;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n;
for (long long i = 1; i < n; i++) {
long long x;
cin >> x;
a[i].push_back(x);
a[x].push_back(i);
}
for (long long i = 0; i < n; i++) {
cin >> bl[i];
}
dfs(0, 0);
cout << dp[0][1] << "\n";
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
struct listNode {
long long int n;
listNode *next;
listNode(long long int n = 0LL) : n(n), next(NULL) {}
};
long long int num;
listNode *edge[100100], *tail[100100];
long long int color[100100];
void insertEdge(long long int a, long long int b) {
if (!edge[a]) {
edge[a] = new listNode(b);
tail[a] = edge[a];
} else {
tail[a]->next = new listNode(b);
tail[a] = tail[a]->next;
}
}
void dfs(long long int node, long long int parent, long long int &res0,
long long int &res1) {
long long int a, b, temp;
if (color[node]) {
res0 = 0LL;
res1 = 1LL;
} else {
res0 = 1LL;
res1 = 0LL;
}
for (listNode *ptr = edge[node]; ptr; ptr = ptr->next) {
if (ptr->n == parent) continue;
dfs(ptr->n, node, a, b);
if (color[node]) {
res1 *= (a + b);
if (res1 >= 1000000007) res1 %= 1000000007;
} else {
res1 *= (a + b);
if (res1 >= 1000000007) res1 %= 1000000007;
temp = res0 * b;
if (temp >= 1000000007) temp %= 1000000007;
res1 += temp;
if (res1 >= 1000000007) res1 %= 1000000007;
res0 *= (a + b);
if (res0 >= 1000000007) res0 %= 1000000007;
}
}
}
int main() {
long long int i, a, b;
scanf("%I64d", &num);
for (i = 1; i <= num - 1; i++) {
scanf("%I64d", &a);
insertEdge(i, a);
insertEdge(a, i);
}
for (i = 0; i < num; i++) scanf("%I64d", color + i);
dfs(0, 0, a, b);
printf("%I64d\n", b);
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;
struct listNode {
long long int n;
listNode *next;
listNode(long long int n = 0LL) : n(n), next(NULL) {}
};
long long int num;
listNode *edge[100100], *tail[100100];
long long int color[100100];
void insertEdge(long long int a, long long int b) {
if (!edge[a]) {
edge[a] = new listNode(b);
tail[a] = edge[a];
} else {
tail[a]->next = new listNode(b);
tail[a] = tail[a]->next;
}
}
void dfs(long long int node, long long int parent, long long int &res0,
long long int &res1) {
long long int a, b, temp;
if (color[node]) {
res0 = 0LL;
res1 = 1LL;
} else {
res0 = 1LL;
res1 = 0LL;
}
for (listNode *ptr = edge[node]; ptr; ptr = ptr->next) {
if (ptr->n == parent) continue;
dfs(ptr->n, node, a, b);
if (color[node]) {
res1 *= (a + b);
if (res1 >= 1000000007) res1 %= 1000000007;
} else {
res1 *= (a + b);
if (res1 >= 1000000007) res1 %= 1000000007;
temp = res0 * b;
if (temp >= 1000000007) temp %= 1000000007;
res1 += temp;
if (res1 >= 1000000007) res1 %= 1000000007;
res0 *= (a + b);
if (res0 >= 1000000007) res0 %= 1000000007;
}
}
}
int main() {
long long int i, a, b;
scanf("%I64d", &num);
for (i = 1; i <= num - 1; i++) {
scanf("%I64d", &a);
insertEdge(i, a);
insertEdge(a, i);
}
for (i = 0; i < num; i++) scanf("%I64d", color + i);
dfs(0, 0, a, b);
printf("%I64d\n", b);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int N = 100000 + 10;
int n;
vector<int> adj[N];
int color[N];
unsigned long long dp[N][2];
unsigned long long pow_mod(unsigned long long x, int n,
unsigned long long mod) {
unsigned long long ret = 1;
while (n) {
if (n & 1) ret = ret * x % mod;
x = x * x % mod;
n >>= 1;
}
return ret;
}
unsigned long long inv(unsigned long long x) {
if (x == 0 || x == 1) return 1;
return pow_mod(x, MOD - 2, MOD);
}
void dfs(int t, int p) {
for (int i = 0; i < adj[t].size(); i++) {
int x = adj[t][i];
if (x != p) dfs(x, t);
}
if (color[t]) {
dp[t][1] = 1;
dp[t][0] = 0;
if (adj[t].size() == 1 && adj[t][0] == p) return;
for (int i = 0; i < adj[t].size(); i++) {
int x = adj[t][i];
if (x != p) {
dp[t][1] *= (dp[x][1] + dp[x][0]);
dp[t][1] %= MOD;
}
}
} else {
dp[t][0] = 1;
dp[t][1] = 0;
if (adj[t].size() == 1 && adj[t][0] == p) return;
for (int i = 0; i < adj[t].size(); i++) {
int x = adj[t][i];
if (x != p) {
dp[t][0] *= (dp[x][0] + dp[x][1]);
dp[t][0] %= MOD;
}
}
for (int i = 0; i < adj[t].size(); i++) {
int x = adj[t][i];
if (x != p && dp[x][1] > 0) {
dp[t][1] += dp[t][0] * inv(dp[x][0] + dp[x][1]) % MOD * dp[x][1] % MOD;
dp[t][1] %= MOD;
}
}
}
}
int main() {
scanf("%d", &n);
for (int i = 0; i <= n; i++) adj[i].clear();
int t;
for (int i = 0; i < n - 1; i++) {
scanf("%d", &t);
adj[i + 1].push_back(t);
adj[t].push_back(i + 1);
}
for (int i = 0; i < n; i++) {
scanf("%d", &color[i]);
}
dfs(0, -1);
printf("%llu\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;
const int MOD = 1000000007;
const int N = 100000 + 10;
int n;
vector<int> adj[N];
int color[N];
unsigned long long dp[N][2];
unsigned long long pow_mod(unsigned long long x, int n,
unsigned long long mod) {
unsigned long long ret = 1;
while (n) {
if (n & 1) ret = ret * x % mod;
x = x * x % mod;
n >>= 1;
}
return ret;
}
unsigned long long inv(unsigned long long x) {
if (x == 0 || x == 1) return 1;
return pow_mod(x, MOD - 2, MOD);
}
void dfs(int t, int p) {
for (int i = 0; i < adj[t].size(); i++) {
int x = adj[t][i];
if (x != p) dfs(x, t);
}
if (color[t]) {
dp[t][1] = 1;
dp[t][0] = 0;
if (adj[t].size() == 1 && adj[t][0] == p) return;
for (int i = 0; i < adj[t].size(); i++) {
int x = adj[t][i];
if (x != p) {
dp[t][1] *= (dp[x][1] + dp[x][0]);
dp[t][1] %= MOD;
}
}
} else {
dp[t][0] = 1;
dp[t][1] = 0;
if (adj[t].size() == 1 && adj[t][0] == p) return;
for (int i = 0; i < adj[t].size(); i++) {
int x = adj[t][i];
if (x != p) {
dp[t][0] *= (dp[x][0] + dp[x][1]);
dp[t][0] %= MOD;
}
}
for (int i = 0; i < adj[t].size(); i++) {
int x = adj[t][i];
if (x != p && dp[x][1] > 0) {
dp[t][1] += dp[t][0] * inv(dp[x][0] + dp[x][1]) % MOD * dp[x][1] % MOD;
dp[t][1] %= MOD;
}
}
}
}
int main() {
scanf("%d", &n);
for (int i = 0; i <= n; i++) adj[i].clear();
int t;
for (int i = 0; i < n - 1; i++) {
scanf("%d", &t);
adj[i + 1].push_back(t);
adj[t].push_back(i + 1);
}
for (int i = 0; i < n; i++) {
scanf("%d", &color[i]);
}
dfs(0, -1);
printf("%llu\n", dp[0][1]);
return 0;
}
```
|
#include <bits/stdc++.h>
template <class T>
T read(T &a) {
a = 0;
char x = getchar();
bool f = 0;
for (; x < '0' || x > '9'; x = getchar()) f |= x == '-';
for (; x >= '0' && x <= '9'; x = getchar()) a = (a << 3) + (a << 1) + x - '0';
if (f) a = -a;
return a;
}
using namespace std;
const int N = 2e5 + 5, MOD = 1e9 + 7;
int nnext[N], v[N], d[N];
int F[N], cnt;
long long f[N][2];
void add(int a, int b) {
nnext[++cnt] = F[a];
F[a] = cnt;
v[cnt] = b;
}
void dfs(int u, int fa) {
f[u][0] = 1;
f[u][1] = 0;
for (int i = F[u]; i; i = nnext[i]) {
int to = v[i];
if (to == fa) continue;
dfs(to, u);
f[u][1] *= f[to][0];
f[u][1] += f[to][1] * f[u][0];
f[u][0] *= f[to][0];
f[u][1] %= MOD;
f[u][0] %= MOD;
}
if (d[u]) {
f[u][1] = f[u][0];
} else {
f[u][0] += f[u][1];
}
f[u][1] %= MOD;
f[u][0] %= MOD;
}
int main() {
int n;
read(n);
for (int i = 2; i <= n; ++i) {
int a;
read(a);
a++;
add(a, i);
add(i, a);
}
for (int i = 1; i <= n; ++i) {
read(d[i]);
}
dfs(1, 0);
printf("%lld\n", f[1][1] % MOD);
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>
template <class T>
T read(T &a) {
a = 0;
char x = getchar();
bool f = 0;
for (; x < '0' || x > '9'; x = getchar()) f |= x == '-';
for (; x >= '0' && x <= '9'; x = getchar()) a = (a << 3) + (a << 1) + x - '0';
if (f) a = -a;
return a;
}
using namespace std;
const int N = 2e5 + 5, MOD = 1e9 + 7;
int nnext[N], v[N], d[N];
int F[N], cnt;
long long f[N][2];
void add(int a, int b) {
nnext[++cnt] = F[a];
F[a] = cnt;
v[cnt] = b;
}
void dfs(int u, int fa) {
f[u][0] = 1;
f[u][1] = 0;
for (int i = F[u]; i; i = nnext[i]) {
int to = v[i];
if (to == fa) continue;
dfs(to, u);
f[u][1] *= f[to][0];
f[u][1] += f[to][1] * f[u][0];
f[u][0] *= f[to][0];
f[u][1] %= MOD;
f[u][0] %= MOD;
}
if (d[u]) {
f[u][1] = f[u][0];
} else {
f[u][0] += f[u][1];
}
f[u][1] %= MOD;
f[u][0] %= MOD;
}
int main() {
int n;
read(n);
for (int i = 2; i <= n; ++i) {
int a;
read(a);
a++;
add(a, i);
add(i, a);
}
for (int i = 1; i <= n; ++i) {
read(d[i]);
}
dfs(1, 0);
printf("%lld\n", f[1][1] % MOD);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long int M = 1e5 + 7;
const long long int mod = 1e9 + 7;
const long long int infi = LLONG_MAX;
long long int ans, k, n, x, y, m, mymax = LLONG_MIN, mymin = LLONG_MAX, b, c, z,
sum;
long long int dp[M][2];
long long int color[M], visit[M];
vector<long long int> v[M];
long long int binpow(long long int base, long long int pow,
long long int modulus) {
long long int ans = 1;
base = base % modulus;
while (pow > 0) {
while (pow % 2 == 0) {
base = (base * base) % modulus;
pow /= 2;
}
ans = (ans * base) % modulus;
pow--;
}
return ans;
}
long long int dfs(long long int node) {
visit[node] = 1;
long long int flag = 0;
long long int val1 = 1, val2 = 0;
for (auto kk : v[node]) {
if (visit[kk] != 1) {
flag = 1;
dfs(kk);
val1 = ((val1 * (dp[kk][0] + dp[kk][1])) % mod);
}
}
if (flag == 0) {
dp[node][color[node]] = 1;
visit[node] = 0;
return 0;
}
if (color[node] == 1) {
dp[node][1] = val1;
visit[node] = 0;
return 0;
}
for (auto kk : v[node]) {
if (visit[kk] != 1) {
long long int temp1, temp2;
temp1 = ((val1 * (dp[kk][1])) % mod);
temp2 = binpow(dp[kk][1] + dp[kk][0], mod - 2, mod);
temp1 = ((temp2 * temp1) % mod);
val2 = (val2 + temp1) % mod;
}
}
dp[node][1] = val2;
dp[node][0] = val1;
visit[node] = 0;
}
int main() {
long long int i, j;
scanf("%lld", &n);
for (i = 1; i < n; i++) {
scanf("%lld", &x);
v[i].push_back(x);
v[x].push_back(i);
}
for (i = 0; i < n; i++) {
scanf("%lld", &x);
if (x == 1) y = i;
color[i] = x;
}
dfs(0);
printf("%lld\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 long long int M = 1e5 + 7;
const long long int mod = 1e9 + 7;
const long long int infi = LLONG_MAX;
long long int ans, k, n, x, y, m, mymax = LLONG_MIN, mymin = LLONG_MAX, b, c, z,
sum;
long long int dp[M][2];
long long int color[M], visit[M];
vector<long long int> v[M];
long long int binpow(long long int base, long long int pow,
long long int modulus) {
long long int ans = 1;
base = base % modulus;
while (pow > 0) {
while (pow % 2 == 0) {
base = (base * base) % modulus;
pow /= 2;
}
ans = (ans * base) % modulus;
pow--;
}
return ans;
}
long long int dfs(long long int node) {
visit[node] = 1;
long long int flag = 0;
long long int val1 = 1, val2 = 0;
for (auto kk : v[node]) {
if (visit[kk] != 1) {
flag = 1;
dfs(kk);
val1 = ((val1 * (dp[kk][0] + dp[kk][1])) % mod);
}
}
if (flag == 0) {
dp[node][color[node]] = 1;
visit[node] = 0;
return 0;
}
if (color[node] == 1) {
dp[node][1] = val1;
visit[node] = 0;
return 0;
}
for (auto kk : v[node]) {
if (visit[kk] != 1) {
long long int temp1, temp2;
temp1 = ((val1 * (dp[kk][1])) % mod);
temp2 = binpow(dp[kk][1] + dp[kk][0], mod - 2, mod);
temp1 = ((temp2 * temp1) % mod);
val2 = (val2 + temp1) % mod;
}
}
dp[node][1] = val2;
dp[node][0] = val1;
visit[node] = 0;
}
int main() {
long long int i, j;
scanf("%lld", &n);
for (i = 1; i < n; i++) {
scanf("%lld", &x);
v[i].push_back(x);
v[x].push_back(i);
}
for (i = 0; i < n; i++) {
scanf("%lld", &x);
if (x == 1) y = i;
color[i] = x;
}
dfs(0);
printf("%lld\n", dp[0][1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
double eps = 1e-9;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const int N = 1e5 + 7;
long long dp[N][2];
vector<int> g[N];
int x[N];
void dfs(int v, int p) {
dp[v][0] = 1;
dp[v][1] = 0;
for (int to : g[v]) {
if (to != p) {
dfs(to, v);
dp[v][1] = (dp[v][1] * dp[to][0]) % MOD;
dp[v][1] = (dp[v][1] + dp[v][0] * dp[to][1] % MOD) % MOD;
dp[v][0] = (dp[v][0] * dp[to][0]) % MOD;
}
}
if (x[v])
dp[v][1] = dp[v][0];
else
dp[v][0] += dp[v][1];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 0; i < (int)(n - 1); ++i) {
int j;
cin >> j;
g[i + 1].push_back(j);
g[j].push_back(i + 1);
}
for (int i = 0; i < (int)(n); ++i) cin >> x[i];
dfs(0, 0);
cout << dp[0][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;
double eps = 1e-9;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const int N = 1e5 + 7;
long long dp[N][2];
vector<int> g[N];
int x[N];
void dfs(int v, int p) {
dp[v][0] = 1;
dp[v][1] = 0;
for (int to : g[v]) {
if (to != p) {
dfs(to, v);
dp[v][1] = (dp[v][1] * dp[to][0]) % MOD;
dp[v][1] = (dp[v][1] + dp[v][0] * dp[to][1] % MOD) % MOD;
dp[v][0] = (dp[v][0] * dp[to][0]) % MOD;
}
}
if (x[v])
dp[v][1] = dp[v][0];
else
dp[v][0] += dp[v][1];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 0; i < (int)(n - 1); ++i) {
int j;
cin >> j;
g[i + 1].push_back(j);
g[j].push_back(i + 1);
}
for (int i = 0; i < (int)(n); ++i) cin >> x[i];
dfs(0, 0);
cout << dp[0][1] << endl;
}
```
|
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:256000000")
using namespace std;
const long long P = 1000000007LL;
const int maxN = 110000;
vector<int> g[maxN];
int n;
int c[maxN];
long long d[2][maxN];
long long dp[maxN][2];
void dfs(int v) {
for (int i = 0; i < g[v].size(); ++i) {
dfs(g[v][i]);
}
if (g[v].size() == 0) {
d[c[v]][v] = 1;
return;
}
for (int i = 0; i <= g[v].size(); ++i) {
dp[i][0] = dp[i][1] = 0;
}
dp[0][c[v]] = 1;
for (int i = 0; i < g[v].size(); ++i) {
for (int j = 0; j < 2; ++j) {
if (dp[i][j] == 0) {
continue;
}
{
for (int k = 0; k < 2; ++k) {
if (j + k < 2) {
dp[i + 1][j + k] += dp[i][j] * d[k][g[v][i]];
dp[i + 1][j + k] %= P;
}
}
}
{
dp[i + 1][j] += dp[i][j] * d[1][g[v][i]];
dp[i + 1][j] %= P;
}
}
}
d[0][v] = dp[g[v].size()][0];
d[1][v] = dp[g[v].size()][1];
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n - 1; ++i) {
int v;
scanf("%d", &v);
g[v].push_back(i + 1);
}
int cnt = 0;
for (int i = 0; i < n; ++i) {
scanf("%d", &c[i]);
cnt += c[i];
}
if (cnt == 0) {
printf("0\n");
return 0;
}
dfs(0);
cout << d[1][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>
#pragma comment(linker, "/STACK:256000000")
using namespace std;
const long long P = 1000000007LL;
const int maxN = 110000;
vector<int> g[maxN];
int n;
int c[maxN];
long long d[2][maxN];
long long dp[maxN][2];
void dfs(int v) {
for (int i = 0; i < g[v].size(); ++i) {
dfs(g[v][i]);
}
if (g[v].size() == 0) {
d[c[v]][v] = 1;
return;
}
for (int i = 0; i <= g[v].size(); ++i) {
dp[i][0] = dp[i][1] = 0;
}
dp[0][c[v]] = 1;
for (int i = 0; i < g[v].size(); ++i) {
for (int j = 0; j < 2; ++j) {
if (dp[i][j] == 0) {
continue;
}
{
for (int k = 0; k < 2; ++k) {
if (j + k < 2) {
dp[i + 1][j + k] += dp[i][j] * d[k][g[v][i]];
dp[i + 1][j + k] %= P;
}
}
}
{
dp[i + 1][j] += dp[i][j] * d[1][g[v][i]];
dp[i + 1][j] %= P;
}
}
}
d[0][v] = dp[g[v].size()][0];
d[1][v] = dp[g[v].size()][1];
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n - 1; ++i) {
int v;
scanf("%d", &v);
g[v].push_back(i + 1);
}
int cnt = 0;
for (int i = 0; i < n; ++i) {
scanf("%d", &c[i]);
cnt += c[i];
}
if (cnt == 0) {
printf("0\n");
return 0;
}
dfs(0);
cout << d[1][0] << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = (int)200004;
const int dx[] = {-1, 0, 1, 0, -1, -1, 1, 1};
const int dy[] = {0, 1, 0, -1, -1, 1, -1, 1};
char dir[] = {'L', 'D', 'R', 'U'};
const long long int mod = (long long int)1e9 + 7;
const long long int inf = 1e18 + 1000;
int BIT[200005], n;
long long int prime[17], spf[N];
template <typename T>
T lcm(T a, T b) {
return a / gcd(a, b) * b;
}
template <typename T>
T min3(T a, T b, T c) {
return min(a, min(b, c));
}
template <typename T>
T max3(T a, T b, T c) {
return max(a, max(b, c));
}
bool isVowel(char ch) {
ch = toupper(ch);
if (ch == 'A' || ch == 'U' || ch == 'I' || ch == 'O' || ch == 'E')
return true;
return false;
}
long long int mInv(long long int a, long long int m) {
long long int m0 = m;
long long int y = 0, x = 1;
if (m == 1) return 0;
while (a > 1) {
long long int q = a / m;
long long int t = m;
m = a % m, a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0) x += m0;
return x;
}
long long int power(long long int x, long long int y) {
long long int res = 1;
x = x % mod;
while (y > 0) {
if (y & 1) res = (res * x) % mod;
y = y >> 1;
x = (x * x) % mod;
}
return res;
}
long long int gcd(long long int a, long long int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long Extendedgcd(long long a, long long b, long long *x, long long *y) {
if (!a) {
*x = 0;
*y = 1;
return b;
}
long long nx, ny;
long long d = Extendedgcd(b % a, a, &nx, &ny);
long long k = b / a;
*y = nx;
*x = -k * nx + ny;
return d;
}
void update(int x, int delta) {
if (!x) {
BIT[x] += delta;
return;
}
for (; x <= N; x += x & -x) BIT[x] += delta;
}
long long int query(int x) {
long long int sum = BIT[0];
for (; x > 0; x -= x & -x) sum += BIT[x];
return sum;
}
void Sieve() {
for (int i = 1; i <= N; i++) prime[i] = 1;
prime[0] = 0;
prime[1] = 0;
for (long long int p = 2; p * p <= N; p++) {
if (prime[p] == 1) {
for (long long int i = p * p; i <= N; i += p) prime[i] = 0;
}
}
for (int p = 3; p <= N; p++) prime[p] += prime[p - 1];
}
void sieve() {
spf[1] = -1;
for (int i = 2; i < N; i++) spf[i] = i;
for (int i = 4; i < N; i += 2) spf[i] = 2;
for (long long int i = 3; i * i < N; i++) {
if (spf[i] == i) {
for (long long int j = i * i; j < N; j += i)
if (spf[j] == j) spf[j] = i;
}
}
}
struct cmp {
bool operator()(const pair<int, int> &x, const pair<int, int> &y) const {
if (x.first != y.first) return x.first < y.first;
return x.second > y.second;
}
};
void gInput(int m, vector<int> adj[]) {
int u, v;
for (int i = 0; i < m; i++) {
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
}
vector<long long int> fac;
void factorial() {
int N = (int)3e5 + 100;
fac.resize(N);
fac[0] = 1;
for (long long int i = 1; i < N; i++) {
fac[i] = ((fac[i - 1] % mod) * i) % mod;
}
}
long long int nCr(long long int n, long long int r) {
long long int x = fac[n];
long long int y = fac[r];
long long int z = fac[n - r];
long long int pr = ((y % mod) * (z % mod)) % mod;
pr = mInv(pr, mod);
long long int ncr = ((x % mod) * (pr % mod)) % mod;
return ncr;
}
long long int dp[100003][2];
int a[100003];
void dfs(int node, vector<int> adj[], int pr) {
if (a[node] == 1) {
dp[node][1] = 1;
dp[node][0] = 0;
} else {
dp[node][1] = 0;
dp[node][0] = 1;
}
for (int j : adj[node]) {
if (j != pr) {
dfs(j, adj, node);
long long int res_w = 0, res_b = 0;
res_w = (res_w + dp[node][0] * dp[j][0]) % mod;
res_b = (res_b + (dp[node][1] * dp[j][0]) % mod +
(dp[node][0] * dp[j][1]) % mod) %
mod;
;
res_w = (res_w + dp[node][0] * dp[j][1]) % mod;
res_b = (res_b + dp[node][1] * dp[j][1]) % mod;
dp[node][0] = res_w;
dp[node][1] = res_b;
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
while (t--) {
int n;
cin >> n;
vector<int> adj[n + 1];
int x;
for (int i = 0; i < n - 1; i++) {
cin >> x;
adj[i + 1].push_back(x);
adj[x].push_back(i + 1);
}
for (int i = 0; i < n; i++) cin >> a[i];
dfs(0, adj, 0);
cout << dp[0][1] << "\n";
}
return 0;
}
|
### Prompt
Develop a solution in cpp to the problem described below:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = (int)200004;
const int dx[] = {-1, 0, 1, 0, -1, -1, 1, 1};
const int dy[] = {0, 1, 0, -1, -1, 1, -1, 1};
char dir[] = {'L', 'D', 'R', 'U'};
const long long int mod = (long long int)1e9 + 7;
const long long int inf = 1e18 + 1000;
int BIT[200005], n;
long long int prime[17], spf[N];
template <typename T>
T lcm(T a, T b) {
return a / gcd(a, b) * b;
}
template <typename T>
T min3(T a, T b, T c) {
return min(a, min(b, c));
}
template <typename T>
T max3(T a, T b, T c) {
return max(a, max(b, c));
}
bool isVowel(char ch) {
ch = toupper(ch);
if (ch == 'A' || ch == 'U' || ch == 'I' || ch == 'O' || ch == 'E')
return true;
return false;
}
long long int mInv(long long int a, long long int m) {
long long int m0 = m;
long long int y = 0, x = 1;
if (m == 1) return 0;
while (a > 1) {
long long int q = a / m;
long long int t = m;
m = a % m, a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0) x += m0;
return x;
}
long long int power(long long int x, long long int y) {
long long int res = 1;
x = x % mod;
while (y > 0) {
if (y & 1) res = (res * x) % mod;
y = y >> 1;
x = (x * x) % mod;
}
return res;
}
long long int gcd(long long int a, long long int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long Extendedgcd(long long a, long long b, long long *x, long long *y) {
if (!a) {
*x = 0;
*y = 1;
return b;
}
long long nx, ny;
long long d = Extendedgcd(b % a, a, &nx, &ny);
long long k = b / a;
*y = nx;
*x = -k * nx + ny;
return d;
}
void update(int x, int delta) {
if (!x) {
BIT[x] += delta;
return;
}
for (; x <= N; x += x & -x) BIT[x] += delta;
}
long long int query(int x) {
long long int sum = BIT[0];
for (; x > 0; x -= x & -x) sum += BIT[x];
return sum;
}
void Sieve() {
for (int i = 1; i <= N; i++) prime[i] = 1;
prime[0] = 0;
prime[1] = 0;
for (long long int p = 2; p * p <= N; p++) {
if (prime[p] == 1) {
for (long long int i = p * p; i <= N; i += p) prime[i] = 0;
}
}
for (int p = 3; p <= N; p++) prime[p] += prime[p - 1];
}
void sieve() {
spf[1] = -1;
for (int i = 2; i < N; i++) spf[i] = i;
for (int i = 4; i < N; i += 2) spf[i] = 2;
for (long long int i = 3; i * i < N; i++) {
if (spf[i] == i) {
for (long long int j = i * i; j < N; j += i)
if (spf[j] == j) spf[j] = i;
}
}
}
struct cmp {
bool operator()(const pair<int, int> &x, const pair<int, int> &y) const {
if (x.first != y.first) return x.first < y.first;
return x.second > y.second;
}
};
void gInput(int m, vector<int> adj[]) {
int u, v;
for (int i = 0; i < m; i++) {
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
}
vector<long long int> fac;
void factorial() {
int N = (int)3e5 + 100;
fac.resize(N);
fac[0] = 1;
for (long long int i = 1; i < N; i++) {
fac[i] = ((fac[i - 1] % mod) * i) % mod;
}
}
long long int nCr(long long int n, long long int r) {
long long int x = fac[n];
long long int y = fac[r];
long long int z = fac[n - r];
long long int pr = ((y % mod) * (z % mod)) % mod;
pr = mInv(pr, mod);
long long int ncr = ((x % mod) * (pr % mod)) % mod;
return ncr;
}
long long int dp[100003][2];
int a[100003];
void dfs(int node, vector<int> adj[], int pr) {
if (a[node] == 1) {
dp[node][1] = 1;
dp[node][0] = 0;
} else {
dp[node][1] = 0;
dp[node][0] = 1;
}
for (int j : adj[node]) {
if (j != pr) {
dfs(j, adj, node);
long long int res_w = 0, res_b = 0;
res_w = (res_w + dp[node][0] * dp[j][0]) % mod;
res_b = (res_b + (dp[node][1] * dp[j][0]) % mod +
(dp[node][0] * dp[j][1]) % mod) %
mod;
;
res_w = (res_w + dp[node][0] * dp[j][1]) % mod;
res_b = (res_b + dp[node][1] * dp[j][1]) % mod;
dp[node][0] = res_w;
dp[node][1] = res_b;
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
while (t--) {
int n;
cin >> n;
vector<int> adj[n + 1];
int x;
for (int i = 0; i < n - 1; i++) {
cin >> x;
adj[i + 1].push_back(x);
adj[x].push_back(i + 1);
}
for (int i = 0; i < n; i++) cin >> a[i];
dfs(0, adj, 0);
cout << dp[0][1] << "\n";
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
char i = getchar();
long long f = 1, res = 0;
while (i < '0' || i > '9') {
if (i == '-') f = -1;
i = getchar();
}
while (i >= '0' && i <= '9') {
res = res * 10 + i - '0';
i = getchar();
}
return res * f;
}
const int N = 1e5 + 50;
const long long mod = 1e9 + 7;
int k, head[N];
long long a[N], dp[N][2];
struct zk {
int v, next;
} ed[N << 1];
inline void adde(int u, int v) {
ed[++k].v = v;
ed[k].next = head[u];
head[u] = k;
}
inline void dfs(int u, int fa) {
for (register int i = head[u]; ~i; i = ed[i].next) {
int v = ed[i].v;
if (v == fa) continue;
dfs(v, u);
dp[u][1] =
(dp[u][1] * (dp[v][0] + dp[v][1]) % mod + dp[u][0] * dp[v][1] % mod) %
mod;
dp[u][0] = (dp[u][0] * (dp[v][0] + dp[v][1])) % mod;
}
}
int main() {
memset(head, -1, sizeof head);
int n = read();
for (register int i = 2; i <= n; ++i) {
int p = read() + 1;
adde(i, p);
adde(p, i);
}
for (register int i = 1; i <= n; ++i) {
a[i] = read();
dp[i][a[i]] = 1;
}
dfs(1, 0);
printf("%lld", dp[1][1]);
}
|
### Prompt
Construct a cpp code solution to the problem outlined:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
char i = getchar();
long long f = 1, res = 0;
while (i < '0' || i > '9') {
if (i == '-') f = -1;
i = getchar();
}
while (i >= '0' && i <= '9') {
res = res * 10 + i - '0';
i = getchar();
}
return res * f;
}
const int N = 1e5 + 50;
const long long mod = 1e9 + 7;
int k, head[N];
long long a[N], dp[N][2];
struct zk {
int v, next;
} ed[N << 1];
inline void adde(int u, int v) {
ed[++k].v = v;
ed[k].next = head[u];
head[u] = k;
}
inline void dfs(int u, int fa) {
for (register int i = head[u]; ~i; i = ed[i].next) {
int v = ed[i].v;
if (v == fa) continue;
dfs(v, u);
dp[u][1] =
(dp[u][1] * (dp[v][0] + dp[v][1]) % mod + dp[u][0] * dp[v][1] % mod) %
mod;
dp[u][0] = (dp[u][0] * (dp[v][0] + dp[v][1])) % mod;
}
}
int main() {
memset(head, -1, sizeof head);
int n = read();
for (register int i = 2; i <= n; ++i) {
int p = read() + 1;
adde(i, p);
adde(p, i);
}
for (register int i = 1; i <= n; ++i) {
a[i] = read();
dp[i][a[i]] = 1;
}
dfs(1, 0);
printf("%lld", dp[1][1]);
}
```
|
#include <bits/stdc++.h>
const long base = 97;
using namespace std;
long long n, a[1000005];
vector<long> G[1000005];
long long F[1000005][2];
void nhap() {
cin >> n;
long x;
for (long i = 1; i < n; i++) {
cin >> x;
G[x].push_back(i);
}
for (long i = 0; i < n; i++) cin >> a[i];
}
void DFS(long v) {
F[v][a[v]] = 1;
for (long i = 0; i < G[v].size(); i++) {
long u = G[v][i];
DFS(u);
F[v][1] =
((F[v][1] * F[u][0]) % 1000000007 + (F[v][0] * F[u][1]) % 1000000007 +
(F[v][1] * F[u][1]) % 1000000007) %
1000000007;
F[v][0] =
((F[v][0] * F[u][1]) % 1000000007 + (F[v][0] * F[u][0]) % 1000000007) %
1000000007;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
nhap();
DFS(0);
cout << F[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>
const long base = 97;
using namespace std;
long long n, a[1000005];
vector<long> G[1000005];
long long F[1000005][2];
void nhap() {
cin >> n;
long x;
for (long i = 1; i < n; i++) {
cin >> x;
G[x].push_back(i);
}
for (long i = 0; i < n; i++) cin >> a[i];
}
void DFS(long v) {
F[v][a[v]] = 1;
for (long i = 0; i < G[v].size(); i++) {
long u = G[v][i];
DFS(u);
F[v][1] =
((F[v][1] * F[u][0]) % 1000000007 + (F[v][0] * F[u][1]) % 1000000007 +
(F[v][1] * F[u][1]) % 1000000007) %
1000000007;
F[v][0] =
((F[v][0] * F[u][1]) % 1000000007 + (F[v][0] * F[u][0]) % 1000000007) %
1000000007;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
nhap();
DFS(0);
cout << F[0][1] << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
struct node {
long long end, next;
} g[100100 * 2];
long long fa[100100], tot, n, i, ok[100100], x, f[100100][2];
long long ksm(long long a, long long b) {
long long tem = 1ll;
while (b) {
if (b & 1) tem = tem * a % 1000000007;
a = a * a % 1000000007;
b >>= 1;
}
return tem;
}
void add(long long a, long long b) {
g[++tot].end = b;
g[tot].next = fa[a];
fa[a] = tot;
}
void dfs(long long w, long long pre) {
long long i, tem, uk, s;
for (i = fa[w]; i; i = g[i].next)
if (pre != g[i].end) dfs(g[i].end, w);
if (ok[w]) {
tem = 1ll;
for (i = fa[w]; i; i = g[i].next)
if (pre != g[i].end) tem = tem * f[g[i].end][0] % 1000000007;
f[w][0] = f[w][1] = tem;
} else {
tem = 0;
uk = 1;
for (i = fa[w]; i; i = g[i].next)
if (pre != g[i].end) uk = uk * f[g[i].end][0] % 1000000007;
for (i = fa[w]; i; i = g[i].next)
if (pre != g[i].end) {
s = uk * ksm(f[g[i].end][0], 1000000007 - 2) % 1000000007 *
f[g[i].end][1] % 1000000007;
tem = (tem + s) % 1000000007;
}
f[w][1] = tem;
f[w][0] = (f[w][1] + uk) % 1000000007;
}
}
int main() {
scanf("%I64d", &n);
for (i = 1; i < n; i++)
scanf("%I64d", &x), add(x + 1, i + 1), add(i + 1, x + 1);
for (i = 1; i <= n; i++) scanf("%I64d", &ok[i]);
dfs(1, 0);
printf("%I64d\n", f[1][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;
struct node {
long long end, next;
} g[100100 * 2];
long long fa[100100], tot, n, i, ok[100100], x, f[100100][2];
long long ksm(long long a, long long b) {
long long tem = 1ll;
while (b) {
if (b & 1) tem = tem * a % 1000000007;
a = a * a % 1000000007;
b >>= 1;
}
return tem;
}
void add(long long a, long long b) {
g[++tot].end = b;
g[tot].next = fa[a];
fa[a] = tot;
}
void dfs(long long w, long long pre) {
long long i, tem, uk, s;
for (i = fa[w]; i; i = g[i].next)
if (pre != g[i].end) dfs(g[i].end, w);
if (ok[w]) {
tem = 1ll;
for (i = fa[w]; i; i = g[i].next)
if (pre != g[i].end) tem = tem * f[g[i].end][0] % 1000000007;
f[w][0] = f[w][1] = tem;
} else {
tem = 0;
uk = 1;
for (i = fa[w]; i; i = g[i].next)
if (pre != g[i].end) uk = uk * f[g[i].end][0] % 1000000007;
for (i = fa[w]; i; i = g[i].next)
if (pre != g[i].end) {
s = uk * ksm(f[g[i].end][0], 1000000007 - 2) % 1000000007 *
f[g[i].end][1] % 1000000007;
tem = (tem + s) % 1000000007;
}
f[w][1] = tem;
f[w][0] = (f[w][1] + uk) % 1000000007;
}
}
int main() {
scanf("%I64d", &n);
for (i = 1; i < n; i++)
scanf("%I64d", &x), add(x + 1, i + 1), add(i + 1, x + 1);
for (i = 1; i <= n; i++) scanf("%I64d", &ok[i]);
dfs(1, 0);
printf("%I64d\n", f[1][1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const unsigned long long MOD = 1e9 + 7;
const int MAXN = 1e5 + 5;
int n;
int *color;
vector<int> *child;
unsigned long long dp[MAXN][2];
int main() {
scanf("%d", &n);
child = new vector<int>[n];
for (int i = 0; i < n - 1; ++i) {
int x;
scanf("%d", &x);
child[x].push_back(i + 1);
}
color = new int[n];
for (int i = 0; i < n; ++i) {
scanf("%d", color + i);
}
for (int i = n - 1; i >= 0; --i) {
if (color[i] == 0) {
dp[i][0] = 1;
dp[i][1] = 0;
for (int k = 0; k < child[i].size(); ++k) {
int u = child[i][k];
dp[i][1] =
(dp[i][0] * dp[u][1] + dp[i][1] * (dp[u][0] + dp[u][1])) % MOD;
dp[i][0] = (dp[i][0] * (dp[u][0] + dp[u][1])) % MOD;
}
} else {
dp[i][0] = 0;
dp[i][1] = 1;
for (int k = 0; k < child[i].size(); ++k) {
int u = child[i][k];
dp[i][1] = (dp[i][1] * (dp[u][0] + dp[u][1])) % MOD;
}
}
}
cout << dp[0][1] << endl;
delete[] color;
delete[] child;
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 unsigned long long MOD = 1e9 + 7;
const int MAXN = 1e5 + 5;
int n;
int *color;
vector<int> *child;
unsigned long long dp[MAXN][2];
int main() {
scanf("%d", &n);
child = new vector<int>[n];
for (int i = 0; i < n - 1; ++i) {
int x;
scanf("%d", &x);
child[x].push_back(i + 1);
}
color = new int[n];
for (int i = 0; i < n; ++i) {
scanf("%d", color + i);
}
for (int i = n - 1; i >= 0; --i) {
if (color[i] == 0) {
dp[i][0] = 1;
dp[i][1] = 0;
for (int k = 0; k < child[i].size(); ++k) {
int u = child[i][k];
dp[i][1] =
(dp[i][0] * dp[u][1] + dp[i][1] * (dp[u][0] + dp[u][1])) % MOD;
dp[i][0] = (dp[i][0] * (dp[u][0] + dp[u][1])) % MOD;
}
} else {
dp[i][0] = 0;
dp[i][1] = 1;
for (int k = 0; k < child[i].size(); ++k) {
int u = child[i][k];
dp[i][1] = (dp[i][1] * (dp[u][0] + dp[u][1])) % MOD;
}
}
}
cout << dp[0][1] << endl;
delete[] color;
delete[] child;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<long long int> v[100005];
long long int c[100005];
long long int ans[100005][2];
void dfs(long long int s, long long int pa = -1) {
ans[s][0] = 1;
ans[s][1] = 0;
for (auto i : v[s]) {
if (i == pa) continue;
dfs(i, s);
ans[s][1] *= ans[i][0];
ans[s][1] %= 1000000007;
ans[s][1] += (ans[s][0] * ans[i][1]) % 1000000007;
ans[s][1] %= 1000000007;
ans[s][0] *= ans[i][0];
ans[s][0] %= 1000000007;
}
if (c[s] == 1)
ans[s][1] = ans[s][0];
else {
ans[s][0] += ans[s][1];
ans[s][0] %= 1000000007;
}
}
int main() {
long long int i, j, k, n, m, t;
cin >> n;
for (i = 1; i < n; i++) {
cin >> k;
v[i].push_back(k);
v[k].push_back(i);
}
for (i = 0; i < n; i++) {
cin >> c[i];
}
dfs(0);
cout << ans[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<long long int> v[100005];
long long int c[100005];
long long int ans[100005][2];
void dfs(long long int s, long long int pa = -1) {
ans[s][0] = 1;
ans[s][1] = 0;
for (auto i : v[s]) {
if (i == pa) continue;
dfs(i, s);
ans[s][1] *= ans[i][0];
ans[s][1] %= 1000000007;
ans[s][1] += (ans[s][0] * ans[i][1]) % 1000000007;
ans[s][1] %= 1000000007;
ans[s][0] *= ans[i][0];
ans[s][0] %= 1000000007;
}
if (c[s] == 1)
ans[s][1] = ans[s][0];
else {
ans[s][0] += ans[s][1];
ans[s][0] %= 1000000007;
}
}
int main() {
long long int i, j, k, n, m, t;
cin >> n;
for (i = 1; i < n; i++) {
cin >> k;
v[i].push_back(k);
v[k].push_back(i);
}
for (i = 0; i < n; i++) {
cin >> c[i];
}
dfs(0);
cout << ans[0][1];
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int p[100010];
long long d[100010][2];
int main(void) {
int n, xi;
scanf("%d", &n);
for (int i = 0; i < n - 1; ++i) {
scanf("%d", &p[i + 1]);
}
for (int i = 0; i < n; ++i) {
scanf("%d", &xi);
d[i][xi] = 1;
}
for (int i = n - 1; i > 0; --i) {
d[p[i]][1] =
(d[p[i]][1] * (d[i][0] + d[i][1]) + d[p[i]][0] * d[i][1]) % 1000000007;
d[p[i]][0] = (d[p[i]][0] * (d[i][0] + d[i][1])) % 1000000007;
}
printf("%lld\n", d[0][1]);
}
|
### Prompt
Develop a solution in Cpp to the problem described below:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int p[100010];
long long d[100010][2];
int main(void) {
int n, xi;
scanf("%d", &n);
for (int i = 0; i < n - 1; ++i) {
scanf("%d", &p[i + 1]);
}
for (int i = 0; i < n; ++i) {
scanf("%d", &xi);
d[i][xi] = 1;
}
for (int i = n - 1; i > 0; --i) {
d[p[i]][1] =
(d[p[i]][1] * (d[i][0] + d[i][1]) + d[p[i]][0] * d[i][1]) % 1000000007;
d[p[i]][0] = (d[p[i]][0] * (d[i][0] + d[i][1])) % 1000000007;
}
printf("%lld\n", d[0][1]);
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[100005];
int color[100005];
int n;
int dp[100005][2];
void go(int u, int p) {
for (int i = 0; i < adj[u].size(); ++i) {
int v = adj[u][i];
if (v == p) continue;
go(v, u);
}
if (color[u])
dp[u][1] = 1;
else
dp[u][0] = 1;
for (int i = 0; i < adj[u].size(); ++i) {
int v = adj[u][i];
if (v == p) continue;
int nxt0 = 0, nxt1 = 0;
nxt0 = (long long)dp[u][0] * (dp[v][0] + dp[v][1]) % 1000000007;
nxt1 = ((long long)dp[u][0] * dp[v][1] +
(long long)dp[u][1] * (dp[v][0] + dp[v][1]) % 1000000007) %
1000000007;
dp[u][0] = nxt0;
dp[u][1] = nxt1;
}
}
int main() {
scanf("%d", &n);
for (int u = 1; u < n; ++u) {
int v;
scanf("%d", &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 0; i < n; ++i) scanf("%d", color + i);
go(0, -1);
printf("%d\n", dp[0][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>
using namespace std;
vector<int> adj[100005];
int color[100005];
int n;
int dp[100005][2];
void go(int u, int p) {
for (int i = 0; i < adj[u].size(); ++i) {
int v = adj[u][i];
if (v == p) continue;
go(v, u);
}
if (color[u])
dp[u][1] = 1;
else
dp[u][0] = 1;
for (int i = 0; i < adj[u].size(); ++i) {
int v = adj[u][i];
if (v == p) continue;
int nxt0 = 0, nxt1 = 0;
nxt0 = (long long)dp[u][0] * (dp[v][0] + dp[v][1]) % 1000000007;
nxt1 = ((long long)dp[u][0] * dp[v][1] +
(long long)dp[u][1] * (dp[v][0] + dp[v][1]) % 1000000007) %
1000000007;
dp[u][0] = nxt0;
dp[u][1] = nxt1;
}
}
int main() {
scanf("%d", &n);
for (int u = 1; u < n; ++u) {
int v;
scanf("%d", &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 0; i < n; ++i) scanf("%d", color + i);
go(0, -1);
printf("%d\n", dp[0][1]);
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 111111;
const int MOD = 1e9 + 7;
int n;
int a[maxn];
struct Edge {
int next, v;
} e[2 * maxn];
int en = 0;
int head[maxn];
void add(int x, int y) {
e[en].v = y;
e[en].next = head[x];
head[x] = en++;
}
bool u[maxn];
long long f[maxn][2];
void dfs(int x) {
int i;
u[x] = 1;
f[x][0] = 1;
f[x][1] = 0;
for (i = head[x]; i != -1; i = e[i].next) {
if (!u[e[i].v]) {
dfs(e[i].v);
f[x][1] = (f[x][1] * f[e[i].v][0] + f[x][0] * f[e[i].v][1]) % MOD;
f[x][0] = f[x][0] * f[e[i].v][0] % MOD;
}
}
u[x] = 0;
if (a[x] == 0)
f[x][0] = (f[x][0] + f[x][1]) % MOD;
else
f[x][1] = f[x][0];
}
long long farm() {
if (n == 1) return 1;
memset(u, 0, sizeof(u));
dfs(0);
return f[0][1];
}
int main() {
int i;
int x;
scanf("%d", &n);
memset(head, -1, sizeof(head));
en = 0;
for (i = 0; i < (n - 1); i++) {
scanf("%d", &x);
add(i + 1, x);
add(x, i + 1);
}
for (i = 0; i < n; i++) scanf("%d", &a[i]);
printf("%I64d", farm());
return 0;
}
|
### Prompt
Please create a solution in CPP to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 111111;
const int MOD = 1e9 + 7;
int n;
int a[maxn];
struct Edge {
int next, v;
} e[2 * maxn];
int en = 0;
int head[maxn];
void add(int x, int y) {
e[en].v = y;
e[en].next = head[x];
head[x] = en++;
}
bool u[maxn];
long long f[maxn][2];
void dfs(int x) {
int i;
u[x] = 1;
f[x][0] = 1;
f[x][1] = 0;
for (i = head[x]; i != -1; i = e[i].next) {
if (!u[e[i].v]) {
dfs(e[i].v);
f[x][1] = (f[x][1] * f[e[i].v][0] + f[x][0] * f[e[i].v][1]) % MOD;
f[x][0] = f[x][0] * f[e[i].v][0] % MOD;
}
}
u[x] = 0;
if (a[x] == 0)
f[x][0] = (f[x][0] + f[x][1]) % MOD;
else
f[x][1] = f[x][0];
}
long long farm() {
if (n == 1) return 1;
memset(u, 0, sizeof(u));
dfs(0);
return f[0][1];
}
int main() {
int i;
int x;
scanf("%d", &n);
memset(head, -1, sizeof(head));
en = 0;
for (i = 0; i < (n - 1); i++) {
scanf("%d", &x);
add(i + 1, x);
add(x, i + 1);
}
for (i = 0; i < n; i++) scanf("%d", &a[i]);
printf("%I64d", farm());
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
const int MOD = 1000000007;
int N;
int a[MAXN];
vector<int> ch[MAXN];
int dp[MAXN][2];
inline int add(int x, int y) {
x += y;
if (x >= MOD) x -= MOD;
return x;
}
inline int mul(int x, int y) { return (long long)x * y % MOD; }
void load() {
scanf("%d", &N);
for (int i = 1; i < N; i++) {
int x;
scanf("%d", &x);
ch[x].push_back(i);
}
for (int i = 0; i < N; i++) scanf("%d", a + i);
}
int dfs(int x) {
vector<int> tmp;
for (int i = 0; i < ch[x].size(); i++) tmp.push_back(dfs(ch[x][i]));
int prod = 1, sz = tmp.size();
for (int i = 0; i < sz; i++) prod = mul(prod, tmp[i]);
if (a[x])
dp[x][1] = prod;
else {
dp[x][0] = prod;
vector<int> suff(sz + 1);
suff[sz] = 1;
for (int i = sz - 1; i >= 0; i--) suff[i] = mul(suff[i + 1], tmp[i]);
prod = 1;
for (int i = 0; i < sz; i++) {
dp[x][1] = add(dp[x][1], mul(mul(prod, suff[i + 1]), dp[ch[x][i]][1]));
prod = mul(prod, tmp[i]);
}
}
return add(dp[x][0], dp[x][1]);
}
int solve() {
dfs(0);
return dp[0][1];
}
int main() {
load();
printf("%d\n", solve());
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 = 100005;
const int MOD = 1000000007;
int N;
int a[MAXN];
vector<int> ch[MAXN];
int dp[MAXN][2];
inline int add(int x, int y) {
x += y;
if (x >= MOD) x -= MOD;
return x;
}
inline int mul(int x, int y) { return (long long)x * y % MOD; }
void load() {
scanf("%d", &N);
for (int i = 1; i < N; i++) {
int x;
scanf("%d", &x);
ch[x].push_back(i);
}
for (int i = 0; i < N; i++) scanf("%d", a + i);
}
int dfs(int x) {
vector<int> tmp;
for (int i = 0; i < ch[x].size(); i++) tmp.push_back(dfs(ch[x][i]));
int prod = 1, sz = tmp.size();
for (int i = 0; i < sz; i++) prod = mul(prod, tmp[i]);
if (a[x])
dp[x][1] = prod;
else {
dp[x][0] = prod;
vector<int> suff(sz + 1);
suff[sz] = 1;
for (int i = sz - 1; i >= 0; i--) suff[i] = mul(suff[i + 1], tmp[i]);
prod = 1;
for (int i = 0; i < sz; i++) {
dp[x][1] = add(dp[x][1], mul(mul(prod, suff[i + 1]), dp[ch[x][i]][1]));
prod = mul(prod, tmp[i]);
}
}
return add(dp[x][0], dp[x][1]);
}
int solve() {
dfs(0);
return dp[0][1];
}
int main() {
load();
printf("%d\n", solve());
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
vector<int> G[100001];
long long dp[100001][2], p[100001];
void dfs(int v, int pre) {
dp[v][0] = 1 - p[v];
dp[v][1] = p[v];
for (int i = 0; i < G[v].size(); i++) {
int nxt = G[v][i];
if (nxt == pre) continue;
dfs(nxt, v);
int tdp[2];
tdp[0] = tdp[1] = 0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
if (i == 0) {
tdp[0] += (dp[v][i] * dp[nxt][j]) % MOD;
tdp[0] %= MOD;
}
if (i == 1 || j == 1) {
tdp[1] += (dp[v][i] * dp[nxt][j]) % MOD;
tdp[1] %= MOD;
}
}
}
dp[v][1] = tdp[1];
dp[v][0] = tdp[0];
}
}
int main() {
int n, t;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
scanf("%d", &t);
G[t].push_back(i);
}
for (int i = 0; i < n; i++) scanf("%d", &p[i]);
dfs(0, -1);
printf("%lld\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 long long MOD = 1e9 + 7;
vector<int> G[100001];
long long dp[100001][2], p[100001];
void dfs(int v, int pre) {
dp[v][0] = 1 - p[v];
dp[v][1] = p[v];
for (int i = 0; i < G[v].size(); i++) {
int nxt = G[v][i];
if (nxt == pre) continue;
dfs(nxt, v);
int tdp[2];
tdp[0] = tdp[1] = 0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
if (i == 0) {
tdp[0] += (dp[v][i] * dp[nxt][j]) % MOD;
tdp[0] %= MOD;
}
if (i == 1 || j == 1) {
tdp[1] += (dp[v][i] * dp[nxt][j]) % MOD;
tdp[1] %= MOD;
}
}
}
dp[v][1] = tdp[1];
dp[v][0] = tdp[0];
}
}
int main() {
int n, t;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
scanf("%d", &t);
G[t].push_back(i);
}
for (int i = 0; i < n; i++) scanf("%d", &p[i]);
dfs(0, -1);
printf("%lld\n", dp[0][1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
inline void OPEN(const string &s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
int n, p[100100], c[100100];
vector<int> child[100100];
long long power(long long a, long long b) {
if (b == 0) return 1;
long long temp = power(a, b / 2LL);
temp *= temp;
temp %= 1000000007LL;
if (b & 1) {
temp *= a;
temp %= 1000000007LL;
}
return temp;
}
long long inv(long long x) { return power(x, 1000000007LL - 2LL); }
pair<long long, long long> dfs(int v) {
vector<pair<long long, long long> > t;
int czero = 0;
for (__typeof((child[v]).begin()) it = (child[v]).begin();
it != (child[v]).end(); it++) {
pair<long long, long long> temp = dfs(*it);
if (temp.first != 0 || temp.second != 0) {
t.push_back(temp);
if ((temp.first + temp.second) % 1000000007LL == 0) czero++;
}
}
if (t.empty()) {
if (c[v] == 1) {
return make_pair(0, 1);
} else {
return make_pair(0, 0);
}
}
long long x = 1;
long long xzero = 1;
for (__typeof((t).begin()) it = (t).begin(); it != (t).end(); it++) {
x *= (it->first + it->second);
x %= 1000000007LL;
if ((it->first + it->second) % 1000000007LL != 0) {
xzero *= (it->first + it->second);
xzero %= 1000000007LL;
}
}
long long y = 0;
for (__typeof((t).begin()) it = (t).begin(); it != (t).end(); it++) {
long long ty = it->second;
if (czero == 0) {
ty *= x;
ty %= 1000000007LL;
ty *= inv(it->first + it->second);
ty %= 1000000007LL;
} else if (czero == 1) {
if ((it->first + it->second) % 1000000007LL == 0) {
ty *= xzero;
ty %= 1000000007LL;
} else {
ty = 0;
}
} else {
ty = 0;
}
y += ty;
y %= 1000000007LL;
}
if (c[v] == 1) {
y = x;
x = 0;
}
return make_pair(x, y);
}
int main() {
scanf("%d", &n);
for (int i = 0; i < (n - 1); ++i) {
scanf("%d", &p[i + 1]);
child[p[i + 1]].push_back(i + 1);
}
for (int i = 0; i < (n); ++i) {
scanf("%d", &c[i]);
}
pair<long long, long long> ans = dfs(0);
cout << ans.second << 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;
inline void OPEN(const string &s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
int n, p[100100], c[100100];
vector<int> child[100100];
long long power(long long a, long long b) {
if (b == 0) return 1;
long long temp = power(a, b / 2LL);
temp *= temp;
temp %= 1000000007LL;
if (b & 1) {
temp *= a;
temp %= 1000000007LL;
}
return temp;
}
long long inv(long long x) { return power(x, 1000000007LL - 2LL); }
pair<long long, long long> dfs(int v) {
vector<pair<long long, long long> > t;
int czero = 0;
for (__typeof((child[v]).begin()) it = (child[v]).begin();
it != (child[v]).end(); it++) {
pair<long long, long long> temp = dfs(*it);
if (temp.first != 0 || temp.second != 0) {
t.push_back(temp);
if ((temp.first + temp.second) % 1000000007LL == 0) czero++;
}
}
if (t.empty()) {
if (c[v] == 1) {
return make_pair(0, 1);
} else {
return make_pair(0, 0);
}
}
long long x = 1;
long long xzero = 1;
for (__typeof((t).begin()) it = (t).begin(); it != (t).end(); it++) {
x *= (it->first + it->second);
x %= 1000000007LL;
if ((it->first + it->second) % 1000000007LL != 0) {
xzero *= (it->first + it->second);
xzero %= 1000000007LL;
}
}
long long y = 0;
for (__typeof((t).begin()) it = (t).begin(); it != (t).end(); it++) {
long long ty = it->second;
if (czero == 0) {
ty *= x;
ty %= 1000000007LL;
ty *= inv(it->first + it->second);
ty %= 1000000007LL;
} else if (czero == 1) {
if ((it->first + it->second) % 1000000007LL == 0) {
ty *= xzero;
ty %= 1000000007LL;
} else {
ty = 0;
}
} else {
ty = 0;
}
y += ty;
y %= 1000000007LL;
}
if (c[v] == 1) {
y = x;
x = 0;
}
return make_pair(x, y);
}
int main() {
scanf("%d", &n);
for (int i = 0; i < (n - 1); ++i) {
scanf("%d", &p[i + 1]);
child[p[i + 1]].push_back(i + 1);
}
for (int i = 0; i < (n); ++i) {
scanf("%d", &c[i]);
}
pair<long long, long long> ans = dfs(0);
cout << ans.second << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200020;
vector<int> g[N];
int cor[N];
int mod = 1000 * 1000 * 1000 + 7;
long long dp[N][2][2];
long long fpow(long long b, long long e) {
if (!e) return 1;
long long r = fpow(b, e / 2);
r = (r * r) % mod;
return e % 2 ? (r * b) % mod : r;
}
long long solve(int u, int has_black, int need_black) {
long long &ret = dp[u][has_black][need_black];
if (ret != -1) return ret;
if (cor[u] and has_black) {
return ret = solve(u, 0, 1);
}
if (g[u].empty()) {
if (need_black and !cor[u]) return ret = 0;
return ret = 1;
}
ret = 0;
if (has_black) ret = solve(u, 0, 1);
if (cor[u] or has_black) {
long long aux = 1;
for (auto v : g[u]) aux = (aux * solve(v, 1, 0)) % mod;
ret = (ret + aux) % mod;
} else {
long long total = 1;
for (auto v : g[u]) total = (total * solve(v, 1, 0)) % mod;
for (auto v : g[u]) {
long long aux = (total * fpow(solve(v, 1, 0), mod - 2)) % mod;
aux = (aux * solve(v, 0, 1)) % mod;
ret = (ret + aux) % mod;
}
}
return ret;
}
int main(void) {
memset(dp, -1, sizeof dp);
int n;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int p;
scanf("%d", &p);
g[p].push_back(i);
}
for (int i = 0; i < n; i++) scanf("%d", cor + i);
cout << solve(0, 0, 1) << endl;
return 0;
}
|
### Prompt
Develop a solution in cpp to the problem described below:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 200020;
vector<int> g[N];
int cor[N];
int mod = 1000 * 1000 * 1000 + 7;
long long dp[N][2][2];
long long fpow(long long b, long long e) {
if (!e) return 1;
long long r = fpow(b, e / 2);
r = (r * r) % mod;
return e % 2 ? (r * b) % mod : r;
}
long long solve(int u, int has_black, int need_black) {
long long &ret = dp[u][has_black][need_black];
if (ret != -1) return ret;
if (cor[u] and has_black) {
return ret = solve(u, 0, 1);
}
if (g[u].empty()) {
if (need_black and !cor[u]) return ret = 0;
return ret = 1;
}
ret = 0;
if (has_black) ret = solve(u, 0, 1);
if (cor[u] or has_black) {
long long aux = 1;
for (auto v : g[u]) aux = (aux * solve(v, 1, 0)) % mod;
ret = (ret + aux) % mod;
} else {
long long total = 1;
for (auto v : g[u]) total = (total * solve(v, 1, 0)) % mod;
for (auto v : g[u]) {
long long aux = (total * fpow(solve(v, 1, 0), mod - 2)) % mod;
aux = (aux * solve(v, 0, 1)) % mod;
ret = (ret + aux) % mod;
}
}
return ret;
}
int main(void) {
memset(dp, -1, sizeof dp);
int n;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int p;
scanf("%d", &p);
g[p].push_back(i);
}
for (int i = 0; i < n; i++) scanf("%d", cor + i);
cout << solve(0, 0, 1) << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long modulo = 1e9 + 7;
const int MAXN = 200000;
int colours[MAXN + 5];
vector<int> children[MAXN + 5];
long long combinations[MAXN + 5][2];
void dfs(int node) {
if (colours[node] == 1) {
combinations[node][0] = 0;
combinations[node][1] = 1;
} else {
combinations[node][0] = 1;
combinations[node][1] = 0;
}
for (auto child : children[node]) {
dfs(child);
long long a, b, c;
a = (combinations[child][0] * combinations[node][1]) % modulo;
b = (combinations[child][1] * combinations[node][0]) % modulo;
c = (combinations[child][1] * combinations[node][1]) % modulo;
combinations[node][1] = (a + b + c) % modulo;
long long d, e;
d = (combinations[child][0] * combinations[node][0]) % modulo;
e = (combinations[child][1] * combinations[node][0]) % modulo;
combinations[node][0] = (d + e) % modulo;
}
}
int main() {
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int parent;
cin >> parent;
children[parent].emplace_back(i);
}
for (int i = 0; i < n; i++) {
cin >> colours[i];
}
dfs(0);
cout << combinations[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;
const long long modulo = 1e9 + 7;
const int MAXN = 200000;
int colours[MAXN + 5];
vector<int> children[MAXN + 5];
long long combinations[MAXN + 5][2];
void dfs(int node) {
if (colours[node] == 1) {
combinations[node][0] = 0;
combinations[node][1] = 1;
} else {
combinations[node][0] = 1;
combinations[node][1] = 0;
}
for (auto child : children[node]) {
dfs(child);
long long a, b, c;
a = (combinations[child][0] * combinations[node][1]) % modulo;
b = (combinations[child][1] * combinations[node][0]) % modulo;
c = (combinations[child][1] * combinations[node][1]) % modulo;
combinations[node][1] = (a + b + c) % modulo;
long long d, e;
d = (combinations[child][0] * combinations[node][0]) % modulo;
e = (combinations[child][1] * combinations[node][0]) % modulo;
combinations[node][0] = (d + e) % modulo;
}
}
int main() {
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int parent;
cin >> parent;
children[parent].emplace_back(i);
}
for (int i = 0; i < n; i++) {
cin >> colours[i];
}
dfs(0);
cout << combinations[0][1] << '\n';
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
const int MOD = 1e9 + 7;
int dp[N][2];
int col[N];
vector<int> child[N];
long long bpow(long long a, long long b) {
long long res = 1;
while (b) {
if (b & 1) res = res * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return res;
}
void dfs(int v) {
long long mul = 1;
for (auto u : child[v]) {
dfs(u);
mul = mul * (dp[u][1] + dp[u][0]) % MOD;
}
if (col[v])
dp[v][1] = mul;
else {
dp[v][0] = mul;
dp[v][1] = 0;
for (auto u : child[v])
dp[v][1] = (dp[v][1] + mul * bpow(dp[u][1] + dp[u][0], MOD - 2) % MOD *
dp[u][1] % MOD) %
MOD;
}
}
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int p;
cin >> p;
child[p].push_back(i);
}
for (int i = 0; i < n; i++) cin >> col[i];
dfs(0);
cout << dp[0][1] << "\n";
return 0;
}
|
### Prompt
Your task is to create a CPP solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
const int MOD = 1e9 + 7;
int dp[N][2];
int col[N];
vector<int> child[N];
long long bpow(long long a, long long b) {
long long res = 1;
while (b) {
if (b & 1) res = res * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return res;
}
void dfs(int v) {
long long mul = 1;
for (auto u : child[v]) {
dfs(u);
mul = mul * (dp[u][1] + dp[u][0]) % MOD;
}
if (col[v])
dp[v][1] = mul;
else {
dp[v][0] = mul;
dp[v][1] = 0;
for (auto u : child[v])
dp[v][1] = (dp[v][1] + mul * bpow(dp[u][1] + dp[u][0], MOD - 2) % MOD *
dp[u][1] % MOD) %
MOD;
}
}
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int p;
cin >> p;
child[p].push_back(i);
}
for (int i = 0; i < n; i++) cin >> col[i];
dfs(0);
cout << dp[0][1] << "\n";
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
using lli = long long int;
using pii = pair<int, int>;
using vi = vector<int>;
using vb = vector<bool>;
using vvi = vector<vector<int>>;
using vlli = vector<long long int>;
using vpii = vector<pair<int, int>>;
lli n, x, dp[200005][2], f[2];
vlli col;
vvi adj;
void dfs(int u, int prev) {
dp[u][1] = col[u];
dp[u][0] = 1 - col[u];
for (auto v : adj[u])
if (v != prev) {
dfs(v, u);
f[0] = dp[u][0];
f[1] = dp[u][1];
dp[u][0] = dp[u][1] = 0;
dp[u][1] = (dp[u][1] + f[1] * dp[v][0] + f[0] * dp[v][1]) % 1000000007;
dp[u][0] = (dp[u][0] + f[0] * dp[v][0] + f[0] * dp[v][1]) % 1000000007;
dp[u][1] = (dp[u][1] + f[1] * dp[v][1]) % 1000000007;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
adj.resize(n + 1);
col.resize(n + 1);
for (int i = 2; i <= n; i++) {
cin >> x;
adj[i].push_back(x + 1);
adj[x + 1].push_back(i);
}
for (int i = 1; i <= n; i++) cin >> col[i];
dfs(1, -1);
cout << dp[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;
using lli = long long int;
using pii = pair<int, int>;
using vi = vector<int>;
using vb = vector<bool>;
using vvi = vector<vector<int>>;
using vlli = vector<long long int>;
using vpii = vector<pair<int, int>>;
lli n, x, dp[200005][2], f[2];
vlli col;
vvi adj;
void dfs(int u, int prev) {
dp[u][1] = col[u];
dp[u][0] = 1 - col[u];
for (auto v : adj[u])
if (v != prev) {
dfs(v, u);
f[0] = dp[u][0];
f[1] = dp[u][1];
dp[u][0] = dp[u][1] = 0;
dp[u][1] = (dp[u][1] + f[1] * dp[v][0] + f[0] * dp[v][1]) % 1000000007;
dp[u][0] = (dp[u][0] + f[0] * dp[v][0] + f[0] * dp[v][1]) % 1000000007;
dp[u][1] = (dp[u][1] + f[1] * dp[v][1]) % 1000000007;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
adj.resize(n + 1);
col.resize(n + 1);
for (int i = 2; i <= n; i++) {
cin >> x;
adj[i].push_back(x + 1);
adj[x + 1].push_back(i);
}
for (int i = 1; i <= n; i++) cin >> col[i];
dfs(1, -1);
cout << dp[1][1];
}
```
|
#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)];
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]]) {
if (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;
ret = (ret + go(n, p + 1, 1) * go(adj[n][p], 0, 1)) % 1000000007;
} else {
ret = (ret + go(n, p + 1, 0) * go(adj[n][p], 0, 0)) % 1000000007;
ret = (ret + go(n, p + 1, 0) * go(adj[n][p], 0, 1)) % 1000000007;
}
} else {
if (b) {
ret = (ret + go(n, p + 1, 0) * go(adj[n][p], 0, 0)) % 1000000007;
ret = (ret + go(n, p + 1, 1) * go(adj[n][p], 0, 0)) % 1000000007;
} else {
ret = (ret + go(n, p + 1, 0) * go(adj[n][p], 0, 0)) % 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;
adj[x + 1].push_back(i + 2);
}
for (int i = 1; i <= n; i++) {
cin >> col[i];
}
adj[0].push_back(1);
memset(dp, -1, sizeof dp);
cout << go(0, 0, 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;
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)];
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]]) {
if (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;
ret = (ret + go(n, p + 1, 1) * go(adj[n][p], 0, 1)) % 1000000007;
} else {
ret = (ret + go(n, p + 1, 0) * go(adj[n][p], 0, 0)) % 1000000007;
ret = (ret + go(n, p + 1, 0) * go(adj[n][p], 0, 1)) % 1000000007;
}
} else {
if (b) {
ret = (ret + go(n, p + 1, 0) * go(adj[n][p], 0, 0)) % 1000000007;
ret = (ret + go(n, p + 1, 1) * go(adj[n][p], 0, 0)) % 1000000007;
} else {
ret = (ret + go(n, p + 1, 0) * go(adj[n][p], 0, 0)) % 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;
adj[x + 1].push_back(i + 2);
}
for (int i = 1; i <= n; i++) {
cin >> col[i];
}
adj[0].push_back(1);
memset(dp, -1, sizeof dp);
cout << go(0, 0, 1) % 1000000007 << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[200001];
vector<int> x(200001);
vector<vector<long long> > dp(200001, vector<long long>(2));
void dfs(int node, int parent) {
int i;
dp[node][0] = 1;
dp[node][1] = 0;
for (i = 0; i < adj[node].size(); i++) {
if (adj[node][i] == parent) continue;
dfs(adj[node][i], node);
dp[node][1] = (dp[node][1] * dp[adj[node][i]][0]) % 1000000007;
dp[node][1] =
(dp[node][1] + (dp[node][0] * dp[adj[node][i]][1]) % 1000000007) %
1000000007;
dp[node][0] = (dp[node][0] * dp[adj[node][i]][0]) % 1000000007;
}
if (x[node] == 1)
dp[node][1] = dp[node][0];
else
dp[node][0] = (dp[node][0] + dp[node][1]) % 1000000007;
}
int main() {
long long n, i, u, v;
cin >> n;
for (i = 1; i <= n - 1; i++) {
cin >> u;
adj[i].push_back(u);
adj[u].push_back(i);
}
for (i = 0; i < n; i++) {
cin >> u;
x[i] = u;
}
dfs(0, -1);
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;
vector<int> adj[200001];
vector<int> x(200001);
vector<vector<long long> > dp(200001, vector<long long>(2));
void dfs(int node, int parent) {
int i;
dp[node][0] = 1;
dp[node][1] = 0;
for (i = 0; i < adj[node].size(); i++) {
if (adj[node][i] == parent) continue;
dfs(adj[node][i], node);
dp[node][1] = (dp[node][1] * dp[adj[node][i]][0]) % 1000000007;
dp[node][1] =
(dp[node][1] + (dp[node][0] * dp[adj[node][i]][1]) % 1000000007) %
1000000007;
dp[node][0] = (dp[node][0] * dp[adj[node][i]][0]) % 1000000007;
}
if (x[node] == 1)
dp[node][1] = dp[node][0];
else
dp[node][0] = (dp[node][0] + dp[node][1]) % 1000000007;
}
int main() {
long long n, i, u, v;
cin >> n;
for (i = 1; i <= n - 1; i++) {
cin >> u;
adj[i].push_back(u);
adj[u].push_back(i);
}
for (i = 0; i < n; i++) {
cin >> u;
x[i] = u;
}
dfs(0, -1);
cout << dp[0][1] << " ";
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
using namespace std;
long long n, mark[200009], dp[200009][2], col[200009], mod = 1e9 + 7, ans = 1,
x;
vector<long long> g[200009];
long long xgcd(long long a, long long b, long long &x, long long &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
long long x1, y1, gcd = xgcd(b, a % b, x1, y1);
x = y1;
y = x1 - (long long)(a / b) * y1;
return gcd;
}
long long dfs(long long x1) {
if (col[x1] == 1) {
dp[x1][0] = 0;
dp[x1][1] = 1;
return 0;
}
mark[x1] = 1;
vector<long long> v1, v2;
long long pr = 1;
for (long long i = 0; i < g[x1].size(); i++) {
if (mark[g[x1][i]] == 0) {
dfs(g[x1][i]);
pr *= ((dp[g[x1][i]][0] % mod) + (dp[g[x1][i]][1] % mod)) % mod;
pr %= mod;
v1.push_back((dp[g[x1][i]][0] + dp[g[x1][i]][1]) % mod);
v2.push_back(dp[g[x1][i]][1] % mod);
}
}
dp[x1][0] = pr % mod;
for (long long i = 0; i < v1.size(); i++) {
long long a, x = 1, y = 1, g1 = xgcd(v1[i], mod, x, y);
a = (dp[x1][0] * ((x + mod) % mod)) % mod;
dp[x1][1] += (a * v2[i]) % mod;
dp[x1][1] %= mod;
}
return 0;
}
int main() {
cin >> n;
for (long long i = 1; i < n; i++) {
cin >> x;
g[x].push_back(i);
g[i].push_back(x);
}
for (long long i = 0; i < n; i++) cin >> col[i];
for (long long i = 0; i < n; i++) {
if (mark[i] == 0 && col[i] == 0) {
dfs(i);
ans *= dp[i][1];
ans %= mod;
}
}
cout << 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;
using namespace std;
long long n, mark[200009], dp[200009][2], col[200009], mod = 1e9 + 7, ans = 1,
x;
vector<long long> g[200009];
long long xgcd(long long a, long long b, long long &x, long long &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
long long x1, y1, gcd = xgcd(b, a % b, x1, y1);
x = y1;
y = x1 - (long long)(a / b) * y1;
return gcd;
}
long long dfs(long long x1) {
if (col[x1] == 1) {
dp[x1][0] = 0;
dp[x1][1] = 1;
return 0;
}
mark[x1] = 1;
vector<long long> v1, v2;
long long pr = 1;
for (long long i = 0; i < g[x1].size(); i++) {
if (mark[g[x1][i]] == 0) {
dfs(g[x1][i]);
pr *= ((dp[g[x1][i]][0] % mod) + (dp[g[x1][i]][1] % mod)) % mod;
pr %= mod;
v1.push_back((dp[g[x1][i]][0] + dp[g[x1][i]][1]) % mod);
v2.push_back(dp[g[x1][i]][1] % mod);
}
}
dp[x1][0] = pr % mod;
for (long long i = 0; i < v1.size(); i++) {
long long a, x = 1, y = 1, g1 = xgcd(v1[i], mod, x, y);
a = (dp[x1][0] * ((x + mod) % mod)) % mod;
dp[x1][1] += (a * v2[i]) % mod;
dp[x1][1] %= mod;
}
return 0;
}
int main() {
cin >> n;
for (long long i = 1; i < n; i++) {
cin >> x;
g[x].push_back(i);
g[i].push_back(x);
}
for (long long i = 0; i < n; i++) cin >> col[i];
for (long long i = 0; i < n; i++) {
if (mark[i] == 0 && col[i] == 0) {
dfs(i);
ans *= dp[i][1];
ans %= mod;
}
}
cout << ans;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
int n, dp1[maxn], dp[maxn], c[maxn];
vector<int> v[maxn];
void enter() {
cin >> n;
for (int i = 0; i < n - 1; ++i) {
int x;
cin >> x;
v[x].push_back(i + 1);
v[i + 1].push_back(x);
}
for (int i = 0; i < n; ++i) cin >> c[i];
}
int P(int a, int b) {
if (b == 0) return 1;
long long r = P(a, b >> 1);
if (b & 1) return 1ll * r * r % mod * (long long)a % mod;
return 1ll * r * r % mod;
}
void DFS(int u, int par) {
if (v[u].size() == 1 && u != 0) {
dp[u] = c[u];
dp1[u] = c[u] ^ 1;
return;
}
int r = 1;
for (int c : v[u]) {
if (c == par) continue;
DFS(c, u);
r = 1ll * r * (dp[c] + dp1[c]) % mod;
}
dp[u] = dp1[u] = 0;
int d = 0;
for (int c : v[u]) {
if (c == par) continue;
int k = dp[c] + dp1[c];
d = (d + (long long)r * P(k, mod - 2) % mod * dp[c] % mod) % mod;
}
if (c[u])
dp1[u] = 0, dp[u] = r;
else
dp1[u] = r, dp[u] = d;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
enter();
DFS(0, -1);
cout << dp[0];
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 maxn = 1e5 + 5;
const int mod = 1e9 + 7;
int n, dp1[maxn], dp[maxn], c[maxn];
vector<int> v[maxn];
void enter() {
cin >> n;
for (int i = 0; i < n - 1; ++i) {
int x;
cin >> x;
v[x].push_back(i + 1);
v[i + 1].push_back(x);
}
for (int i = 0; i < n; ++i) cin >> c[i];
}
int P(int a, int b) {
if (b == 0) return 1;
long long r = P(a, b >> 1);
if (b & 1) return 1ll * r * r % mod * (long long)a % mod;
return 1ll * r * r % mod;
}
void DFS(int u, int par) {
if (v[u].size() == 1 && u != 0) {
dp[u] = c[u];
dp1[u] = c[u] ^ 1;
return;
}
int r = 1;
for (int c : v[u]) {
if (c == par) continue;
DFS(c, u);
r = 1ll * r * (dp[c] + dp1[c]) % mod;
}
dp[u] = dp1[u] = 0;
int d = 0;
for (int c : v[u]) {
if (c == par) continue;
int k = dp[c] + dp1[c];
d = (d + (long long)r * P(k, mod - 2) % mod * dp[c] % mod) % mod;
}
if (c[u])
dp1[u] = 0, dp[u] = r;
else
dp1[u] = r, dp[u] = d;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
enter();
DFS(0, -1);
cout << dp[0];
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const double eps = 1e-12;
const int inf = 2000000000;
long long int MOD = 1000000007;
int MOD1 = 1000000007;
int MOD2 = 1000000009;
inline bool checkBit(long long int n, long long int i) {
return n & (1LL << i);
}
inline long long int setBit(long long int n, long long int i) {
return n | (1LL << i);
;
}
inline long long int resetBit(long long int n, long long int i) {
return n & (~(1LL << i));
}
int dx[] = {0, 0, +1, -1};
int dy[] = {+1, -1, 0, 0};
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
inline bool isLeapYear(long long int year) {
return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}
inline void normal(long long int &a) {
a %= MOD;
(a < 0) && (a += MOD);
}
inline long long int modMul(long long int a, long long int b) {
a %= MOD, b %= MOD;
normal(a), normal(b);
return (a * b) % MOD;
}
inline long long int modAdd(long long int a, long long int b) {
a %= MOD, b %= MOD;
normal(a), normal(b);
return (a + b) % MOD;
}
inline long long int modSub(long long int a, long long int b) {
a %= MOD, b %= MOD;
normal(a), normal(b);
a -= b;
normal(a);
return a;
}
inline long long int modPow(long long int b, long long int p) {
long long int r = 1LL;
while (p) {
if (p & 1) r = modMul(r, b);
b = modMul(b, b);
p >>= 1LL;
}
return r;
}
inline long long int modDiv(long long int a, long long int b) {
return modMul(a, modPow(b, MOD - 2));
}
bool comp(const pair<long long int, pair<long long int, long long int> > &p1,
const pair<long long int, pair<long long int, long long int> > &p2) {
return p1.first > p2.first;
}
bool comp1(const pair<long long int, long long int> &p1,
const pair<long long int, long long int> &p2) {
if (p1.first == p2.first) {
return p1.second > p2.second;
}
return p1.first < p2.first;
}
long long int converter(string a) {
long long int i, mul = 1, r, t, ans = 0LL;
if (a.length() == 0) return 0;
for (i = a.length() - 1; i >= 0; i--) {
t = a[i] - '0';
r = t % 10;
ans += (mul * r);
mul = mul * 10;
}
return ans;
}
int msb(unsigned x) {
union {
double a;
int b[2];
};
a = x;
return (b[1] >> 20) - 1023;
}
const int MAX = 100005;
vector<int> adj[MAX];
long long int dp[MAX][2], par[MAX], n, vis[MAX], col[MAX];
void dfs(int u) {
int i, v, f = 0;
vis[u] = true;
for (i = 0; i < (int)adj[u].size(); ++i) {
v = adj[u][i];
if (!vis[v]) {
par[v] = u;
dfs(v);
}
}
long long int cur0, cur1;
if (col[u] == 0)
dp[u][0] = 1, dp[u][1] = 0;
else
dp[u][0] = 0, dp[u][1] = 1;
;
for (i = 0; i < (int)adj[u].size(); ++i) {
v = adj[u][i];
if (par[u] != v) {
cur0 = dp[u][0];
cur1 = dp[u][1];
dp[u][0] = (cur0 * dp[v][1]) % MOD;
dp[u][1] = (cur1 * dp[v][1]) % MOD;
dp[u][1] =
(dp[u][1] + (cur0 * dp[v][1]) % MOD + (cur1 * dp[v][0]) % MOD) % MOD;
dp[u][0] = (dp[u][0] + (cur0 * dp[v][0]) % MOD) % MOD;
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int x, i;
cin >> n;
for (i = 1; i < n; ++i) {
cin >> x;
adj[x].push_back(i);
adj[i].push_back(x);
}
for (i = 0; i < n; ++i) cin >> col[i];
dfs(0);
cout << dp[0][1] << '\n';
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 double PI = acos(-1);
const double eps = 1e-12;
const int inf = 2000000000;
long long int MOD = 1000000007;
int MOD1 = 1000000007;
int MOD2 = 1000000009;
inline bool checkBit(long long int n, long long int i) {
return n & (1LL << i);
}
inline long long int setBit(long long int n, long long int i) {
return n | (1LL << i);
;
}
inline long long int resetBit(long long int n, long long int i) {
return n & (~(1LL << i));
}
int dx[] = {0, 0, +1, -1};
int dy[] = {+1, -1, 0, 0};
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
inline bool isLeapYear(long long int year) {
return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}
inline void normal(long long int &a) {
a %= MOD;
(a < 0) && (a += MOD);
}
inline long long int modMul(long long int a, long long int b) {
a %= MOD, b %= MOD;
normal(a), normal(b);
return (a * b) % MOD;
}
inline long long int modAdd(long long int a, long long int b) {
a %= MOD, b %= MOD;
normal(a), normal(b);
return (a + b) % MOD;
}
inline long long int modSub(long long int a, long long int b) {
a %= MOD, b %= MOD;
normal(a), normal(b);
a -= b;
normal(a);
return a;
}
inline long long int modPow(long long int b, long long int p) {
long long int r = 1LL;
while (p) {
if (p & 1) r = modMul(r, b);
b = modMul(b, b);
p >>= 1LL;
}
return r;
}
inline long long int modDiv(long long int a, long long int b) {
return modMul(a, modPow(b, MOD - 2));
}
bool comp(const pair<long long int, pair<long long int, long long int> > &p1,
const pair<long long int, pair<long long int, long long int> > &p2) {
return p1.first > p2.first;
}
bool comp1(const pair<long long int, long long int> &p1,
const pair<long long int, long long int> &p2) {
if (p1.first == p2.first) {
return p1.second > p2.second;
}
return p1.first < p2.first;
}
long long int converter(string a) {
long long int i, mul = 1, r, t, ans = 0LL;
if (a.length() == 0) return 0;
for (i = a.length() - 1; i >= 0; i--) {
t = a[i] - '0';
r = t % 10;
ans += (mul * r);
mul = mul * 10;
}
return ans;
}
int msb(unsigned x) {
union {
double a;
int b[2];
};
a = x;
return (b[1] >> 20) - 1023;
}
const int MAX = 100005;
vector<int> adj[MAX];
long long int dp[MAX][2], par[MAX], n, vis[MAX], col[MAX];
void dfs(int u) {
int i, v, f = 0;
vis[u] = true;
for (i = 0; i < (int)adj[u].size(); ++i) {
v = adj[u][i];
if (!vis[v]) {
par[v] = u;
dfs(v);
}
}
long long int cur0, cur1;
if (col[u] == 0)
dp[u][0] = 1, dp[u][1] = 0;
else
dp[u][0] = 0, dp[u][1] = 1;
;
for (i = 0; i < (int)adj[u].size(); ++i) {
v = adj[u][i];
if (par[u] != v) {
cur0 = dp[u][0];
cur1 = dp[u][1];
dp[u][0] = (cur0 * dp[v][1]) % MOD;
dp[u][1] = (cur1 * dp[v][1]) % MOD;
dp[u][1] =
(dp[u][1] + (cur0 * dp[v][1]) % MOD + (cur1 * dp[v][0]) % MOD) % MOD;
dp[u][0] = (dp[u][0] + (cur0 * dp[v][0]) % MOD) % MOD;
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int x, i;
cin >> n;
for (i = 1; i < n; ++i) {
cin >> x;
adj[x].push_back(i);
adj[i].push_back(x);
}
for (i = 0; i < n; ++i) cin >> col[i];
dfs(0);
cout << dp[0][1] << '\n';
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 100 * 1001, MOD = 1e9 + 7;
long long n, dp[N][2];
long long r[N], l[N];
bool black[N];
vector<int> adj[N];
long long _mul(long long a, long long b) { return (1LL * a * b) % MOD; }
long long _sum(long long a, long long b) { return (a + b) % MOD; }
void dfs(int u, int par = -1) {
int size = adj[u].size();
if (size == 1 && par != -1) {
dp[u][1] = black[u], dp[u][0] = !black[u];
return;
}
for (auto v : adj[u])
if (v != par) dfs(v, u);
l[0] = r[size - 1] = 1;
for (int i = 0; i < size; i++) {
if (adj[u][i] != par)
l[i + 1] = _mul(l[i], dp[adj[u][i]][0] + dp[adj[u][i]][1]);
else
l[i + 1] = l[i];
}
for (int i = size - 1; 0 < i; i--) {
if (adj[u][i] != par)
r[i - 1] = _mul(r[i], dp[adj[u][i]][0] + dp[adj[u][i]][1]);
else
r[i - 1] = r[i];
}
if (black[u])
dp[u][1] = l[size];
else
for (int i = 0; i < size; i++) {
if (adj[u][i] != par)
dp[u][1] = _sum(dp[u][1], _mul(_mul(l[i], r[i]), dp[adj[u][i]][1]));
}
dp[u][0] = !black[u] * l[size];
}
int main() {
ios_base ::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
cin >> n;
for (int i = 1; i < n; i++) {
int u;
cin >> u;
adj[i].push_back(u);
adj[u].push_back(i);
}
for (int i = 0; i < n; i++) cin >> black[i];
dfs(0);
cout << dp[0][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>
using namespace std;
const int N = 100 * 1001, MOD = 1e9 + 7;
long long n, dp[N][2];
long long r[N], l[N];
bool black[N];
vector<int> adj[N];
long long _mul(long long a, long long b) { return (1LL * a * b) % MOD; }
long long _sum(long long a, long long b) { return (a + b) % MOD; }
void dfs(int u, int par = -1) {
int size = adj[u].size();
if (size == 1 && par != -1) {
dp[u][1] = black[u], dp[u][0] = !black[u];
return;
}
for (auto v : adj[u])
if (v != par) dfs(v, u);
l[0] = r[size - 1] = 1;
for (int i = 0; i < size; i++) {
if (adj[u][i] != par)
l[i + 1] = _mul(l[i], dp[adj[u][i]][0] + dp[adj[u][i]][1]);
else
l[i + 1] = l[i];
}
for (int i = size - 1; 0 < i; i--) {
if (adj[u][i] != par)
r[i - 1] = _mul(r[i], dp[adj[u][i]][0] + dp[adj[u][i]][1]);
else
r[i - 1] = r[i];
}
if (black[u])
dp[u][1] = l[size];
else
for (int i = 0; i < size; i++) {
if (adj[u][i] != par)
dp[u][1] = _sum(dp[u][1], _mul(_mul(l[i], r[i]), dp[adj[u][i]][1]));
}
dp[u][0] = !black[u] * l[size];
}
int main() {
ios_base ::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
cin >> n;
for (int i = 1; i < n; i++) {
int u;
cin >> u;
adj[i].push_back(u);
adj[u].push_back(i);
}
for (int i = 0; i < n; i++) cin >> black[i];
dfs(0);
cout << dp[0][1] << "\n";
}
```
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target( \
"avx,avx2,fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
long long MOD = 1e9 + 7;
const int N = 200000;
vector<long long> gr[N];
long long sz[N];
long long st(long long t, long long id, long long mod) {
long long res = 1, a = t, n = id;
while (n) {
if (n & 1) res = (res * a) % mod;
a = (a * a) % mod;
n >>= 1;
}
return res % mod;
}
long long del(long long a, long long b) {
return (a * st(b, MOD - 2, MOD)) % MOD;
}
pair<long long, long long> dfs(long long v, long long pr = -1) {
pair<long long, long long> ans = {0, 1};
vector<pair<long long, long long> > gg;
for (auto i : gr[v]) {
if (i != pr) {
auto g = dfs(i, v);
gg.push_back(g);
ans.second = ans.second * (g.second + g.first) % MOD;
}
}
for (auto g : gg) {
ans.first += del(ans.second, (g.second + g.first)) * g.first % MOD;
ans.first %= MOD;
}
if (sz[v]) {
ans.first = ans.second;
ans.second = 0;
}
return ans;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0),
cout << fixed << setprecision(20);
long long n;
cin >> n;
for (int i = 0; i < n - 1; i++) {
long long u, v = i + 1;
cin >> u;
gr[u].push_back(v);
gr[v].push_back(u);
}
for (int i = 0; i < n; i++) {
cin >> sz[i];
}
auto mem = dfs(0, 0);
cout << mem.first;
}
|
### Prompt
Please formulate a cpp solution to the following problem:
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of k (0 β€ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (k + 1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109 + 7).
Input
The first line contains an integer n (2 β€ n β€ 105) β the number of tree vertices.
The second line contains the description of the tree: n - 1 integers p0, p1, ..., pn - 2 (0 β€ pi β€ i). Where pi means that there is an edge connecting vertex (i + 1) of the tree and vertex pi. Consider tree vertices are numbered from 0 to n - 1.
The third line contains the description of the colors of the vertices: n integers x0, x1, ..., xn - 1 (xi is either 0 or 1). If xi is equal to 1, vertex i is colored black. Otherwise, vertex i is colored white.
Output
Output a single integer β the number of ways to split the tree modulo 1000000007 (109 + 7).
Examples
Input
3
0 0
0 1 1
Output
2
Input
6
0 1 1 0 4
1 1 0 0 1 0
Output
1
Input
10
0 1 2 1 4 4 4 0 8
0 0 0 1 0 1 1 0 0 1
Output
27
### Response
```cpp
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target( \
"avx,avx2,fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
long long MOD = 1e9 + 7;
const int N = 200000;
vector<long long> gr[N];
long long sz[N];
long long st(long long t, long long id, long long mod) {
long long res = 1, a = t, n = id;
while (n) {
if (n & 1) res = (res * a) % mod;
a = (a * a) % mod;
n >>= 1;
}
return res % mod;
}
long long del(long long a, long long b) {
return (a * st(b, MOD - 2, MOD)) % MOD;
}
pair<long long, long long> dfs(long long v, long long pr = -1) {
pair<long long, long long> ans = {0, 1};
vector<pair<long long, long long> > gg;
for (auto i : gr[v]) {
if (i != pr) {
auto g = dfs(i, v);
gg.push_back(g);
ans.second = ans.second * (g.second + g.first) % MOD;
}
}
for (auto g : gg) {
ans.first += del(ans.second, (g.second + g.first)) * g.first % MOD;
ans.first %= MOD;
}
if (sz[v]) {
ans.first = ans.second;
ans.second = 0;
}
return ans;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0),
cout << fixed << setprecision(20);
long long n;
cin >> n;
for (int i = 0; i < n - 1; i++) {
long long u, v = i + 1;
cin >> u;
gr[u].push_back(v);
gr[v].push_back(u);
}
for (int i = 0; i < n; i++) {
cin >> sz[i];
}
auto mem = dfs(0, 0);
cout << mem.first;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
void EE(int a, int b, int& x, int& y) {
if (a % b == 0) {
x = 0;
y = 1;
return;
}
EE(b, a % b, x, y);
int temp = x;
x = y;
y = temp - y * (a / b);
}
int inverse(int a, int m) {
int x, y;
EE(a, m, x, y);
if (x < 0) x += m;
return x;
}
int n;
vector<int> g[100005];
int dp[100005][2];
vector<int> x;
void dfs(int u, int par) {
dp[u][0] = 1;
if (x[u] == 1) {
dp[u][0] = 0;
dp[u][1] = 1;
}
for (auto v : g[u]) {
if (v != par) {
dfs(v, u);
if (x[u] == 1) {
dp[u][1] = (1LL * dp[u][1] * (dp[v][0] + dp[v][1])) % mod;
} else {
dp[u][0] = (1LL * dp[u][0] * (dp[v][0] + dp[v][1])) % mod;
}
}
}
if (x[u] != 1) {
int val = dp[u][0];
dp[u][1] = 0;
for (auto v : g[u]) {
if (v != par) {
int temp = (1LL * val * inverse(dp[v][0] + dp[v][1], mod)) % mod;
temp = (1LL * temp * dp[v][1]) % mod;
dp[u][1] = (dp[u][1] + temp) % mod;
}
}
}
return;
}
int main() {
cin >> n;
x.resize(n);
for (int i = 0, v; i < n - 1; i++) {
cin >> v;
g[i + 1].push_back(v);
g[v].push_back(i + 1);
}
for (int i = 0; i < n; i++) {
cin >> x[i];
}
dfs(0, -1);
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 mod = 1e9 + 7;
void EE(int a, int b, int& x, int& y) {
if (a % b == 0) {
x = 0;
y = 1;
return;
}
EE(b, a % b, x, y);
int temp = x;
x = y;
y = temp - y * (a / b);
}
int inverse(int a, int m) {
int x, y;
EE(a, m, x, y);
if (x < 0) x += m;
return x;
}
int n;
vector<int> g[100005];
int dp[100005][2];
vector<int> x;
void dfs(int u, int par) {
dp[u][0] = 1;
if (x[u] == 1) {
dp[u][0] = 0;
dp[u][1] = 1;
}
for (auto v : g[u]) {
if (v != par) {
dfs(v, u);
if (x[u] == 1) {
dp[u][1] = (1LL * dp[u][1] * (dp[v][0] + dp[v][1])) % mod;
} else {
dp[u][0] = (1LL * dp[u][0] * (dp[v][0] + dp[v][1])) % mod;
}
}
}
if (x[u] != 1) {
int val = dp[u][0];
dp[u][1] = 0;
for (auto v : g[u]) {
if (v != par) {
int temp = (1LL * val * inverse(dp[v][0] + dp[v][1], mod)) % mod;
temp = (1LL * temp * dp[v][1]) % mod;
dp[u][1] = (dp[u][1] + temp) % mod;
}
}
}
return;
}
int main() {
cin >> n;
x.resize(n);
for (int i = 0, v; i < n - 1; i++) {
cin >> v;
g[i + 1].push_back(v);
g[v].push_back(i + 1);
}
for (int 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;
constexpr long long mod = 1000000007;
pair<long long, long long> dfs(int v, const vector<vector<int>> &children,
const vector<int> &black) {
long long zero = 0;
long long one = 0;
(black[v] ? one : zero) = 1;
for (const auto &child : children[v]) {
const auto result = dfs(child, children, black);
long long t_zero = (zero * result.second + zero * result.first) % mod;
one =
(one * result.second + zero * result.second + one * result.first) % mod;
zero = t_zero;
}
return {zero, one};
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<vector<int>> children(n);
for (int i = 1; i < n; ++i) {
int p;
cin >> p;
children[p].emplace_back(i);
}
vector<int> black(n);
for (auto &e : black) cin >> e;
const auto ans = dfs(0, children, black);
cout << ans.second << endl;
return EXIT_SUCCESS;
}
|
### 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;
constexpr long long mod = 1000000007;
pair<long long, long long> dfs(int v, const vector<vector<int>> &children,
const vector<int> &black) {
long long zero = 0;
long long one = 0;
(black[v] ? one : zero) = 1;
for (const auto &child : children[v]) {
const auto result = dfs(child, children, black);
long long t_zero = (zero * result.second + zero * result.first) % mod;
one =
(one * result.second + zero * result.second + one * result.first) % mod;
zero = t_zero;
}
return {zero, one};
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<vector<int>> children(n);
for (int i = 1; i < n; ++i) {
int p;
cin >> p;
children[p].emplace_back(i);
}
vector<int> black(n);
for (auto &e : black) cin >> e;
const auto ans = dfs(0, children, black);
cout << ans.second << endl;
return EXIT_SUCCESS;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
const int mod = 1000000007;
long long dp[maxn][2];
vector<int> G[maxn];
int col[maxn];
void gcd(long long a, long long b, long long &d, long long &x, long long &y) {
if (!b) {
d = a;
x = 1;
y = 0;
} else {
gcd(b, a % b, d, y, x);
y -= x * (a / b);
}
}
long long niyuan(long long a) {
long long x, y, d;
gcd(a, mod, d, x, y);
return (x + mod) % mod;
}
void dfs(int u, int fa) {
if (G[u].size() == 0) {
dp[u][col[u]] = 1;
return;
}
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
dfs(v, u);
}
if (col[u]) {
dp[u][1] = 1;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
dp[u][1] *= dp[v][0] + dp[v][1];
dp[u][1] %= mod;
}
if (dp[u][1] == 0) dp[u][1] = 1;
} else {
dp[u][0] = 1;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
dp[u][0] *= dp[v][0] + dp[v][1];
dp[u][0] %= mod;
}
long long tmp = dp[u][0];
dp[u][1] = 0;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (dp[v][1]) {
long long t = (tmp * niyuan(dp[v][0] + dp[v][1])) % mod;
dp[u][1] += dp[v][1] * t;
dp[u][1] %= mod;
}
}
}
}
int main() {
int n, pi;
while (cin >> n) {
for (int i = 0; i < n; i++) G[i].clear();
for (int i = 1; i < n; i++) {
scanf("%d", &pi);
G[pi].push_back(i);
}
for (int i = 0; i < n; i++) scanf("%d", col + i);
memset(dp, 0, sizeof dp);
dfs(0, -1);
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 = 100010;
const int mod = 1000000007;
long long dp[maxn][2];
vector<int> G[maxn];
int col[maxn];
void gcd(long long a, long long b, long long &d, long long &x, long long &y) {
if (!b) {
d = a;
x = 1;
y = 0;
} else {
gcd(b, a % b, d, y, x);
y -= x * (a / b);
}
}
long long niyuan(long long a) {
long long x, y, d;
gcd(a, mod, d, x, y);
return (x + mod) % mod;
}
void dfs(int u, int fa) {
if (G[u].size() == 0) {
dp[u][col[u]] = 1;
return;
}
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
dfs(v, u);
}
if (col[u]) {
dp[u][1] = 1;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
dp[u][1] *= dp[v][0] + dp[v][1];
dp[u][1] %= mod;
}
if (dp[u][1] == 0) dp[u][1] = 1;
} else {
dp[u][0] = 1;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
dp[u][0] *= dp[v][0] + dp[v][1];
dp[u][0] %= mod;
}
long long tmp = dp[u][0];
dp[u][1] = 0;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (dp[v][1]) {
long long t = (tmp * niyuan(dp[v][0] + dp[v][1])) % mod;
dp[u][1] += dp[v][1] * t;
dp[u][1] %= mod;
}
}
}
}
int main() {
int n, pi;
while (cin >> n) {
for (int i = 0; i < n; i++) G[i].clear();
for (int i = 1; i < n; i++) {
scanf("%d", &pi);
G[pi].push_back(i);
}
for (int i = 0; i < n; i++) scanf("%d", col + i);
memset(dp, 0, sizeof dp);
dfs(0, -1);
cout << dp[0][1] << endl;
}
return 0;
}
```
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.