๋ฌธ์
https://www.acmicpc.net/problem/1043
ํ์ด
# ๋ฐฑ์ค 1043๋ฒ ๋ฌธ์ - ๊ฑฐ์ง๋ง
import sys
n, m = list(map(int, sys.stdin.readline().split()))
truth = list(map(int, sys.stdin.readline().split()))[1:]
KNOW_TRUTH = 0
parent = [i for i in range(n + 1)]
for person in truth:
parent[person] = KNOW_TRUTH
def find(x):
if parent[x] != x:
parent[x] = find(parent[x])
return parent[x]
def union(a, b):
a = find(a)
b = find(b)
if a < b:
parent[b] = a
else:
parent[a] = b
parties = []
for _ in range(m):
party = list(map(int, sys.stdin.readline().split()))[1:]
for i in range(len(party) - 1):
union(party[i], party[i + 1])
parties.append(party)
answer = 0
for party in parties:
know = False
for i in range(len(party)):
if find(party[i]) == KNOW_TRUTH:
know = True
break
if not know:
answer += 1
print(answer)
์ ๋์จ ํ์ธ๋ ์๊ณ ๋ฆฌ์ฆ์ ์ฌ์ฉํ ํ์ด์ด๋ค.
'๐๏ธ Algorithm > ๐ฉ ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐ฉ [๋ฐฑ์ค] [Python] [Gold5] 13219๋ฒ_Trains (0) | 2023.05.14 |
---|---|
๐ฉ [๋ฐฑ์ค] [Python] [Gold2] 12015๋ฒ_๊ฐ์ฅ ๊ธด ์ฆ๊ฐํ๋ ๋ถ๋ถ ์์ด 2 (0) | 2023.05.12 |
๐ฉ [๋ฐฑ์ค] [Python] [Gold5] 13023๋ฒ_ABCDE (0) | 2023.05.11 |
๐ฉ [๋ฐฑ์ค] [Python] [Gold3] 1238๋ฒ_ํํฐ (1) | 2023.05.09 |
๐ฉ [๋ฐฑ์ค] [Python] [Gold4] 11054๋ฒ_๊ฐ์ฅ ๊ธด ๋ฐ์ดํ ๋ ๋ถ๋ถ ์์ด (1) | 2023.05.09 |