-
[백준] 1922 - 네트워크 연결 (파이썬)알고리즘/백준 2022. 2. 14. 09:32
import sys input = sys.stdin.readline n = int(input()) m = int(input()) array = [] for _ in range(m): array.append(list(map(int,input().split()))) array.sort(key=lambda x:x[2]) check = [i for i in range(n+1)] def find(x): if check[x] == x: return x check[x] = find(check[x]) return check[x] def union(x,y): x = find(x) y = find(y) if x == y: return if x<y: check[x] = y else: check[y] = x cnt = 0 for i in array: if find(i[0]) != find(i[1]): union(i[0],i[1]) cnt += i[2] print(cnt)
유니온 파인드를 이용해서 최소 스패닝 트리? 를 구성하였다.
'알고리즘 > 백준' 카테고리의 다른 글
[백준] 15954 - 인형들 (파이썬) (0) 2022.02.16 [백준] 1497 - 기타콘서트 (파이썬) (3) 2022.02.14 [백준] 1253 - 좋다 (파이썬) (0) 2022.02.14 [백준] 2467 - 용액 (파이썬) (0) 2022.02.14 [백준] 10819 - 차이를 최대로 (파이썬) (0) 2021.10.20