Skip to content

Commit 98772fc

Browse files
committed
solved(python): baekjoon 11438
1 parent 8bab8a1 commit 98772fc

4 files changed

Lines changed: 134 additions & 0 deletions

File tree

baekjoon/python/11438/__init__.py

Whitespace-only changes.

baekjoon/python/11438/main.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import itertools
2+
import sys
3+
from collections import defaultdict, deque
4+
5+
read = lambda: sys.stdin.readline().rstrip()
6+
7+
8+
class Problem:
9+
def __init__(self):
10+
self.n = int(read())
11+
12+
self.nodes = defaultdict(list[int])
13+
for src, dest in [map(lambda x: int(x) - 1, read().split()) for _ in range(self.n - 1)]:
14+
self.nodes[src].append(dest)
15+
self.nodes[dest].append(src)
16+
17+
self.queries = [map(lambda x: int(x) - 1, read().split()) for _ in range(int(read()))]
18+
self.depth, self.parent = self.make_tree()
19+
20+
def solve(self) -> None:
21+
for x, y in self.queries:
22+
print(self.lca(x, y) + 1)
23+
24+
def make_tree(self):
25+
stack, visited = deque([(0, 0)]), {0}
26+
depth, parent = [0 for _ in range(self.n)], [[0 for _ in range(17)] for _ in range(self.n)]
27+
28+
while stack:
29+
idx, node = stack.pop()
30+
depth[node] = idx
31+
32+
for next_node in self.nodes[node]:
33+
if next_node not in visited:
34+
stack.append((idx + 1, next_node))
35+
visited.add(next_node)
36+
parent[next_node][0] = node
37+
38+
for k, node in itertools.product(range(1, 17), range(self.n)):
39+
parent[node][k] = parent[parent[node][k - 1]][k - 1]
40+
41+
return depth, parent
42+
43+
def lca(self, x: int, y: int) -> int:
44+
if self.depth[x] < self.depth[y]:
45+
x, y = y, x
46+
47+
for k in range(16, -1, -1):
48+
if self.depth[x] - (1 << k) >= self.depth[y]:
49+
x = self.parent[x][k]
50+
51+
if x == y:
52+
return x
53+
54+
for k in range(16, -1, -1):
55+
if self.parent[x][k] != self.parent[y][k]:
56+
x, y = self.parent[x][k], self.parent[y][k]
57+
58+
return self.parent[x][0]
59+
60+
61+
if __name__ == "__main__":
62+
Problem().solve()

baekjoon/python/11438/sample.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[
2+
{
3+
"input": [
4+
"15",
5+
"1 2",
6+
"1 3",
7+
"2 4",
8+
"3 7",
9+
"6 2",
10+
"3 8",
11+
"4 9",
12+
"2 5",
13+
"5 11",
14+
"7 13",
15+
"10 4",
16+
"11 15",
17+
"12 5",
18+
"14 7",
19+
"6",
20+
"6 11",
21+
"10 9",
22+
"2 6",
23+
"7 6",
24+
"8 13",
25+
"8 15"
26+
],
27+
"expected": [
28+
"2",
29+
"4",
30+
"2",
31+
"1",
32+
"3",
33+
"1"
34+
]
35+
}
36+
]

baekjoon/python/11438/test_main.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import json
2+
import os.path
3+
import unittest
4+
from io import StringIO
5+
from unittest.mock import patch
6+
7+
from parameterized import parameterized
8+
9+
from main import Problem
10+
11+
12+
def load_sample(filename: str):
13+
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), filename)
14+
15+
with open(path, "r") as file:
16+
return [(case["input"], case["expected"]) for case in json.load(file)]
17+
18+
19+
class TestCase(unittest.TestCase):
20+
@parameterized.expand(load_sample("sample.json"))
21+
def test_case(self, case: str, expected: list[str]):
22+
# When
23+
with (
24+
patch("sys.stdin.readline", side_effect=case),
25+
patch("sys.stdout", new_callable=StringIO) as output,
26+
):
27+
Problem().solve()
28+
29+
result = output.getvalue().rstrip()
30+
31+
# Then
32+
self.assertEqual("\n".join(expected), result)
33+
34+
35+
if __name__ == "__main__":
36+
unittest.main()

0 commit comments

Comments
 (0)