-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlca_ptr.cpp
214 lines (203 loc) · 5.88 KB
/
lca_ptr.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include<iostream>
#include<vector>
using namespace std;
const int SIZE = 10000;
class Tree
{
private:
vector<int>tree[SIZE];
int used[SIZE];
int euler_order[SIZE];
int dfs_lvl[SIZE];
int first_appearance[SIZE];
int nodes;
int edges;
public:
int lvl=0,order_idx=0;
Tree()
{
this -> nodes = 0;
this -> edges = 0;
}
void Set()
{
cin>>this -> nodes>>this -> edges;
for(int i = 0; i < edges; i++)
{
int u,v;
cin>>u>>v;
this -> tree[u].push_back(v);
this -> tree[v].push_back(u);
}
}
void DFS(int node)
{
used[node] = 1;
order_idx++;
this -> euler_order[order_idx] = node;
this -> first_appearance[node] = order_idx;
this -> dfs_lvl[order_idx] = lvl;
for(int i = 0; i < tree[node].size(); i++)
{
int v = tree[node][i];
if(used[v] != 1)
{
lvl++;
DFS(v);
lvl--;
order_idx++;
this -> dfs_lvl[order_idx] = lvl;
this -> euler_order[order_idx] = node;
}
}
}
int GetEulerOrder(int index)
{
return this -> euler_order[index];
}
int GetDFSLvl(int index)
{
return this -> dfs_lvl[index];
}
int GetFirstAppearance(int index)
{
return this -> first_appearance[index];
}
};
class SegmentTree : public Tree
{
private:
class Node
{
private:
int left_bound;
int right_bound;
int index;
Node *left;
Node *right;
public:
Node()
{
this -> left_bound = -2;
this -> right_bound = -2;
this -> index = -2;
this -> left = NULL;
this -> right = NULL;
}
void SetBounds(int left_bound, int right_bound)
{
this -> left_bound = left_bound;
this -> right_bound = right_bound;
}
void SetSons(Node *left, Node *right)
{
this -> left = left;
this -> right = right;
}
void SetIndex(int index)
{
this -> index = index;
}
Node *GetLeft()
{
return this -> left;
}
Node *GetRight()
{
return this -> right;
}
int GetLeftBound()
{
return this -> left_bound;
}
int GetRightBound()
{
return this -> right_bound;
}
int GetIndex()
{
return this -> index;
}
};
Node *root;
public:
SegmentTree()
{
this -> root = new Node();
}
void BuildSegTree(Node *node, int left_bound, int right_bound)
{
if(left_bound == right_bound)
{
node -> SetBounds(left_bound, left_bound);
node -> SetIndex(left_bound);
node -> SetSons(NULL, NULL);
return;
}
else
{
Node *left = new Node();
left -> SetBounds(left_bound, (left_bound+right_bound)/2);
Node *right = new Node();
right -> SetBounds((left_bound+right_bound)/2+1,right_bound);
node -> SetSons(left, right);
BuildSegTree(left,left_bound, (left_bound+right_bound)/2);
BuildSegTree(right,(left_bound+right_bound)/2+1,right_bound);
int l = node -> GetLeft() -> GetIndex();
int r = node -> GetRight() -> GetIndex();
if(GetDFSLvl(l) < GetDFSLvl(r))
{
node -> SetIndex(l);
}
else
{
node -> SetIndex(r);
}
}
}
void BuildSegTree()
{
BuildSegTree(this -> root, 1, order_idx);
this -> root -> SetBounds(1, order_idx);
}
int Query(Node *node, int left, int right)
{
int l,r;
if(right < node -> GetLeftBound() || left > node -> GetRightBound())
return -1;
if(node -> GetLeftBound() >= left && node -> GetRightBound() <= right)
return node -> GetIndex();
l = Query(node -> GetLeft(), left, right);
r = Query(node -> GetRight(), left, right);
if(l == -1)
return r;
if(r == -1)
return l;
if(GetDFSLvl(l) < GetDFSLvl(r))
return l;
return r;
}
int Query(int u, int v)
{
int left = GetFirstAppearance(u);
int right = GetFirstAppearance(v);
int index = Query(this -> root, left, right);
return GetEulerOrder(index);
}
};
int main()
{
SegmentTree A;
A.Set();
A.DFS(1);
A.BuildSegTree();
int q;
cin>>q;
for(int i = 0; i < q; i++)
{
int u, v;
cin>>u>>v;
cout<<A.Query(u, v)<<endl;
}
return 0;
}