-문제: https://www.acmicpc.net/problem/1167
주어진 리스트에 따라 그래프 입력하는 방식이 달라져서 그래프 구현 코드를 다르게 구현했지만 계속해서 IndexError가 발생했다
이유는 찾아봐야할 것 같다
-정답풀이:
from collections import deque
import sys
input=sys.stdin.readline
n = int(input())
graph = [[] for _ in range(n + 1)]
for _ in range(n):
result = list(map(int, input().split()))
for i in range(1, len(result) - 2, 2):
graph[result[0]].append((result[i], result[i + 1]))
def bfs(start):
visit = [-1] * (n + 1)
queue = deque()
queue.append(start)
visit[start] = 0
_max = [0, 0]
while queue:
t = queue.popleft()
for x,y in graph[t]:
if visit[x] == -1:
visit[x] = visit[t] + y
queue.append(x)
if _max[0] < visit[x]:
_max = visit[x], x
return _max
distance, leaf = bfs(1)
distance, leaf = bfs(leaf)
print(distance)
-틀린풀이:
틀린 이유 (7/30)
while로 일단 돌리면 시간초과 나고,
첫번째, 마지막 인덱스 빼고 두개씩 인덱스가 진행되어야 하는데 한개씩 진행된다
입력값 처리를 제대로 못해서 틀린 것 같다
'백준 > Search' 카테고리의 다른 글
[백준/dfs] 9466번: 텀 프로젝트 (0) | 2022.02.28 |
---|---|
[백준/dfs] 2210번: 숫자판 점프 (0) | 2022.02.25 |
[백준/dfs] 1967번: 트리의 지름 (0) | 2022.02.25 |
[백준/bfs] 2573번: 빙산 (0) | 2022.02.24 |
[백준/bfs] 1707번: 이분 그래프 (0) | 2022.02.22 |