forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD.cpp
More file actions
122 lines (104 loc) · 2.94 KB
/
D.cpp
File metadata and controls
122 lines (104 loc) · 2.94 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#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 EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define DEBUG(x) { cout << #x << " = "; cout << (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))
using namespace std;
const int MN = 100111;
int n, getIn[MN], getOut[MN], cur, father[20][MN], root, h[MN];
vector<int> ke[MN];
void init() {
memset(father, 0, sizeof father);
FOR(i,1,n) ke[i].clear();
root = 1;
cur = 0;
h[1] = 1;
}
void dfs(int u, int fu) {
getIn[u] = ++cur;
father[0][u] = fu;
for(int v : ke[u]) {
if (v == fu) continue;
h[v] = h[u] + 1;
dfs(v, u);
}
getOut[u] = ++cur;
}
void initLCA() {
FOR(t,1,19) {
FOR(u,1,n) {
father[t][u] = father[t-1][ father[t-1][u] ];
}
}
}
int lca(int u, int v) {
if (u == v) return u;
if (h[u] < h[v]) swap(u, v);
if (h[u] > h[v]) {
FORD(i,19,0) {
int x = father[i][u];
if (h[x] >= h[v]) u = x;
}
}
if (u == v) return u;
FORD(i,19,0)
if (father[i][u] != father[i][v]) {
u = father[i][u];
v = father[i][v];
}
return father[0][u];
}
// u is ancestor of v ?
bool isAncestor(int u, int v) {
if (u == v) return true;
return getIn[u] < getIn[v] && getOut[v] < getOut[u];
}
int get(int u, int v) {
if (isAncestor(v, u)) swap(u, v);
if (isAncestor(u, v)) {
int x = lca(v, root);
if (isAncestor(u, x)) return x;
else return u;
}
else {
int l = lca(u, v);
int x = lca(u, root);
if (x != l && isAncestor(l, x)) return x;
int y = lca(v, root);
if (y != l && isAncestor(l, y)) return y;
return l;
}
}
int main() {
freopen("dynamic.in", "r", stdin);
freopen("dynamic.out", "w", stdout);
while (scanf("%d", &n) == 1 && n) {
init();
FOR(i,2,n) {
int u, v; scanf("%d%d", &u, &v);
ke[u].push_back(v);
ke[v].push_back(u);
}
dfs(1, -1);
initLCA();
int q; scanf("%d", &q);
while (q--) {
char typ; scanf("%c", &typ);
while (typ != '?' && typ != '!') scanf("%c", &typ);
assert(typ == '?' || typ == '!');
if (typ == '?') {
int u, v; scanf("%d%d", &u, &v);
printf("%d\n", get(u, v));
}
else {
int u; scanf("%d", &u);
root = u;
}
}
}
return 0;
}