본문 바로가기

Union-Find6

[백준] 16562번 - 친구비 (파이썬) # 친구비 import sys import heapq input = sys.stdin.readline N, M, k = map(int, input().split()) arr = [[]for _ in range(N+1)] moneys = [0]+list(map(int, input().split())) for i in range(1, N+1): arr[0].append((moneys[i], i)) for _ in range(M): a, b = map(int, input().split()) arr[a].append((0, b)) arr[b].append((0, a)) def make_friend(): global answer q = [] heapq.heappush(q, (0, 0)) visited = [Fal.. 2022. 3. 25.
[백준] 20040번 - 사이클 게임 (파이썬) # 사이클 게임 N, T = map(int, input().split()) parent = [i for i in range(N)] def find_parent(x): if x != parent[x]: x = find_parent(parent[x]) return parent[x] def union_parent(x, y): x = find_parent(x) y = find_parent(y) if x y: parent[x] = y return False else: return True for i in range(1, T+1): x, y = map(int, input().split()) if union_parent(find_parent(x.. 2022. 3. 16.
[백준] 10775번 - 공항 (파이썬) # 공항 def find_parent(x): if x != parent[x]: parent[x] = find_parent(parent[x]) return parent[x] def union_parent(x, y): x = find_parent(x) y = find_parent(y) if x < y: parent[y] = x else: parent[x] = y G = int(input()) P = int(input()) parent = [i for i in range(G+1)] plane = [] for _ in range(P): plane.append(int(input())) count = 0 for p in plane: x = find_parent(p) if x == 0: break union_pare.. 2022. 3. 1.
[백준] 17472번 - 다리 만들기 2 (파이썬) # 다리 만들기 2 from collections import deque import sys input = sys.stdin.readline MAX = sys.maxsize dx = [0, 1, 0, -1] dy = [1, 0, -1, 0] N, M = map(int, input().split()) land = [] arr = [] for i in range(N): arr.append(list(map(int, input().split()))) for j in range(M): if arr[i][j] == 1: land.append((i, j)) def find_land(visited, a, b, num): q = deque() q.append((a, b)) while q: x, y = q.popleft(.. 2022. 2. 19.
[백준] 4195번 - 친구 네트워크 (파이썬) # 친구 네트워크 import sys input = sys.stdin.readline T = int(input()) def find_parent(x): global result if x != parent[x]: parent[x] = find_parent(parent[x]) return parent[x] def union(a, b): global result x = find_parent(arr[a]) y = find_parent(arr[b]) if x y: parent[x] = y num[y] += num[x] for _ in range(T): N = int(input()) arr = dict() n = 0 parent = [.. 2022. 1. 29.
[백준] 1717번 - 집합의 표현 (파이썬) # 집합의 표현 import sys input = sys.stdin.readline sys.setrecursionlimit(10**5) N, m = map(int, input().split()) parent = [i for i in range(N+1)] def union(x, y): a = find_parent(x) b = find_parent(y) if a > b: parent[a] = b else: parent[b] = a def find_parent(x): if x != parent[x]: parent[x] = find_parent(parent[x]) return parent[x] def is_set(x, y): if find_parent(x) == find_parent(y): print('YES'.. 2022. 1. 24.