-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathB.cpp
88 lines (72 loc) · 1.79 KB
/
B.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <vector>
#include <climits>
using namespace std;
const int MAX = 200001;
const int LOGMAX = 20;
size_t n, l;
vector<pair<int, int>> g[MAX];
int tin[MAX], tout[MAX];
int timer;
pair<int, int> up[MAX][LOGMAX];
void dfs(int v, int p = 1, int c = INT_MAX) {
tin[v] = timer++;
up[v][0] = make_pair(p, c);
for (int i = 1; i <= l; ++i) {
up[v][i].first = up[up[v][i - 1].first][i - 1].first;
up[v][i].second = min(up[v][i - 1].second, up[up[v][i - 1].first][i - 1].second);
}
for (int i = 0; i < g[v].size(); ++i) {
int to = g[v][i].first;
dfs(to, v, g[v][i].second);
}
tout[v] = timer++;
}
bool upper(int a, int b) {
return tin[a] <= tin[b] && tout[a] >= tout[b];
}
int minlca(int a, int b) {
int res = INT_MAX;
for (int i = l; i >= 0; i--) {
if (!upper(up[a][i].first, b)) {
res = min(res, up[a][i].second);
a = up[a][i].first;
}
}
if (!upper(a, b)) {
res = min(res, up[a][0].second);
}
for (int i = l; i >= 0; i--) {
if (!upper(up[b][i].first, a)) {
res = min(res, up[b][i].second);
b = up[b][i].first;
}
}
if (!upper(b, a)) {
res = min(res, up[b][0].second);
}
return res;
}
int main() {
freopen("minonpath.in", "r", stdin);
freopen("minonpath.out", "w", stdout);
int m;
scanf("%d", &n);
l = 1;
while ((1 << l) <= n) ++l;
for (int i = 2; i < n + 1; i++) {
int x;
int p;
scanf("%d %d", &x, &p);
g[x].push_back(make_pair(i, p));
}
dfs(1);
scanf("%d", &m);
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d %d", &u, &v);
int res = minlca(u, v);
printf("%d\n", res);
};
return 0;
}