forked from glapul/contest_library
-
Notifications
You must be signed in to change notification settings - Fork 59
/
lcahldc.cpp
77 lines (75 loc) · 1.37 KB
/
lcahldc.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
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
#define FOREACH(i,c) for(__typeof((c).begin()) i = (c).begin();i!=(c).end();i++)
const int MAXN = 100007;
vector<int> graf [MAXN];
int parent[MAXN];
int skip[MAXN];
int depth[MAXN];
int size[MAXN];
int dfs(int v)
{
size[v]=1;
FOREACH(i,graf[v])
if(*i!=parent[v])
{
parent[*i]=v;
depth[*i]=depth[v]+1;
size[*i] = dfs(*i);
size[v]+=size[*i];
}
return size[v];
}
void skipdfs(int v)
{
skip[v]=v;
if(v!=0 && 2*size[v] > size[parent[v]])
skip[v] = skip[parent[v]];
FOREACH(i,graf[v])
if(*i!=parent[v])
skipdfs(*i);
}
int lca (int u, int v)
{
int last=-1;
while(u!=v)
{
if(depth[u] > depth[v])
swap(u,v);
if(skip[v]!=v)
{
last=v;
v=skip[v];
}
else
{
last=-1;
v=parent[v];
}
}
return last == -1 ?v :last;
}
int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<n-1;i++)
{
int a,b;
scanf("%d %d",&a,&b);
graf[a].push_back(b);
graf[b].push_back(a);
}
dfs(0);
skipdfs(0);
int q;
scanf("%d",&q);
for(int i =0;i<q;i++)
{
int a,b;
scanf("%d %d",&a,&b);
printf("%d\n",lca(a,b));
}
}