-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathLOWEST COMMON ANCESTOR.JAVA
More file actions
377 lines (366 loc) · 11.6 KB
/
LOWEST COMMON ANCESTOR.JAVA
File metadata and controls
377 lines (366 loc) · 11.6 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import java.io.*;
import java.util.*;
import java.text.*;
import java.lang.*;
import java.math.*;
public class Main{
/*********************************************Constants******************************************/
static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static long mod=(long)1e9+7;
static long mod1=998244353;
static boolean sieve[];
static ArrayList<Integer> primes;
static ArrayList<Long> factorial;
static HashSet<Pair> graph[];
static boolean oj = System.getProperty("ONLINE_JUDGE") != null;
/****************************************Solutions Begins***************************************/
static int first[],height[];
static ArrayList<Integer> euler;
static class segmentTree{
int n=0;
int[] lo,hi;
int[] value;
segmentTree(int n){
this.n=n;
lo=new int[4*n+1];
hi=new int[4*n+1];
value=new int[4*n+1];
init(1,1,n);
}
void init(int i,int left,int right){
lo[i]=left;
hi[i]=right;
if(left==right){
return;
}
int mid=(left+right)/2;
init(2*i,left,mid);
init(2*i+1,mid+1,right);
}
void update(int left,int right,int val){
update(1,left,right,val);
}
int query(int left,int right){
return query(1,left,right);
}
void update(int i,int left,int right,int val){
if(left>hi[i]||right<lo[i]){
return ;
}
if(lo[i]==hi[i]){
value[i]=val;
return ;
}
update(2*i,left,right,val);
update(2*i+1,left,right,val);
int l=value[2*i];
int r=value[2*i+1];
int ans=0;
if(l==-1&&r==-1){
ans=-1;
}
else if(l==-1){
ans=r;
}
else if(r==-1){
ans=l;
}
else{
if(height[l]>=height[r]){
ans=r;
}
else{
ans=l;
}
}
value[i]=ans;
return;
}
int query(int i,int left,int right){
if(left>hi[i]||right<lo[i]){
return -1;
}
if(lo[i]>=left&&hi[i]<=right){
return value[i];
}
int l=query(2*i,left,right);
int r=query(2*i+1,left,right);
int ans=0;
if(l==-1&&r==-1){
ans=-1;
}
else if(l==-1){
ans=r;
}
else if(r==-1){
ans=l;
}
else{
if(height[l]>=height[r]){
ans=r;
}
else{
ans=l;
}
}
return ans;
}
}
static void dfs(int start,int par,int h){
first[start]=euler.size();
height[start]=h;
euler.add(start);
for(Pair p:graph[start]){
if(p.u!=par){
dfs(p.u,start,h+1);
euler.add(start);
}
}
}
public static void main (String[] args) throws Exception {
String st[]=br.readLine().split(" ");
int n=Integer.parseInt(st[0]);
Makegraph(n+1);
for(int i=0;i<n-1;i++){
st=br.readLine().split(" ");
int a=Integer.parseInt(st[0]);
int b=Integer.parseInt(st[1]);
addEdge(a,b,1);
addEdge(b,a,1);
}
first=new int[n+1];
height=new int[n+1];
euler=new ArrayList<>();
dfs(1,1,1);
segmentTree seg=new segmentTree(euler.size());
// debug(first);
// debug(height);
// debug(euler);
// debug(seg.lo);
// debug(seg.hi);
for(int i=1;i<=euler.size();i++){
seg.update(i,i,euler.get(i-1));
// debug(seg.value);
}
// debug(seg.value);
st=br.readLine().split(" ");
int q=Integer.parseInt(st[0]);
while(q-->0){
st=br.readLine().split(" ");
int r=Integer.parseInt(st[0]);
int u=Integer.parseInt(st[1]);
int v=Integer.parseInt(st[2]);
int fr=first[r];
int fu=first[u];
int fv=first[v];
int ss=Math.min(fu,fv)+1;
int ee=Math.max(fu,fv)+1;
int uv=seg.query(ss,ee);
ss=Math.min(fu,fr)+1;
ee=Math.max(fu,fr)+1;
int ru=seg.query(ss,ee);
ss=Math.min(fv,fr)+1;
ee=Math.max(fv,fr)+1;
int rv=seg.query(ss,ee);
int ans=0;
// debug(uv,ru,rv);
if(ru==rv){
out.println(uv);
continue;
}
if(ru==uv){
out.println(rv);
continue;
}
if(rv==uv){
out.println(ru);
continue;
}
}
/****************************************Solutions Ends**************************************************/
out.flush();
out.close();
}
/****************************************Template Begins************************************************/
static String[] nl() throws Exception{
return br.readLine().split(" ");
}
static String[] nls() throws Exception{
return br.readLine().split("");
}
static int pi(String str) {
return Integer.parseInt(str);
}
static long pl(String str){
return Long.parseLong(str);
}
static double pd(String str){
return Double.parseDouble(str);
}
/***************************************Precision Printing**********************************************/
static void printPrecision(double d){
DecimalFormat ft = new DecimalFormat("0.00000000000000000");
out.println(ft.format(d));
}
/**************************************Bit Manipulation**************************************************/
static void printMask(long mask){
System.out.println(Long.toBinaryString(mask));
}
static int countBit(int mask){
int ans=0;
while(mask!=0){
if(mask%2==1){
ans++;
}
mask/=2;
}
return ans;
}
/******************************************Graph*********************************************************/
static void Makegraph(int n){
graph=new HashSet[n];
for(int i=0;i<n;i++){
graph[i]=new HashSet<>();
}
}
static void addEdge(int a,int b,int c){
graph[a].add(new Pair(b,c));
}
/*********************************************PAIR********************************************************/
static class PairComp implements Comparator<Pair>{
public int compare(Pair p1,Pair p2){
if(p1.u!=p2.u){
return p1.u-p2.u;
}
else{
return p1.u-p2.u;
}
}
}
static class Pair implements Comparable<Pair> {
int u;
int v;
int index=-1;
public Pair(int u, int v) {
this.u = u;
this.v = v;
}
public int hashCode() {
int hu = (int) (u ^ (u >>> 32));
int hv = (int) (v ^ (v >>> 32));
return 31 * hu + hv;
}
public boolean equals(Object o) {
Pair other = (Pair) o;
return u == other.u && v == other.v;
}
public int compareTo(Pair other) {
if(index!=other.index)
return Long.compare(index, other.index);
return Long.compare(v, other.v)!=0?Long.compare(v, other.v):Long.compare(u, other.u);
}
public String toString() {
return "[u=" + u + ", v=" + v + "]";
}
}
/******************************************Long Pair*************************************************/
static class PairCompL implements Comparator<Pairl>{
public int compare(Pairl p1,Pairl p2){
long aa=p2.v-p1.v;
if(aa<0){
return -1;
}
else if(aa>0){
return 1;
}
else{
return 0;
}
}
}
static class Pairl implements Comparable<Pairl> {
long u;
long v;
int index=-1;
public Pairl(long u, long v) {
this.u = u;
this.v = v;
}
public int hashCode() {
int hu = (int) (u ^ (u >>> 32));
int hv = (int) (v ^ (v >>> 32));
return 31 * hu + hv;
}
public boolean equals(Object o) {
Pair other = (Pair) o;
return u == other.u && v == other.v;
}
public int compareTo(Pairl other) {
if(index!=other.index)
return Long.compare(index, other.index);
return Long.compare(v, other.v)!=0?Long.compare(v, other.v):Long.compare(u, other.u);
}
public String toString() {
return "[u=" + u + ", v=" + v + "]";
}
}
/*****************************************DEBUG***********************************************************/
public static void debug(Object... o) {
if(!oj)
System.out.println(Arrays.deepToString(o));
}
/************************************MODULAR EXPONENTIATION***********************************************/
static long modulo(long a,long b,long c) {
long x=1;
long y=a;
while(b > 0){
if(b%2 == 1){
x=(x*y)%c;
}
y = (y*y)%c; // squaring the base
b /= 2;
}
return x%c;
}
/********************************************GCD**********************************************************/
static long gcd(long x, long y)
{
if(x==0)
return y;
if(y==0)
return x;
long r=0, a, b;
a = (x > y) ? x : y; // a is greater number
b = (x < y) ? x : y; // b is smaller number
r = b;
while(a % b != 0)
{
r = a % b;
a = b;
b = r;
}
return r;
}
/******************************************SIEVE**********************************************************/
static void sieveMake(int n){
sieve=new boolean[n];
Arrays.fill(sieve,true);
sieve[0]=false;
sieve[1]=false;
for(int i=2;i*i<n;i++){
if(sieve[i]){
for(int j=i*i;j<n;j+=i){
sieve[j]=false;
}
}
}
primes=new ArrayList<Integer>();
for(int i=0;i<n;i++){
if(sieve[i]){
primes.add(i);
}
}
}
/********************************************End***********************************************************/
}