From a36c2500d3808b714d8e49c99ec12b7273ca7577 Mon Sep 17 00:00:00 2001 From: heerucan Date: Sun, 27 Mar 2022 20:56:39 +0900 Subject: [PATCH] =?UTF-8?q?[=EC=BD=94=EB=94=A9=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=EC=B1=85]=2010-1.=20=ED=8C=80=20=EA=B2=B0=EC=84=B1=20(#8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\355\214\200\352\262\260\354\204\261.py" | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 "CodingTest/CH10 \352\267\270\353\236\230\355\224\204 \354\235\264\353\241\240/\355\214\200\352\262\260\354\204\261.py" diff --git "a/CodingTest/CH10 \352\267\270\353\236\230\355\224\204 \354\235\264\353\241\240/\355\214\200\352\262\260\354\204\261.py" "b/CodingTest/CH10 \352\267\270\353\236\230\355\224\204 \354\235\264\353\241\240/\355\214\200\352\262\260\354\204\261.py" new file mode 100644 index 0000000..ca81349 --- /dev/null +++ "b/CodingTest/CH10 \352\267\270\353\236\230\355\224\204 \354\235\264\353\241\240/\355\214\200\352\262\260\354\204\261.py" @@ -0,0 +1,33 @@ +# 같은 팀 여부 확인 +def find_parent(parent, x): + if parent[x] != x: + parent[x] = find_parent(parent, parent[x]) + return parent[x] + +# 팀 합치기 +def union_parent(parent, a, b): + a = find_parent(parent, a) + b = find_parent(parent, b) + if a < b: + parent[b] = a + else: + parent[a] = b + +# n번, m개 연산 +n, m = map(int, input().split()) +parent = [0]*(n+1) #부모 테이블 초기화 + +# 부모 테이블 상에서, 부모를 자기 자신으로 초기화 +for i in range(1, n+1): + parent[i] = i + +# union 연산을 각각 수행 +for i in range(m): + cal, a, b = map(int, input().split()) + if cal == 0: + union_parent(parent, a, b) + else: + if find_parent(parent, a) == find_parent(parent, b): + print("YES") + else: + print("NO")