Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
competitive
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Harbor Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bruno Freitas Tissei
competitive
Commits
a7543847
Commit
a7543847
authored
7 years ago
by
Bruno Freitas Tissei
Browse files
Options
Downloads
Patches
Plain Diff
Add LCA
Signed-off-by:
Bruno Freitas Tissei
<
bft15@inf.ufpr.br
>
parent
bba9ae81
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
graph/lca.cpp
+78
-0
78 additions, 0 deletions
graph/lca.cpp
with
78 additions
and
0 deletions
graph/lca.cpp
0 → 100644
+
78
−
0
View file @
a7543847
/**
* Lowest Common Ancestor - LCA
*
* Complexity (Time):
* preprocess -> O(n log n)
* query -> O(log n)
* Complexity (Space): O(n + m + n log n)
*
*
* OBS: * = return sum path to LCA
* ** = return max value on path to LCA
*/
vector
<
ii
>
graph
[
MAX
];
int
h
[
MAX
];
int
par
[
MAX
][
MAXLOG
],
cost
[
MAX
][
MAXLOG
];
// Perform DFS while filling h, par, and cost
void
dfs
(
int
v
,
int
p
=
-
1
,
int
c
=
0
)
{
par
[
v
][
0
]
=
p
;
//*** cost[v][0] = c;
if
(
p
+
1
)
h
[
v
]
=
h
[
p
]
+
1
;
for
(
int
i
=
1
;
i
<
MAXLOG
;
i
++
)
if
(
par
[
v
][
i
-
1
]
+
1
)
{
par
[
v
][
i
]
=
par
[
par
[
v
][
i
-
1
]][
i
-
1
];
//*** cost[v][i] += cost[v][i - 1] + cost[par[v][i - 1]][i - 1];
}
for
(
auto
u
:
graph
[
v
])
if
(
p
!=
u
.
fi
)
dfs
(
u
.
fi
,
v
,
u
.
se
);
}
// Preprocess tree with rooted at v
void
preprocess
(
int
v
)
{
memset
(
par
,
-
1
,
sizeof
par
);
memset
(
cost
,
0
,
sizeof
cost
);
dfs
(
v
);
}
// Return LCA between p and q
int
query
(
int
p
,
int
q
)
{
//*** int ans = 0;
if
(
h
[
p
]
<
h
[
q
])
swap
(
p
,
q
);
for
(
int
i
=
MAXLOG
-
1
;
i
>=
0
;
i
--
)
if
(
par
[
p
][
i
]
+
1
&&
h
[
par
[
p
][
i
]]
>=
h
[
q
])
{
//* ans += cost[p][i];
//** ans = max(ans, cost[p][i]);
p
=
par
[
p
][
i
];
}
if
(
p
==
q
)
return
p
;
//*** return ans;
for
(
int
i
=
MAXLOG
-
1
;
i
>=
0
;
i
--
)
if
(
par
[
p
][
i
]
+
1
&&
par
[
p
][
i
]
!=
par
[
q
][
i
])
{
//* ans += cost[p][i] + cost[q][i];
//** ans = max(ans, max(cost[p][i], cost[q][i]));
p
=
par
[
p
][
i
];
q
=
par
[
q
][
i
];
}
return
dp
[
p
][
0
];
//* if (p == q) return ans;
//* else return ans + cost[p][0] + cost[q][0];
//** if (p == q) return ans;
//** else return max(ans, max(cost[p][0], cost[q][0]));
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment