Skip to content

Commit 027769d

Browse files
committed
solved(python): baekjoon 11281
1 parent 6893c71 commit 027769d

File tree

4 files changed

+163
-0
lines changed

4 files changed

+163
-0
lines changed

baekjoon/python/11281/__init__.py

Whitespace-only changes.

baekjoon/python/11281/main.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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) -> None:
9+
self.n, self.m = map(int, read().split())
10+
11+
self.graph = defaultdict(list)
12+
for x, y in [map(int, read().split()) for _ in range(self.m)]:
13+
self.graph[self.literal_to_node(-x)].append(self.literal_to_node(y))
14+
self.graph[self.literal_to_node(-y)].append(self.literal_to_node(x))
15+
16+
def literal_to_node(self, literal: int) -> int:
17+
if literal > 0:
18+
return literal
19+
20+
return -literal + self.n
21+
22+
def solve(self) -> None:
23+
discovered, low, scc_id, on_stack, scc_stack, order, scc_counter = (
24+
[-1] * (2 * self.n + 1),
25+
[-1] * (2 * self.n + 1),
26+
[-1] * (2 * self.n + 1),
27+
[False] * (2 * self.n + 1),
28+
deque(),
29+
0,
30+
0,
31+
)
32+
33+
for node in range(1, 2 * self.n + 1):
34+
if discovered[node] == -1:
35+
order, scc_counter = self.tarjan_scc(
36+
node,
37+
discovered,
38+
low,
39+
scc_id,
40+
on_stack,
41+
scc_stack,
42+
order,
43+
scc_counter,
44+
)
45+
46+
for idx in range(1, self.n + 1):
47+
if scc_id[idx] == scc_id[idx + self.n]:
48+
print(0)
49+
return
50+
51+
print(1)
52+
print(*[0 if scc_id[idx] > scc_id[idx + self.n] else 1 for idx in range(1, self.n + 1)])
53+
54+
def tarjan_scc(
55+
self,
56+
start_node: int,
57+
discovered: list[int],
58+
low: list[int],
59+
scc_id: list[int],
60+
on_stack: list[bool],
61+
scc_stack: deque,
62+
order: int,
63+
scc_counter: int,
64+
) -> tuple[int, int]:
65+
stack = deque([(start_node, iter(self.graph[start_node]))])
66+
67+
while stack:
68+
current_node, neighbors = stack[-1]
69+
70+
if discovered[current_node] == -1:
71+
order += 1
72+
discovered[current_node] = low[current_node] = order
73+
scc_stack.append(current_node)
74+
on_stack[current_node] = True
75+
76+
for neighbor in neighbors:
77+
if discovered[neighbor] == -1:
78+
stack.append((neighbor, iter(self.graph[neighbor])))
79+
break
80+
elif on_stack[neighbor]:
81+
low[current_node] = min(low[current_node], discovered[neighbor])
82+
else:
83+
popped_node, _ = stack.pop()
84+
85+
if low[popped_node] == discovered[popped_node]:
86+
scc_counter += 1
87+
while True:
88+
node_in_scc = scc_stack.pop()
89+
on_stack[node_in_scc] = False
90+
scc_id[node_in_scc] = scc_counter
91+
if node_in_scc == popped_node:
92+
break
93+
94+
if stack:
95+
parent_node, _ = stack[-1]
96+
low[parent_node] = min(low[parent_node], low[popped_node])
97+
98+
return order, scc_counter
99+
100+
101+
if __name__ == "__main__":
102+
Problem().solve()

baekjoon/python/11281/sample.json

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

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