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 952de7c

Browse files
Fix logical bug to find LCA in O(sqrt(n))
1 parent 6918550 commit 952de7c

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

‎LCA.cpp

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -59,37 +59,41 @@ struct node {
5959

6060
vector<node> tree;
6161
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) {
62+
ll sqrtH = 0, maxdepth = 0;
63+
64+
void find_maxdepth(ll u) {
6565
ll i, len = tree[u].adj.size();
6666
FOR(i,0,len) {
6767
ll v = tree[u].adj[i];
6868
depth[v] = depth[u] + 1;
6969
if (depth[v] > maxdepth) {
7070
maxdepth = depth[v];
7171
}
72-
dfs(v);
72+
find_maxdepth(v);
7373
}
7474
}
7575

76-
// LCA preprocessing in O(n)
76+
// LCA preprocessing with DFS in O(n)
7777
// Divide the tree in sqrt(H) sections where H = height of the tree
7878
// 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));
79+
void dfs(ll u) {
80+
ll i, v;
81+
if (depth[u] < sqrtH) {
82+
section_ancestor[u] = 1;
83+
}
84+
else if (depth[u] % sqrtH == 0) {
85+
section_ancestor[u] = parent[u];
86+
}
87+
else {
88+
section_ancestor[u] = section_ancestor[parent[u]];
89+
}
8190

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-
}
91+
ll len = tree[u].adj.size();
92+
FOR(i,0,len) {
93+
v = tree[u].adj[i];
94+
dfs(v);
9295
}
96+
9397
}
9498

9599
// Returns LCA of x and y in O(sqrt(n))
@@ -127,15 +131,14 @@ int main(int argc, char *argv[]) {
127131
tree[j].adj.pb(i);
128132
parent[i] = j;
129133
}
134+
find_maxdepth(1);
135+
sqrtH = int(sqrt(maxdepth));
130136
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+
FOR(i,1,n+1) {
138+
cout << "section_ancestor[" << i <<"] = " << section_ancestor[i] << endl;
139+
}
137140
x = read_ll();
138141
y = read_ll();
139-
//cout << "LCA of " << x << " and " << y << " = " << getLCA(x,y) << endl;
142+
cout << "LCA of " << x << " and " << y << " = " << getLCA(x,y) << endl;
140143
return 0;
141144
}

0 commit comments

Comments
(0)

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