-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathADAGRAFT.cpp
More file actions
72 lines (72 loc) · 1.6 KB
/
Copy pathADAGRAFT.cpp
File metadata and controls
72 lines (72 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Ivan Carvalho
// Solution to https://www.spoj.com/problems/ADAGRAFT/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 4 * 1e5 + 10;
const ll MOD = 1e9 + 7;
bitset<MOD> cjt;
ll produto;
vector<int> grafo[MAXN];
int ini[MAXN], fim[MAXN], ptr, sz[MAXN], resp, cor[MAXN], raiz, N, vetor[MAXN];
void add(int val) {
if (!cjt.test(val)) {
resp++;
cjt.flip(val);
}
}
void remove(int val) {
if (cjt.test(val)) {
resp--;
cjt.flip(val);
}
}
void dfs_init(int v) {
sz[v] = 1;
ini[v] = ++ptr;
vetor[ptr] = v;
for (int u : grafo[v]) {
dfs_init(u);
sz[v] += sz[u];
}
fim[v] = ptr;
}
void dfs_sack(int v, int keep) {
int mx = -1, big = -1;
for (int u : grafo[v]) {
if (sz[u] > mx) {
mx = sz[u];
big = u;
}
}
for (int u : grafo[v]) {
if (u == big) continue;
dfs_sack(u, 0);
}
if (big != -1) {
dfs_sack(big, 1);
for (int i = ini[v]; i < ini[big]; i++) add(cor[vetor[i]]);
for (int i = fim[big] + 1; i <= fim[v]; i++) add(cor[vetor[i]]);
} else {
add(cor[v]);
}
produto *= 1LL * resp;
produto %= MOD;
if (keep == 0) {
for (int i = ini[v]; i <= fim[v]; i++) remove(cor[vetor[i]]);
}
}
int main() {
produto = 1LL;
scanf("%d", &N);
for (int i = 1; i < N; i++) {
int p;
scanf("%d", &p);
grafo[p].push_back(i);
}
for (int i = 0; i < N; i++) scanf("%d", &cor[i]);
dfs_init(0);
dfs_sack(0, 1);
printf("%lld\n", produto);
return 0;
}