[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
This commit is contained in:
pre-commit-ci[bot] 2025-01-13 22:02:43 +00:00
parent 419ccccf24
commit 18eed567e6

View File

@ -22,21 +22,21 @@ def swap(a: int, b: int) -> tuple[int, int]:
def create_sparse(max_node: int, parent: list[list[int]]) -> list[list[int]]: def create_sparse(max_node: int, parent: list[list[int]]) -> list[list[int]]:
""" """
Create a sparse table that saves each node's 2^i-th parent. Create a sparse table that saves each node's 2^i-th parent.
The given ``parent`` table should have the direct parent of each node in row 0. The given ``parent`` table should have the direct parent of each node in row 0.
This function fills in: This function fills in:
parent[j][i] = parent[j - 1][parent[j - 1][i]] parent[j][i] = parent[j - 1][parent[j - 1][i]]
for each j where 2^j is less than max_node. for each j where 2^j is less than max_node.
For example, consider a small tree where: For example, consider a small tree where:
- Node 1 is the root (its parent is 0), - Node 1 is the root (its parent is 0),
- Nodes 2 and 3 have parent 1. - Nodes 2 and 3 have parent 1.
We set up the parent table for only two levels (row 0 and row 1) We set up the parent table for only two levels (row 0 and row 1)
for max_node = 3. (Note that in practice the table has many rows.) for max_node = 3. (Note that in practice the table has many rows.)
>>> parent0 = [0, 0, 1, 1] # 0 is unused; node1's parent=0, nodes 2 and 3's parent=1. >>> parent0 = [0, 0, 1, 1] # 0 is unused; node1's parent=0, nodes 2 and 3's parent=1.
>>> parent1 = [0, 0, 0, 0] >>> parent1 = [0, 0, 0, 0]
>>> parent = [parent0, parent1] >>> parent = [parent0, parent1]
@ -58,11 +58,11 @@ def lowest_common_ancestor(
""" """
Return the lowest common ancestor (LCA) of nodes u and v in a tree. Return the lowest common ancestor (LCA) of nodes u and v in a tree.
<<<<<<< HEAD <<<<<<< HEAD
The lists ``level`` and ``parent`` must be precomputed. ``level[i]`` is the depth The lists ``level`` and ``parent`` must be precomputed. ``level[i]`` is the depth
of node i, and ``parent`` is a sparse table where parent[0][i] is the direct parent of node i, and ``parent`` is a sparse table where parent[0][i] is the direct parent
of node i. of node i.
======= =======
The lists `level` and `parent` must be precomputed. `level[i]` is the depth of node i, The lists `level` and `parent` must be precomputed. `level[i]` is the depth of node i,
@ -107,10 +107,10 @@ def breadth_first_search(
) -> tuple[list[int], list[list[int]]]: ) -> tuple[list[int], list[list[int]]]:
""" """
Run a breadth-first search (BFS) from the root node of the tree. Run a breadth-first search (BFS) from the root node of the tree.
This sets each node's direct parent (stored in parent[0]) and calculates the This sets each node's direct parent (stored in parent[0]) and calculates the
depth (level) of each node from the root. depth (level) of each node from the root.
>>> # Consider a simple tree: >>> # Consider a simple tree:
>>> # 1 >>> # 1
>>> # / \\ >>> # / \\
@ -141,18 +141,18 @@ def main() -> None:
""" """
Run a BFS to set node depths and parents in a sample tree, then create the Run a BFS to set node depths and parents in a sample tree, then create the
sparse table and compute several lowest common ancestors. sparse table and compute several lowest common ancestors.
The sample tree used is: The sample tree used is:
<<<<<<< HEAD <<<<<<< HEAD
1 1
/ | \ / | \
2 3 4 2 3 4
/ / \\ \\ / / \\ \\
5 6 7 8 5 6 7 8
/ \\ | / \\ / \\ | / \\
9 10 11 12 13 9 10 11 12 13
======= =======
1 1
@ -171,7 +171,7 @@ def main() -> None:
- LCA(6, 7) --> 3 - LCA(6, 7) --> 3
- LCA(4, 12) --> 4 - LCA(4, 12) --> 4
- LCA(8, 8) --> 8 - LCA(8, 8) --> 8
To test main() without it printing to the console, we capture the output. To test main() without it printing to the console, we capture the output.
>>> import sys >>> import sys