結果

問題 No.1094 木登り / Climbing tree
ユーザー McGregorsh
提出日時 2023-07-12 23:51:59
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,288 bytes
コンパイル時間 540 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 447,928 KB
最終ジャッジ日時 2024-09-14 12:37:07
合計ジャッジ時間 28,054 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 7 WA * 19
権限があれば一括ダウンロードができます

ソースコード

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] += 1
	  nums[a].append([b, c])

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

# 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