-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLCA.cpp
76 lines (63 loc) · 1.67 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
/// lca used to solve https://codeforces.com/problemset/problem/1583/E
#include<bits/stdc++.h>
using namespace std;
#define MP make_pair
#define PB push_back
#define nn '\n'
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define UNIQUE(vec) vec.resize(distance(vec.begin(),unique(vec.begin(),vec.end()))) ;
#define all(vec) vec.begin(),vec.end()
#define int long long
#define pii pair<int,int>
typedef long long LL ;
const int MOD=1e9+7,Base=998244353 ;
const int N=1e6+7 ;
const int INF=1LL*1000*1000*1000*1000*1000*1000+7LL, INF2=(1LL<<62) ;
const double pie=acos(-1.0) ;
const double EPS=1e-9 ;
int n , test , anc[N][20] , d[N] ;
vector<int>adj[N] ;
void dfs(int nd,int pa=-1,int c=0)
{
d[nd]=c ;
if(pa!=-1)
{
anc[nd][0]=pa ;
for(int j=1,md; j<20; ++j)
md=anc[nd][j-1], anc[nd][j]=anc[md][j-1] ;
}
for(int j=0,ch; j<adj[nd].size(); ++j)
{
ch=adj[nd][j] ;
if(ch==pa)
continue ;
dfs(ch,nd,c+1) ;
}
}
int lca(int u,int v)
{
if(d[u]<d[v])
swap(u,v) ;
for(int j=19;j>=0 and d[u]>d[v] ;--j) /// u niche ...
{
if(anc[u][j]==0)continue ;
if(d[anc[u][j]]>=d[v])
u=anc[u][j] ;
}
for(int j=19;j>=0 and u!=v; --j)
{
if(anc[u][j]!=anc[v][j])
u=anc[u][j], v=anc[v][j] ;
u=anc[u][0], v=anc[v][0] ;
}
return u ;
}
int32_t main()
{
IOS
cin>>n>>m ;
dfs(1,-1,0) ;
lca(u,v) ;
return 0 ;
}