結果

問題 No.898 tri-βutree
コンテスト
ユーザー ああ
提出日時 2026-05-18 13:31:38
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 1,385 ms / 4,000 ms
コード長 969 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 206 ms
コンパイル使用メモリ 84,992 KB
実行使用メモリ 121,952 KB
最終ジャッジ日時 2026-05-18 13:31:59
合計ジャッジ時間 18,363 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge1_1
純コード判定待ち
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from collections import deque

def aa(a,b):
    c,d=dep[a],dep[b]
    if d>c:
        a,b=b,a;c,d=d,c
    c-=d;d=0
    while c:
        if c&1:
            a=doub[d][a]
        c>>=1;d+=1
    if a==b:
        return dist[a]
    for i in range(19,-1,-1):
        if doub[i][a]!=doub[i][b]:
            a,b=doub[i][a],doub[i][b]
    return dist[doub[0][a]]
    
        

n=int(input())
v=[[] for i in range(n)]
for i in range(n-1):
    a,b,c=map(int,input().split())
    v[a].append((b,c));v[b].append((a,c))
dist=[-1]*n;dist[0]=0
dep=[0]*n;doub=[[0]*n for i in range(20)]
f=deque([0])
while f:
    q=f.pop()
    for i,j in v[q]:
        if dist[i]<0:
            dist[i]=dist[q]+j
            dep[i]=dep[q]+1
            doub[0][i]=q
            f.append(i)
for i in range(1,20):
    for j in range(n):
        doub[i][j]=doub[i-1][doub[i-1][j]]
for i in range(int(input())):
    a,b,c=map(int,input().split())
    print(dist[a]+dist[b]+dist[c]-aa(a,b)-aa(b,c)-aa(a,c))
0