Skip to content

Commit 281e815

Browse files
committed
solved(python): baekjoon 15681
1 parent c8e6479 commit 281e815

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed

baekjoon/python/15681/__init__.py

Whitespace-only changes.

baekjoon/python/15681/main.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import sys
2+
from collections import defaultdict, deque
3+
4+
read = lambda: sys.stdin.readline().rstrip()
5+
6+
7+
class Problem:
8+
def __init__(self):
9+
self.n, self.r, self.q = map(int, read().split())
10+
11+
self.edges = defaultdict(list)
12+
for _ in range(self.n - 1):
13+
x, y = map(int, read().split())
14+
self.edges[x].append(y)
15+
self.edges[y].append(x)
16+
17+
self.queries = [int(read()) for _ in range(self.q)]
18+
19+
def solve(self) -> None:
20+
children = self.find_children(self.r)
21+
22+
for query in self.queries:
23+
print(children[query])
24+
25+
def find_children(self, num: int) -> list[int]:
26+
stack, cached = deque([(num, -1)]), []
27+
28+
while stack:
29+
node, head = stack.pop()
30+
cached.append((node, head))
31+
32+
for child in self.edges[node]:
33+
if child != head:
34+
stack.append((child, node))
35+
36+
children = [1] * (self.n + 1)
37+
for node, head in reversed(cached):
38+
if head != -1:
39+
children[head] += children[node]
40+
41+
return children
42+
43+
44+
if __name__ == "__main__":
45+
Problem().solve()

baekjoon/python/15681/sample.json

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

baekjoon/python/15681/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)