알고리즘 문제 풀이/이코테

[이코테] CH10 그래프 이론 실전문제 2번 팀 결성

파란색 가운 2024. 1. 19. 17:24
import sys
# sys.stdin.readline()
from collections import deque
sys.setrecursionlimit(100000000)
import heapq
import copy
import itertools
from itertools import combinations
from itertools import permutations
INF = 1e9
def find_parent(parent,x):
    if parent[x]!=x:
        parent[x] = find_parent(parent,parent[x])
    return parent[x]
def union_parent(parent,x,y):
    x = find_parent(parent,x)
    y = find_parent(parent,y)
    if x < y:
        parent[y] = x
    else:
        parent[x] = y

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

for _ in range(M):
    c,a,b = map(int,sys.stdin.readline().split())
    if c == 0: # union
        union_parent(parent,a,b)
    else:
        if find_parent(parent,a) == find_parent(parent,b):
            print("YES")
        else:
            print("NO")

 

소요시간 5분 (방금 공부하고 풀었으니까 당연히 쉽지)

그냥 같은 팀이라는 건 부모노드가 같다는 소리니깐 

예제랑 거의 복붙이라 쉽게 풀었다!