Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 22 | 23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 원형 연결 구조 연결된 큐
- 문제해결 단계
- 값/참조/주소에 의한 전달
- 회전 및 자리 이동 연산
- 범위 기반 for문
- const화
- 프로그래머스 배열만들기4
- C언어 스택 연산
- 운영체제 기능
- auto 키워드
- C언어 덱
- 주기억장치
- C언어 계산기 프로그램
- 괄호 검사 프로그램
- string유형
- getline()함수
- 알고리즘 조건
- 문자형 배열
- LAN의 분류
- IPv4 주소체계
- 프로그래머스 푸드 파이트 대회
- const l-value참조자
- 백준 파이썬
- 입출력 관리자
- c언어 괄호검사
- l-value참조자
- 유형 변환
- 네트워크 결합
- 논리 연산
- r-value참조자
Archives
- Today
- Total
chyam
[백준] 1922번, python - 네트워크 연결(Union-Find) 본문

https://www.acmicpc.net/problem/1922
import sys
import heapq
input = sys.stdin.readline
n = int(input())
m = int(input())
computers = []
res = 0
parent = [0] * (n+1)
def find(x): # 특정 원소가 속한 집합 찾기
if parent[x] != x: # 루트 노드가 아니라면 루트 노드 찾을때까지 재귀적으로 호출
parent[x] = find(parent[x])
return parent[x]
def union(a,b): # 합치기
a, b = find(a), find(b) # 속하는 집합의 부모를 찾음
if a < b: # 부모의 숫자가 더 작은것이 부모가 된다.
# ex) 2의 부모가 1이고, 5의 부모가 3이면 3의 부모는 1이된다.
parent[b] = a
else:
parent[a] = b
for _ in range(m):
a, b, c = map(int,input().split()) # a컴퓨터, b컴퓨터, 비용
computers.append((c, a, b)) # 비용, a컴퓨터, b컴퓨터
for i in range(1, n+1):
parent[i] = i # 자기 자신을 부모로 설정
computers.sort() # 비용을 오름차순으로 설정
for c, a, b in computers:
if find(a) != find(b): # 부모가 같지 않다면 = 사이클이 되면 안됨!
union(a,b) # 합쳐준다.
res += c # 비용도 더해준다.
print(res)'백준' 카테고리의 다른 글
| [백준] 10986번, python - 나머지 합 (0) | 2026.01.25 |
|---|---|
| [백준] 2565번, python - 전깃줄 (0) | 2026.01.23 |
| [백준] 11404번, python - 플로이드 (0) | 2026.01.21 |
| [백준] 12865번,python - 평범한 배낭(배낭 문제) (0) | 2026.01.20 |
| [백준] 2468번,python - 안전 영역 (0) | 2026.01.19 |