https://www.acmicpc.net/problem/2606
2606๋ฒ: ๋ฐ์ด๋ฌ์ค
์ฒซ์งธ ์ค์๋ ์ปดํจํฐ์ ์๊ฐ ์ฃผ์ด์ง๋ค. ์ปดํจํฐ์ ์๋ 100 ์ดํ์ด๊ณ ๊ฐ ์ปดํจํฐ์๋ 1๋ฒ ๋ถํฐ ์ฐจ๋ก๋๋ก ๋ฒํธ๊ฐ ๋งค๊ฒจ์ง๋ค. ๋์งธ ์ค์๋ ๋คํธ์ํฌ ์์์ ์ง์ ์ฐ๊ฒฐ๋์ด ์๋ ์ปดํจํฐ ์์ ์๊ฐ ์ฃผ์ด
www.acmicpc.net
#boss์ฐพ๊ธฐ
def findboss(group,member):
if group[member]!=member:
group[member]=findboss(group,group[member])
return group[member]
#union ์ํค๊ธฐ
def union(group,a,b):
fa,fb = findboss(group,a),findboss(group,b)
#์ซ์๊ฐ ์์๊ฑธ ๊ธฐ์ค์ผ๋ก union ์์ผ์ฃผ๊ธฐ ์ํด
if fa<fb:
group[fb] = fa
else:
group[fa]=fb
n = int(input())
k = int(input())
#์์ ์ ๋ณด์ค๋ก ๋ฃ์ด์ฃผ๊ธฐ!
group = [i for i in range(n+1)]
for i in range(k):
a,b = map(int,input().split())
#group์ ๋งค๊ฐ๋ณ์๋ก ๊ฐ์ด ๋ฃ์ด์ฃผ๊ธฐ
union(group,a,b)
cnt=0
for g in range(1,len(group)):
#boss๊ฐ 1๋ฒ ์ปดํจํฐ์ธ๊ฑฐ ์ฐพ๊ธฐ!
if findboss(group,g) == 1:
cnt+=1
print(cnt-1)
'Problem Solving > BAEKJOON' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[baekjoon]python #2636 ์น์ฆ (0) | 2022.05.10 |
---|---|
[baekjoon]python #2469 ์ฌ๋ค๋ฆฌ ํ๊ธฐ (0) | 2022.05.03 |
[baekjoon]python #2589 ๋ณด๋ฌผ์ฌ (0) | 2022.04.26 |
[baekjoon]python #19949 ์์ฌ์ ์ํ (0) | 2022.04.14 |
[baekjoon]python #2477 ์ฐธ์ธ๋ฐญ (0) | 2022.03.11 |
๋๊ธ