This question evaluates proficiency in graph algorithms and data structures, focusing on lowest common ancestor computation in rooted trees and its generalization to directed acyclic graphs, along with complexity analysis for preprocessing and handling many queries.
You are given a rooted tree with n nodes labeled 0..n-1.
Input:
parent[i]
for each node
i
(
parent[root] = -1
).
u
and
v
.
Task:
u
and
v
.
Clarifications:
Follow-up (variant):
q
(up to ~1e5) for different pairs
(u, v)
efficiently.
Now the structure is a directed acyclic graph (DAG) where edges point from a node to its parent(s) (a node may have multiple parents).
Input:
parents[i]
: list of parent nodes of
i
(possibly empty).
u
and
v
.
Task:
u
and
v
in the DAG, defined as:
u
and
v
, and
Notes:
n
up to ~2e5, edges up to ~5e5.