궁금하다
난 내 코드만 보거든.
어때?
import sys
import math
from collections import deque
def bfs(start, end) :
global n
#방문 배열
v = [0 for _ in range(n+1)]
que = deque()
que.append((start,home_list[start]))
v[start] = 1
while que :
node, now_ans = que.popleft()
if node == end : return now_ans
for next_node in path[node] :
if v[next_node] == 0 :
v[next_node] = 1
now_value = home_list[next_node]
grade = int(math.log10(now_value)) + 1
next_ans = (now_ans * (10**grade) + now_value) % 1_000_000_007
que.append((next_node, next_ans))
n,q = map(int,sys.stdin.readline().split())
home_list = list(map(int,sys.stdin.readline().split()))
path = [[] for _ in range(n)]
play_list = []
#입력 처리 완료
for _ in range(n-1) :
here, there = map(int,sys.stdin.readline().split())
path[here-1].append(there-1)
path[there-1].append(here-1)
for _ in range(q) :
here, there = map(int, sys.stdin.readline().split())
play_list.append((here-1, there-1))
for start, end in play_list :
print(bfs(start, end))
ㅇㅇ - dc App
dfs을 재귀로 하면 훨씬 깔끔해쥠 - dc App
난 DFS보다 BFS가 좋아!