Skip to content

Commit 5ad92b9

Browse files
committed
solved(python): baekjoon 11266
1 parent 79eaa45 commit 5ad92b9

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed

baekjoon/python/11266/__init__.py

Whitespace-only changes.

baekjoon/python/11266/main.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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.v, self.e = map(int, read().split())
10+
11+
self.graph = defaultdict(list[int])
12+
for x, y in [map(int, read().split()) for _ in range(self.e)]:
13+
self.graph[x].append(y)
14+
self.graph[y].append(x)
15+
16+
def solve(self) -> None:
17+
discovered, low, points, order = (
18+
[-1] * (self.v + 1),
19+
[-1] * (self.v + 1),
20+
set(),
21+
0,
22+
)
23+
24+
for node in range(1, self.v + 1):
25+
if discovered[node] == -1:
26+
order = self.dfs(node, discovered, low, points, order)
27+
28+
print(len(points))
29+
print(*sorted(list(points)))
30+
31+
def dfs(
32+
self,
33+
start_node: int,
34+
discovered: list[int],
35+
low: list[int],
36+
points: set[int],
37+
order: int,
38+
) -> int:
39+
stack, root_children_count = deque([(start_node, 0, iter(self.graph[start_node]))]), 0
40+
41+
while stack:
42+
current_node, parent, neighbors = stack[-1]
43+
44+
if discovered[current_node] == -1:
45+
order += 1
46+
discovered[current_node] = low[current_node] = order
47+
48+
for neighbor in neighbors:
49+
if neighbor == parent:
50+
continue
51+
52+
if discovered[neighbor] != -1:
53+
low[current_node] = min(low[current_node], discovered[neighbor])
54+
else:
55+
if parent == 0:
56+
root_children_count += 1
57+
stack.append((neighbor, current_node, iter(self.graph[neighbor])))
58+
break
59+
else:
60+
stack.pop()
61+
if parent != 0:
62+
low[parent] = min(low[parent], low[current_node])
63+
64+
if parent != start_node and low[current_node] >= discovered[parent]:
65+
points.add(parent)
66+
67+
if root_children_count > 1:
68+
points.add(start_node)
69+
70+
return order
71+
72+
73+
if __name__ == "__main__":
74+
Problem().solve()

baekjoon/python/11266/sample.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[
2+
{
3+
"input": [
4+
"7 7",
5+
"1 4",
6+
"4 5",
7+
"5 1",
8+
"1 6",
9+
"6 7",
10+
"2 7",
11+
"7 3"
12+
],
13+
"expected": [
14+
"3",
15+
"1 6 7"
16+
]
17+
}
18+
]

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