結果

問題 No.1094 木登り / Climbing tree
ユーザー mkawa2mkawa2
提出日時 2021-06-10 18:44:34
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,500 bytes
コンパイル時間 1,240 ms
コンパイル使用メモリ 86,168 KB
実行使用メモリ 254,844 KB
最終ジャッジ日時 2023-08-20 04:48:56
合計ジャッジ時間 32,708 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
権限があれば一括ダウンロードができます

ソースコード

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