-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdp-on-tree-type-c-LCA.cpp
110 lines (98 loc) · 1.85 KB
/
dp-on-tree-type-c-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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define int long long int
#define F first
#define S second
#define mod 1000000007
#define Max 200005
const int LG = 20;
void fast(){
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
}
vector<int> g[Max];
//LCA begins
//1 based index
int a[Max], lvl[Max], P[Max][LG];
void dfs(int u, int par){
lvl[u] = 1+lvl[par];
P[u][0] = par;
for(int i=0;i<g[u].size();i++){
int v=g[u][i];
if (v == par) continue;
dfs(v, u);
}
}
int lca(int u, int v){
int i, lg;
if (lvl[u] < lvl[v]) swap(u, v);
for(lg = 0; (1<<lg) <= lvl[u]; lg++);
lg--;
for(i=lg; i>=0; i--){
if (lvl[u] - (1<<i) >= lvl[v])
u = P[u][i];
}
if (u == v)
return u;
for(i=lg; i>=0; i--){
if (P[u][i] != -1 and P[u][i] != P[v][i])
u = P[u][i], v = P[v][i];
}
return P[u][0];
}
//Get the ancestor of node "u"
//which is "dis" distance above.
int getAncestor(int u, int dis){
dis = lvl[u] - dis;
int i, lg = 0;
for(; (1<<lg) <= lvl[u]; lg++); lg--;
for(i=lg; i>=0; i--){
if (lvl[u] - (1<<i) >= dis)
u = P[u][i];
}
return u;
}
//returns the distance between
//two nodes "u" and "v".
int dis(int u, int v){
if (lvl[u] < lvl[v]) swap(u, v);
int w = lca(u, v);
return lvl[u] + lvl[v] - 2*lvl[w];
}
main()
{
int i, n, q, m, j;
int u, v;
cin >> n;
for(i=0;i<n-1;i++){
cin>>u>>v;
g[u].pb(v);
g[v].pb(u);
}
for(i=0;i<LG;i++)
for(j=0;j<n+1;j++)
P[j][i]=-1;
lvl[0] = -1;
dfs(1, 0);
for(i=1; i<LG; i++){
for(j=1;j<n+1;j++){
if (P[j][i-1] != -1)
P[j][i] = P[P[j][i-1]][i-1];
}
}
//cout<<lca(5,2)<<" "<<lca(5,7)<<endl;
//cout<<dis(5,2)<<" "<<lca(5,7)<<endl;
return 0;
}
/*
8
1 2
1 3
1 4
3 5
3 7
4 6
6 8
*/