Skip to content

Commit 9a6a460

Browse files
committed
solved(python): baekjoon 1043
1 parent 68ec25a commit 9a6a460

File tree

4 files changed

+169
-0
lines changed

4 files changed

+169
-0
lines changed

baekjoon/python/1043/__init__.py

Whitespace-only changes.

baekjoon/python/1043/main.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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.known = set(map(int, read().split()[1:]))
10+
self.parties = [list(map(int, read().split()[1:])) for _ in range(self.m)]
11+
12+
self.parent, self.rank = list(range(self.n + 1)), [0] * (self.n + 1)
13+
14+
def solve(self) -> None:
15+
[self.union(party[0], person) for party in self.parties for person in party[1:]]
16+
17+
truth = {self.find(person) for person in self.known}
18+
print(len([party for party in self.parties if all(self.find(person) not in truth for person in party)]))
19+
20+
def find(self, person: int) -> int:
21+
if self.parent[person] != person:
22+
self.parent[person] = self.find(self.parent[person])
23+
24+
return self.parent[person]
25+
26+
def union(self, x: int, y: int) -> None:
27+
x_root, y_root = self.find(x), self.find(y)
28+
if x_root == y_root:
29+
return
30+
31+
if self.rank[x_root] > self.rank[y_root]:
32+
self.parent[y_root] = x_root
33+
elif self.rank[x_root] < self.rank[y_root]:
34+
self.parent[x_root] = y_root
35+
else:
36+
self.parent[y_root], self.rank[x_root] = x_root, 1
37+
38+
39+
if __name__ == "__main__":
40+
Problem().solve()

baekjoon/python/1043/sample.json

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
[
2+
{
3+
"input": [
4+
"4 3",
5+
"0",
6+
"2 1 2",
7+
"1 3",
8+
"3 2 3 4"
9+
],
10+
"expected": [
11+
"3"
12+
]
13+
},
14+
{
15+
"input": [
16+
"4 1",
17+
"1 1",
18+
"4 1 2 3 4"
19+
],
20+
"expected": [
21+
"0"
22+
]
23+
},
24+
{
25+
"input": [
26+
"4 1",
27+
"0",
28+
"4 1 2 3 4"
29+
],
30+
"expected": [
31+
"1"
32+
]
33+
},
34+
{
35+
"input": [
36+
"4 5",
37+
"1 1",
38+
"1 1",
39+
"1 2",
40+
"1 3",
41+
"1 4",
42+
"2 4 1"
43+
],
44+
"expected": [
45+
"2"
46+
]
47+
},
48+
{
49+
"input": [
50+
"10 9",
51+
"4 1 2 3 4",
52+
"2 1 5",
53+
"2 2 6",
54+
"1 7",
55+
"1 8",
56+
"2 7 8",
57+
"1 9",
58+
"1 10",
59+
"2 3 10",
60+
"1 4"
61+
],
62+
"expected": [
63+
"4"
64+
]
65+
},
66+
{
67+
"input": [
68+
"8 5",
69+
"3 1 2 7",
70+
"2 3 4",
71+
"1 5",
72+
"2 5 6",
73+
"2 6 8",
74+
"1 8"
75+
],
76+
"expected": [
77+
"5"
78+
]
79+
},
80+
{
81+
"input": [
82+
"3 4",
83+
"1 3",
84+
"1 1",
85+
"1 2",
86+
"2 1 2",
87+
"3 1 2 3"
88+
],
89+
"expected": [
90+
"0"
91+
]
92+
}
93+
]

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