[Gold V] Title: 집합의 표현, Time: 348 ms, Memory: 72404 KB -BaekjoonHub

This commit is contained in:
SSUM
2025-02-27 13:57:14 +09:00
parent 8469583609
commit 9c812846b5
2 changed files with 70 additions and 0 deletions

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,40 @@
import sys
input = sys.stdin.readline
n,m = map(int, input().split())
def union(a, b):
a = find(a)
b = find(b)
if a!=b:
lst[b] = a
def find(a):
if(a==lst[a]):
return a
else:
lst[a] = find(lst[a])
return lst[a]
def checkSame(a,b):
a = find(a)
b = find(b)
if(a==b):
return True
return False
lst = []
for i in range(1000001):
lst.append(i)
for _ in range(m):
check, a, b = map(int, input().split())
if check == 0:
union(a,b)
else:
if checkSame(a,b):
print("YES")
else:
print("NO")