Skip to content

Commit d4ca293

Browse files
committed
solved(python): baekjoon 3830
1 parent 6d8c011 commit d4ca293

4 files changed

Lines changed: 119 additions & 0 deletions

File tree

baekjoon/python/3830/__init__.py

Whitespace-only changes.

baekjoon/python/3830/main.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import sys
2+
3+
read = lambda: sys.stdin.readline().rstrip()
4+
5+
6+
class Problem:
7+
def __init__(self):
8+
self.n, self.m = map(int, read().split())
9+
self.queries = []
10+
for line in [read().split() for _ in range(self.m)]:
11+
self.queries.append((line[0] == "?", list(map(int, line[1:]))))
12+
13+
self.parent, self.weight = list(range(self.n + 1)), [0] * (self.n + 1)
14+
15+
def solve(self) -> None:
16+
for is_query, args in self.queries:
17+
if not is_query:
18+
self.union(*args)
19+
continue
20+
21+
x, y = args
22+
print("UNKNOWN" if self.find(x) != self.find(y) else self.weight[y] - self.weight[x])
23+
24+
def find(self, num: int) -> int:
25+
path = []
26+
while self.parent[num] != num:
27+
path.append(num)
28+
num = self.parent[num]
29+
30+
for node in reversed(path):
31+
self.weight[node] += self.weight[self.parent[node]]
32+
self.parent[node] = num
33+
34+
return num
35+
36+
def union(self, x: int, y: int, weight: int) -> None:
37+
root_x, root_y = self.find(x), self.find(y)
38+
if root_x != root_y:
39+
self.parent[root_y] = root_x
40+
self.weight[root_y] = self.weight[x] - self.weight[y] + weight
41+
42+
43+
if __name__ == "__main__":
44+
while True:
45+
try:
46+
Problem().solve()
47+
except StopIteration:
48+
break
49+
except (EOFError, ValueError):
50+
break

baekjoon/python/3830/sample.json

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

baekjoon/python/3830/test_main.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
while True:
28+
try:
29+
Problem().solve()
30+
except StopIteration:
31+
break
32+
33+
result = output.getvalue().rstrip()
34+
35+
# Then
36+
self.assertEqual("\n".join(expected), result)
37+
38+
39+
if __name__ == "__main__":
40+
unittest.main()

0 commit comments

Comments
 (0)