結果

問題 No.1094 木登り / Climbing tree
ユーザー H3PO4H3PO4
提出日時 2021-10-12 10:21:19
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 1,198 ms / 2,000 ms
コード長 1,882 bytes
コンパイル時間 406 ms
コンパイル使用メモリ 87,156 KB
実行使用メモリ 153,088 KB
最終ジャッジ日時 2023-08-08 01:45:20
合計ジャッジ時間 26,637 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 89 ms
71,692 KB
testcase_01 AC 1,020 ms
152,160 KB
testcase_02 AC 251 ms
144,832 KB
testcase_03 AC 286 ms
82,988 KB
testcase_04 AC 393 ms
110,808 KB
testcase_05 AC 546 ms
141,352 KB
testcase_06 AC 541 ms
101,660 KB
testcase_07 AC 1,133 ms
151,584 KB
testcase_08 AC 1,169 ms
151,808 KB
testcase_09 AC 1,071 ms
151,056 KB
testcase_10 AC 1,153 ms
151,376 KB
testcase_11 AC 1,160 ms
151,572 KB
testcase_12 AC 1,049 ms
151,740 KB
testcase_13 AC 1,155 ms
152,212 KB
testcase_14 AC 1,145 ms
151,572 KB
testcase_15 AC 410 ms
94,800 KB
testcase_16 AC 611 ms
133,160 KB
testcase_17 AC 530 ms
111,248 KB
testcase_18 AC 477 ms
103,608 KB
testcase_19 AC 599 ms
124,268 KB
testcase_20 AC 1,059 ms
151,216 KB
testcase_21 AC 564 ms
113,044 KB
testcase_22 AC 1,198 ms
151,928 KB
testcase_23 AC 1,062 ms
152,128 KB
testcase_24 AC 1,182 ms
153,088 KB
testcase_25 AC 1,044 ms
152,444 KB
testcase_26 AC 1,058 ms
152,156 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque

input = sys.stdin.buffer.readline

N = int(input())
T = [[] for _ in range(N)]
for _ in range(N - 1):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    T[a].append((b, c))
    T[b].append((a, c))

cost = [None] * N
cost[0] = 0
depth = [None] * N
depth[0] = 0
prv = [-1] * N

d = deque([0])
while d:
    v = d.pop()
    cv = cost[v]
    dv = depth[v]
    for x, c in T[v]:
        if depth[x] is None:
            cost[x] = cv + c
            depth[x] = dv + 1
            prv[x] = v
            d.append(x)

# https://tjkendev.github.io/procon-library/python/graph/lca-doubling.html から拝借しています。
# N: 頂点数
# G[v]: 頂点vの子頂点 (親頂点は含まない)
#
# - construct
# prv[u] = v: 頂点uの一つ上の祖先頂点v
# - lca
# kprv[k][u] = v: 頂点uの2^k個上の祖先頂点v
# depth[u]: 頂点uの深さ (根頂点は0)

LV = (N - 1).bit_length()


def construct(prv):
    kprv = [prv]
    S = prv
    for k in range(LV):
        T = [0] * N
        for i in range(N):
            if S[i] is None:
                continue
            T[i] = S[S[i]]
        kprv.append(T)
        S = T
    return kprv


def lca(u, v, kprv, depth):
    dd = depth[v] - depth[u]
    if dd < 0:
        u, v = v, u
        dd = -dd

    # assert depth[u] <= depth[v]
    for k in range(LV + 1):
        if dd & 1:
            v = kprv[k][v]
        dd >>= 1

    # assert depth[u] == depth[v]
    if u == v:
        return u

    for k in range(LV - 1, -1, -1):
        pu = kprv[k][u];
        pv = kprv[k][v]
        if pu != pv:
            u = pu;
            v = pv

    # assert kprv[0][u] == kprv[0][v]
    return kprv[0][u]


kprv = construct(prv)
Q = int(input())
for _ in range(Q):
    s, t = (int(x) - 1 for x in input().split())
    print(cost[s] + cost[t] - 2 * cost[lca(s, t, kprv, depth)])
0