회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
채점 현황
게시판
그룹
더 보기
재채점 기록
블로그
도움말
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
왜 틀렸는지 전혀 모르겠습니다
1774번 - 우주신과의 교감
wlgnstjd0717
2년 전
0
모든 반례도 맞고 인덱스, 출력형식 모두 신경썼는데 4퍼센트를 못 넘는 이유를 모르겠습니다.. 도움 부탁드립니다
import heapq import math def solution(N, M, data_list, line_list): # 유니온 리스트 union_list = list(range(N+1)) # 자체 순환을 막기 위한 최상위 노드 top_node = 0 # 이미 연결된 좌표 유니온 리스트에 최신화 for parent, child in line_list: # 최상위 노드가 안정해져 있으면 if not top_node: top_node = parent # 자식이 최상위 노드면 if child == top_node: union_list[parent] = child else: union_list[child] = parent # 크루스칼 dist_sum = round(kruskal(union_list, data_list), 2) print(f'{dist_sum:.2f}') # 크루스칼 def kruskal(union_list, data_list): # 큐 queue = [] # 거리 계산 for i in range(0, len(data_list)): for j in range(i, len(data_list)): # 거리 계산 후 큐에 삽입 heapq.heappush(queue, [cal_dist(data_list[i], data_list[j]), i+1, j+1]) # 거리 합 dist_sum = 0 # 큐를 순회 while True: # 큐가 비면 끝 if not queue: return dist_sum # 거리, 부모, 자식 dist, parent, child = heapq.heappop(queue) # 순환 체크 state, union_list = union(union_list, parent, child) # 순환이 아니면 if state: # 거리에 더해주기 dist_sum += dist # 거리 계산 def cal_dist(idx_1, idx_2): # x, y 차 sub_x = idx_1[0] - idx_2[0] sub_y = idx_1[1] - idx_2[1] return math.sqrt(sub_x**2 + sub_y**2) # 파인드 def find(union_list, num): # 자신의 루트가 자신이면 if union_list[num] == num: return num # 아니면 유니온 리스트 최신화 해주며 재귀 else: union_list[num] = find(union_list, union_list[num]) return union_list[num] # 유니온 def union(union_list, num_1, num_2): # 루트 root_1 = find(union_list, num_1) root_2 = find(union_list, num_2) # 두 루트가 같으면 if root_1 == root_2: # 순환이므로 그냥 리턴 return False, union_list # 다르면 else: # 병합 union_list[root_2] = root_1 return True, union_list N, M = map(int, input().split()) data_list = [list(map(int, input().split())) for _ in range(N)] line_list = [list(map(int, input().split())) for _ in range(M)] solution(N, M, data_list, line_list)
댓글을 작성하려면
로그인
해야 합니다.
wlgnstjd0717 2년 전
모든 반례도 맞고 인덱스, 출력형식 모두 신경썼는데 4퍼센트를 못 넘는 이유를 모르겠습니다.. 도움 부탁드립니다