알고리즘
[백준 알고리즘] 4386번 별자리 만들기. 파이썬(python)
삶은겨란
2022. 5. 5. 03:24
TypeError: list indices must be integers or slices, not list
타입에러: 리스트 인덱스는 리스트가 아닌 정수 또는 슬라이스여야 합니다.
6번째 줄에서 i를 리스트로 감싼 후 다시 리스트에 넣는 실수를 했다.
# 잘못된 경우
parent=[[i] for i in range(n+1)]
[[0],[1],[2],[3]]
# 의도
parent=[i for i in range(n+1)]
[0,1,2,3]
import math
n=int(input())
edges=[list(map(float,input().split())) for _ in range(n)]
costs=[]
parent=[i for i in range(n+1)]
result=0
for i in range(n-1):
for j in range(i+1,n):
costs.append((math.sqrt(((edges[j][0]-edges[i][0])**2)+((edges[j][1]-edges[i][1])**2)),i,j))
costs.sort()
def find(x):
if parent[x]!=x: # 루트가 아니면
parent[x]=find(parent[x]) # 루트를 찾는다
return parent[x] # 루트를 리턴
def union(x,y):
x=find(x)
y=find(y)
if x<y:
parent[y]=x
else:
parent[x]=y
for cost in costs: # 정렬된 간선을 탐색
c,a,b=cost
if find(a) != find(b):
union(a,b)
result+=c
print(round(result,2))