알고리즘/13. Union Find 3

[Python][UnionFind] 연결이 되어있는지 아닌지 (같은 그룹인지 아닌지)

문제) 노드들이 아래와 같이 연결되어 있습니다. 2개의 알파벳을 입력받고, 이 둘이 연결되어있는지 아닌지 찾는 코드를 작성하세요. 연결이 되어있다면 "연결됨"을 출력하고, 아니면 "X"를 출력하세요 더보기 def findboss(member): global arr if arr[ord(member)] == 0: return member ret = findboss(arr[ord(member)]) return ret def union(a,b): global arr aboss = findboss(a) bboss = findboss(b) if aboss == bboss: return arr[ord(bboss)] = aboss arr = [0]*200 union('A', 'B') union('D', 'E') uni..

[Python][UnionFind] Cycle의 유무 확인

문제) 노드 4개의 연결상태를 입력 받고, Cycle이 있는 그래프인지 아닌 그래프인지 판별하고 싶습니다. 사이클이 있다면 "있음"을 사이클이 없다면 "없음"을 출력하세요. 입력 4 A C A B B D C B 출력 있다 더보기 arr=[0]*200 def findboss(member): global arr if arr[ord(member)]==0: return member ret=findboss(arr[ord(member)]) arr[ord(member)]=ret return ret def union(a,b): global arr aboss,bboss=findboss(a),findboss(b) if aboss==bboss: return 1 arr[ord(bboss)]=aboss n = int(input..

[Python][leetcode 1971. Find if Path Exists in Graph] Union Find

📕 Problem https://leetcode.com/problems/find-if-path-exists-in-graph/ 💡 My Solution I solved this probelm using union find method. Because I cannot use "global declearation" in this website, it was a little bit hard. I had to made all of the arr list as a local variable. And test case with only one edge, [[0,4]], made an error. Initializing it as 0 caused a problem. At first I tried to initiate ..