-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathB.cpp
More file actions
77 lines (66 loc) · 1.83 KB
/
B.cpp
File metadata and controls
77 lines (66 loc) · 1.83 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
73
74
75
76
77
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; ++i)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; --i)
#define REP(i,a) for(int i=0,_a=(a); i < _a; ++i)
#define DEBUG(X) { cout << #X << " = " << (X) << endl; }
#define PR(A,n) { cout << #A << " = "; FOR(_,1,n) cout << A[_] << ' '; cout << endl; }
#define PR0(A,n) { cout << #A << " = "; REP(_,n) cout << A[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
#define ll long long
#define __builtin_popcount __builtin_popcountll
#define SZ(x) ((int) (x).size())
using namespace std;
double safe_sqrt(double x) {
return sqrt(max((double)0.0,x));
}
int GI(ll& x) {
return scanf("%lld", &x);
}
const int MN = 300111;
int n, q, f[MN], cen[MN], father[MN], ln[MN], save[MN];
vector<int> ke[MN];
bool isCentroid(int u, int x) {
if (x == u) return true;
// cut at x, what is max size?
int res = f[u] - f[x];
res = max(res, ln[x]);
return res * 2 <= f[u];
}
void dfs1(int u) {
f[u] = 1;
ln[u] = 0;
save[u] = -1;
for(int v : ke[u]) {
dfs1(v);
f[u] += f[v];
if (f[v] > ln[u]) {
ln[u] = f[v];
save[u] = v;
}
}
if (ln[u] == 0) cen[u] = u;
else {
int x = cen[save[u]];
while (!isCentroid(u, x)) {
x = father[x];
}
cen[u] = x;
}
}
int main() {
ios :: sync_with_stdio(0); cin.tie(0);
cout << (fixed) << setprecision(9);
while (scanf("%d%d", &n, &q) == 2) {
FOR(i,1,n) ke[i].clear();
FOR(i,2,n) {
int fi; scanf("%d", &fi);
father[i] = fi;
ke[fi].push_back(i);
}
dfs1(1);
while (q--) {
int u; scanf("%d", &u);
printf("%d\n", cen[u]);
}
}
}