Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6918550

Browse files
Find LCA in O(sqrt(n))
1 parent e744e99 commit 6918550

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed

‎LCA.cpp

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
* Deleted code is debugged code :)
3+
*/
4+
#include <bits/stdc++.h>
5+
#define ll long long
6+
#define ull unsigned long long
7+
#define vi vector<ll>
8+
#define pp pair<ll,ll>
9+
#define mp make_pair
10+
#define PI acos(-1.0)
11+
#define all(v) v.begin(),v.end()
12+
#define pb push_back
13+
#define FOR(i,a,b) for(i=a;i<b;i++)
14+
#define FREV(i,a,b) for(i=a;i>=b;i--)
15+
#define SULL(n) scanf("%llu", &n)
16+
#define INF 1e18
17+
#define MOD 1000000007
18+
19+
#ifndef ONLINE_JUDGE
20+
#define gc getchar
21+
#define pc putchar
22+
#else
23+
#define gc getchar_unlocked
24+
#define pc putchar_unlocked
25+
#endif
26+
27+
using namespace std;
28+
29+
int read_int() {
30+
char c = gc();
31+
while((c < '0' || c > '9') && c != '-') c = gc();
32+
int ret = 0, neg = 0;
33+
if (c == '-') neg = 1, c = gc();
34+
while(c >= '0' && c <= '9') {
35+
ret = 10 * ret + c - 48;
36+
c = gc();
37+
}
38+
return neg ? -ret : ret;
39+
}
40+
41+
ll read_ll() {
42+
char c = gc();
43+
while((c < '0' || c > '9') && c != '-') c = gc();
44+
ll ret = 0;
45+
int neg = 0;
46+
if (c == '-') neg = 1, c = gc();
47+
while(c >= '0' && c <= '9') {
48+
ret = 10 * ret + c - 48;
49+
c = gc();
50+
}
51+
return neg ? -ret : ret;
52+
}
53+
54+
/*******************************************RANDOM STUFF BEGINS HERE**************************************************/
55+
56+
struct node {
57+
vi adj;
58+
};
59+
60+
vector<node> tree;
61+
vi depth(100005), parent(100005), section_ancestor(100005);
62+
ll maxdepth = 0;
63+
// DFS to calculate depths of each node in O(n)
64+
void dfs(ll u) {
65+
ll i, len = tree[u].adj.size();
66+
FOR(i,0,len) {
67+
ll v = tree[u].adj[i];
68+
depth[v] = depth[u] + 1;
69+
if (depth[v] > maxdepth) {
70+
maxdepth = depth[v];
71+
}
72+
dfs(v);
73+
}
74+
}
75+
76+
// LCA preprocessing in O(n)
77+
// Divide the tree in sqrt(H) sections where H = height of the tree
78+
// section_ancestor[i] stores the ancestor of node i in the last level of upper section
79+
void LCA_preprocessing(ll n) {
80+
ll i, sqrtH = int(sqrt(maxdepth));
81+
82+
FOR(i,1,n+1) {
83+
if (i < sqrtH) {
84+
section_ancestor[i] = 1;
85+
}
86+
else if (depth[i] % sqrtH == 0) {
87+
section_ancestor[i] = parent[i];
88+
}
89+
else {
90+
section_ancestor[i] = section_ancestor[parent[i]];
91+
}
92+
}
93+
}
94+
95+
// Returns LCA of x and y in O(sqrt(n))
96+
ll getLCA(ll x, ll y) {
97+
cout << "x = "<< x <<" y = "<< y << endl;
98+
while (section_ancestor[x] != section_ancestor[y]) {
99+
if (depth[x] > depth[y]) {
100+
x = section_ancestor[x];
101+
}
102+
else {
103+
y = section_ancestor[y];
104+
}
105+
}
106+
107+
// x and y are now in the same section
108+
while (x != y) {
109+
if (depth[x] > depth[y]) {
110+
x = parent[x];
111+
}
112+
else {
113+
y = parent[y];
114+
}
115+
}
116+
117+
return x;
118+
}
119+
120+
int main(int argc, char *argv[]) {
121+
ll i,j,t,n,x,y;
122+
n = read_ll();
123+
tree.resize(n+1);
124+
// 1 is the root. Accept parents of the remaining (n-1) nodes
125+
FOR(i,2,n+1) {
126+
j = read_ll();
127+
tree[j].adj.pb(i);
128+
parent[i] = j;
129+
}
130+
dfs(1);
131+
// cout << "DFS complete ----> maxdepth = " << maxdepth << endl;
132+
LCA_preprocessing(n);
133+
// cout << "LCA preprocessing done" << endl;
134+
// FOR(i,1,n+1) {
135+
// cout << "section_ancestor[" << i <<"] = " << section_ancestor[i] << endl;
136+
// }
137+
x = read_ll();
138+
y = read_ll();
139+
// cout << "LCA of " << x << " and " << y << " = " << getLCA(x,y) << endl;
140+
return 0;
141+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /