DevYoon
[๋ฐฑ์ค] 1717. ์งํฉ์ ํํ (Python) ๋ณธ๋ฌธ
link ๐ https://www.acmicpc.net/problem/1717
๐ฌ ์ ๋์จ ํ์ธ๋ ๋ฌธ์ !
๐ฌ ์ ๋์จ ํ์ธ๋๋ฅผ ์ค๋๋ง์ ํ์ด์, ๊ฐ๋ ๋ถํฐ ๋ค์ ๋ด์ผ ํ๋คใ ใ
๐ฌ type์ด 1์ผ ๊ฒฝ์ฐ union ํจ์ ๋ด์์ YES ํน์ NO๋ฅผ ๊ตฌ๋ถํ๋ค.
import sys
input = sys.stdin.readline
def find(n):
if parents[n] == n:
return n
parents[n] = find(parents[n])
return parents[n]
def union(type, n1, n2):
fn1 = find(n1)
fn2 = find(n2)
if type == 1:
if fn1 == fn2:
print("YES")
else:
print("NO")
else:
parents[fn2] = fn1
N, M = map(int, input().split())
parents = [num for num in range(N+1)]
for _ in range(M):
type, num1, num2 = map(int, input().split())
union(type, num1, num2)