-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathboxes.cpp
72 lines (67 loc) · 1.45 KB
/
boxes.cpp
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
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> v2i;
const int MAXN = 2e5;
const int MAX_LEVEL = ceil(log(MAXN) / log(2));
vi C, depth;
v2i T, P;
void dfs(int n) {
for (int nn : T[n]) {
depth[nn] = depth[n] + 1;
P[nn][0] = n;
dfs(nn);
C[n] += C[nn];
}
}
int lca(int a, int b) {
int d = depth[b] - depth[a];
if (d < 0) d *= -1, swap(a, b);
for (int i = MAX_LEVEL; i--;) {
if (d & (1 << i)) b = P[b][i];
}
for (int i = MAX_LEVEL; i--;) {
if (P[a][i] != P[b][i]) a = P[a][i], b = P[b][i];
}
if (a == b) return a;
return P[a][0];
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int n, m, q, k;
cin >> n;
T.resize(n + 1), C.assign(n + 1, 1), depth.assign(n + 1, 0);
P.assign(n + 1, vi(MAX_LEVEL));
for (int i = 0; i < n; ++i) {
cin >> k;
T[k].push_back(i + 1);
}
dfs(0);
for (int j = 1; j < MAX_LEVEL; ++j) {
for (int i = 0; i <= n; ++i) {
P[i][j] = P[P[i][j - 1]][j - 1];
}
}
cin >> q;
while (q--) {
cin >> m;
vi M(m);
for (int &e : M) cin >> e;
int res = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < m; ++j) {
if (i == j) continue;
int &a = M[i], &b = M[j];
if (a < 0 || b < 0) continue;
int c = lca(a, b);
if (c == b) swap(a, b);
if (c == a) b = -1;
}
}
for (int e : M) {
if (e > 0) res += C[e];
}
printf("%d\n", res);
}
return 0;
}