BOJ - 1717 - 집합의 표현

Updated:

import sys

def find(x):
    if(parent[x] == x): return x

    parent[x] = find(parent[x])
    return parent[x]

def unionx(x, y):
    rootX = find(x)
    rootY = find(y)

    if not rootX == rootY: parent[rootY] = rootX

def check(x, y):
    if find(x) == find(y): return "YES"
    else: return "NO"

N, M = map(int, sys.stdin.readline().split())
parent = [i for i in range(N+1)]

for _ in range(M):
    tp, a, b = map(int, sys.stdin.readline().split())
    if tp: print(check(a, b))
    else: unionx(a, b)

https://www.acmicpc.net/problem/1717

Categories:

Updated:

Leave a comment