https://www.acmicpc.net/problem/1240
import sys
def input():
return sys.stdin.readline().rstrip()
n,m = map(int,input().split())
graph =[[0]*(n+1) for _ in range(n+1)]
answer = 0
def dfs(s,e,dist):
global answer
visited[s] = 1
# for nx in graph[s]:
for i in range(1,n+1):
dis = graph[s][i]#거린데
if graph[s][i]>0 and not visited[i]:
if i == e:
answer = dist+graph[s][i]
else:
dfs(i,e,dist+graph[s][i])
for _ in range(n-1):
a,b,d = map(int,input().split())
graph[a][b] = d
graph[b][a] = d
for _ in range(m):
visited = [0]*(n+1)
a,b = map(int,input().split())
dfs(a,b,0)
print(answer)
[백준_14244] 트리만들기 (0) | 2022.10.19 |
---|---|
[백준_14923] 미로탈출_bfs_python (0) | 2022.09.08 |
[백준_18513] 샘터 python (0) | 2022.08.26 |
[백준_16932] 모양만들기 python (0) | 2022.08.26 |
[백준_1953_사과나무] 골드5 python (0) | 2022.08.25 |
댓글 영역