結果

問題 No.1094 木登り / Climbing tree
ユーザー mkawa2mkawa2
提出日時 2021-06-10 18:44:59
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,031 ms / 2,000 ms
コード長 2,502 bytes
コンパイル時間 511 ms
コンパイル使用メモリ 82,524 KB
実行使用メモリ 249,056 KB
最終ジャッジ日時 2024-04-25 19:41:44
合計ジャッジ時間 24,436 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,616 KB
testcase_01 AC 997 ms
246,700 KB
testcase_02 AC 468 ms
240,476 KB
testcase_03 AC 222 ms
84,648 KB
testcase_04 AC 455 ms
154,244 KB
testcase_05 AC 776 ms
221,876 KB
testcase_06 AC 442 ms
126,060 KB
testcase_07 AC 992 ms
246,768 KB
testcase_08 AC 1,003 ms
246,860 KB
testcase_09 AC 1,029 ms
246,712 KB
testcase_10 AC 1,023 ms
246,516 KB
testcase_11 AC 1,016 ms
247,068 KB
testcase_12 AC 1,031 ms
246,524 KB
testcase_13 AC 1,003 ms
243,892 KB
testcase_14 AC 1,002 ms
246,500 KB
testcase_15 AC 322 ms
110,268 KB
testcase_16 AC 591 ms
220,940 KB
testcase_17 AC 445 ms
159,232 KB
testcase_18 AC 375 ms
131,588 KB
testcase_19 AC 522 ms
179,296 KB
testcase_20 AC 998 ms
243,784 KB
testcase_21 AC 450 ms
172,784 KB
testcase_22 AC 1,009 ms
246,380 KB
testcase_23 AC 996 ms
243,416 KB
testcase_24 AC 1,016 ms
249,056 KB
testcase_25 AC 999 ms
247,124 KB
testcase_26 AC 997 ms
243,596 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(10**6)
int1 = lambda x: int(x)-1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.buffer.readline())
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def LI1(): return list(map(int1, sys.stdin.buffer.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def BI(): return sys.stdin.buffer.readline().rstrip()
def SI(): return sys.stdin.buffer.readline().rstrip().decode()
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
dij = [(0, 1), (1, 0), (1, 1), (1, -1)]
inf = 10**16
md = 998244353
# md = 10**9+7

class SparseTableMin:
    def __init__(self, aa):
        w = len(aa)
        h = (w-1).bit_length()
        table = [aa]+[[-1]*w for _ in range(h-1)]
        tablei1 = table[0]
        for i in range(1, h):
            tablei = table[i]
            for j in range(w-(1 << i)+1):
                rj = j+(1 << i-1)
                tablei[j] = min(tablei1[j], tablei1[rj])
            tablei1 = tablei
        self.table = table

    def min(self, l, r):
        i = (r-l).bit_length()-1
        tablei = self.table[i]
        Lmin = tablei[l]
        Rmin = tablei[r-(1 << i)]
        if Lmin < Rmin: Rmin = Lmin
        return Rmin

class LCA:
    def __init__(self, et, depth, first):
        n = len(depth)
        self.sp = SparseTableMin([(depth[u], u) for u in et])
        self.first = first

    def get_lca(self, u, v):
        l, r = self.first[u], self.first[v]
        if l > r: l, r = r, l
        return self.sp.min(l, r+1)[1]

def DFS(to, root=0):
    stack = [root << 1 | 1]
    while stack:
        u, f = divmod(stack.pop(), 2)
        euler.append(u)
        if f:
            first[u] = len(euler)-1
            for v, c in to[u]:
                if v == parent[u]: continue
                parent[v] = u
                depth[v] = depth[u]+1
                cost[v] = cost[u]+c
                stack.append(u << 1)
                stack.append(v << 1 | 1)

n = II()
to = [[] for _ in range(n)]
for _ in range(n-1):
    u, v, c = LI1()
    to[u].append((v, c+1))
    to[v].append((u, c+1))

cost = [0]*n
euler = []
depth = [0]*n
first = [0]*n
parent = [-1]*n
DFS(to)
lca = LCA(euler, depth, first)
# print(euler)

for _ in range(II()):
    u, v = LI1()
    w = lca.get_lca(u, v)
    # print(u,v,w)
    print(cost[u]+cost[v]-cost[w]*2)
0