結果

問題 No.1094 木登り / Climbing tree
ユーザー roarisroaris
提出日時 2020-06-24 03:33:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,129 ms / 2,000 ms
コード長 1,812 bytes
コンパイル時間 438 ms
コンパイル使用メモリ 82,092 KB
実行使用メモリ 149,548 KB
最終ジャッジ日時 2024-04-25 18:24:23
合計ジャッジ時間 24,896 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
55,800 KB
testcase_01 AC 1,037 ms
149,360 KB
testcase_02 AC 249 ms
145,852 KB
testcase_03 AC 254 ms
82,092 KB
testcase_04 AC 354 ms
108,432 KB
testcase_05 AC 573 ms
140,016 KB
testcase_06 AC 538 ms
100,364 KB
testcase_07 AC 1,083 ms
148,848 KB
testcase_08 AC 1,126 ms
149,548 KB
testcase_09 AC 1,120 ms
149,012 KB
testcase_10 AC 1,064 ms
148,840 KB
testcase_11 AC 1,049 ms
148,852 KB
testcase_12 AC 1,061 ms
148,800 KB
testcase_13 AC 1,129 ms
149,060 KB
testcase_14 AC 1,125 ms
149,212 KB
testcase_15 AC 402 ms
93,544 KB
testcase_16 AC 630 ms
131,124 KB
testcase_17 AC 525 ms
109,436 KB
testcase_18 AC 485 ms
101,576 KB
testcase_19 AC 607 ms
122,180 KB
testcase_20 AC 1,064 ms
149,088 KB
testcase_21 AC 544 ms
112,912 KB
testcase_22 AC 1,071 ms
149,160 KB
testcase_23 AC 1,094 ms
149,216 KB
testcase_24 AC 1,074 ms
149,220 KB
testcase_25 AC 1,079 ms
149,236 KB
testcase_26 AC 1,096 ms
149,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from collections import *

class LowestCommonAncestor:
    def __init__(self, N, G):
        self.N = N
        self.G = G
        self.log_size = 22
        self.parent = [[-1]*self.N for _ in range(self.log_size)]
        q = deque([0])
        self.depth = [-1]*self.N
        self.depth[0] = 0
        self.d = [-1]*self.N
        self.d[0] = 0
        
        while q:
            v = q.popleft()
            
            for nv, w in self.G[v]:
                if self.depth[nv]==-1:
                    self.parent[0][nv] = v
                    self.depth[nv] = self.depth[v]+1
                    self.d[nv] = self.d[v]+w
                    q.append(nv)
                    
        for i in range(1, self.log_size):
            for v in range(self.N):
                if self.parent[i-1][v]>=0:
                    self.parent[i][v] = self.parent[i-1][self.parent[i-1][v]]
        
    def lca(self, u, v):
        if self.depth[u]>self.depth[v]:
            u, v = v, u
        
        for i in range(self.log_size):
            if ((self.depth[v]-self.depth[u])>>i)&1:
                v = self.parent[i][v]
        
        if u==v:
            return u
        
        for i in range(self.log_size-1, -1, -1):
            if self.parent[i][u]!=self.parent[i][v]:
                u, v = self.parent[i][u], self.parent[i][v]
        
        return self.parent[0][u]
    
    def dist(self, u, v):
        return self.d[u]+self.d[v]-2*self.d[self.lca(u, v)]

N = int(input())
G = [[] for _ in range(N)]

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

LCA = LowestCommonAncestor(N, G)
Q = int(input())

for _ in range(Q):
    s, t = map(int, input().split())
    print(LCA.dist(s-1, t-1))
0