forked from glapul/contest_library
-
Notifications
You must be signed in to change notification settings - Fork 59
/
lca.cpp
88 lines (87 loc) · 2.07 KB
/
lca.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
#include<algorithm>
#include<vector>
using namespace std;
class LCATree
{
public:
int lca(int u,int v)
{
if(!ready)
compute();
int last=-1;
while(u!=v)
{
if(d[u]>d[v])
swap(u,v);
if(skip[v]!=v)
{
last=v;
v=skip[v];
}
else
{
last=-1;
v=parent[v];
}
}
return last==-1 ?v : last;
}
LCATree(int n, int root):n(n),root(root)
{
graph = vector<vector<int> > (n)
parent = vector<int> (n)
parent[root]=-1;
d = vector<int> (n);
size = vector<int> (n);
skip = vector<int> (n);
ready=false;
}
void add_edge(int u, int v)
{
graph[u].push_back(v);
graph[v].push_back(u);
ready=false;
}
void set_root(int newroot)
{
root=newroot;
ready=false;
parent[root]=-1;
}
private:
vector<vector<int> > graph;
vector<int> parent;
vector<int> d;
vector<int> size;
vector<int> skip;
int root,n;
bool ready;
int dfs(int v)
{
size[v]=1;
FOREACH(i,graph[v])
if(*i!=parent[v])
{
parent[*i]=v;
d[*i]=d[v]+1;
size[*i]=dfs(*i);
size[v]+=size[*i];
}
return size[v];
}
void skipdfs(int v)
{
skip[v]=v;
if(v!=root && 2*size[v] > size[parent[v]])
skip[v]=skip[parent[v]];
FOREACH(i,graf[v])
if(*i!=parent[v])
skipdfs(*i);
}
void compute()
{
ready=true;
dfs(root);
skipdfs(root);
}
};