結果

問題 No.1094 木登り / Climbing tree
ユーザー McGregorsh
提出日時 2023-07-13 00:13:03
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,382 bytes
コンパイル時間 235 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 475,960 KB
最終ジャッジ日時 2024-09-14 12:54:48
合計ジャッジ時間 51,422 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 17 TLE * 9
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from sys import stdin
from fractions import Fraction
import math
from math import ceil, floor, sqrt, pi, factorial, gcd
from copy import deepcopy
from collections import Counter, deque, defaultdict
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement, permutations
from bisect import bisect, bisect_left, bisect_right
from functools import reduce, lru_cache
from decimal import Decimal, getcontext, ROUND_HALF_UP
def i_input(): return int(stdin.readline())
def i_map(): return map(int, stdin.readline().split())
def i_list(): return list(i_map())
def s_input(): return stdin.readline()[:-1]
def s_map(): return s_input().split()
def s_list(): return list(s_map())
def lcm(a, b): return a * b // gcd(a, b)
def get_distance(x1, y1, x2, y2):
	  d = sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
	  return d
def rotate(table):
   	  n_fild = []
   	  for x in zip(*table[::-1]):
   	  	  n_fild.append(x)
   	  return n_fild
sys.setrecursionlimit(10 ** 7)
INF = float('inf')
MOD = 10 ** 9 + 7
MOD2 = 998244353
alpa = 'abcdefghijklmnopqrstuvwxyz'
ALPA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'



N = int(input())
nums = [[] for i in range(N)]
cou = [0] * N
for i in range(N-1):
	  a, b, c = i_map()
	  a -= 1
	  b -= 1
	  cou[b] += 2
	  cou[a] += 2
	  nums[a].append([b, c])
	  nums[b].append([a, c])

start = 0
for i in range(N):
	  if cou[i] == 2:
	  	  start = i
	  	  break

G = [[] for i in range(N)]
costs = [INF] * N
costs[start] = 0
que = deque()
que.append(start)
while que:
   p = que.popleft()
   for nxt, cost in nums[p]:
   	  if costs[nxt] != INF:
   	  	  continue
   	  costs[nxt] = cost + costs[p]
   	  G[p].append(nxt)
   	  que.append(nxt)
#print(costs)

# Euler Tour の構築
def EuletTour(N, G, start):
	  # 注意!!
	  #G[v]: 頂点Vの子頂点(親頂点は含まない)
	  #start 根。基本は0だが問題によりそれ以外になる。
	  def dfs(v, d):
	  	  F[v] = len(S)
	  	  depth[v] = d
	  	  S.append(v)
	  	  for w in G[v]:
	  	  	  dfs(w, d+1)
	  	  	  S.append(v)
	  
	  S = []
	  F = [0]*N
	  depth = [0]*N
	  
	  dfs(start, 0)
	  return S, F, depth

S, F, depth = EuletTour(N, G, start)

# 存在しない範囲は深さが他よりも大きくなるようにする
INF = (N, None)

# LCAを計算するクエリの前計算
M = 2*N
M0 = 2**(M-1).bit_length()
data = [INF]*(2*M0)
for i, v in enumerate(S):
    data[M0-1+i] = (depth[v], i)
for i in range(M0-2, -1, -1):
    data[i] = min(data[2*i+1], data[2*i+2])

# LCAの計算 (generatorで最小値を求める)
def _query(a, b):
    yield INF
    a += M0; b += M0
    while a < b:
        if b & 1:
            b -= 1
            yield data[b-1]
        if a & 1:
            yield data[a-1]
            a += 1
        a >>= 1; b >>= 1

# LCAの計算 (外から呼び出す関数)
def query(u, v):
    fu = F[u]; fv = F[v]
    if fu > fv:
        fu, fv = fv, fu
    return S[min(_query(fu, fv+1))[1]]
    

Q = int(input())
for i in range(Q):
	  a, b = i_map()
	  a -= 1
	  b -= 1
	  flag = query(a, b)
	  if flag != a and flag != b:
	  	  c1 = costs[a] - costs[flag]
	  	  c2 = costs[b] - costs[flag]
	  	  print(c1 + c2)
	  else:
	  	  if flag == a:
	  	  	  print(costs[b] - costs[a])
	  	  else:
	  	  	  print(costs[a] - costs[b])



#def main():
   
   
   
   
   
#if __name__ == '__main__':
#    main()



0