結果

問題 No.1094 木登り / Climbing tree
ユーザー Navier_BoltzmannNavier_Boltzmann
提出日時 2022-01-04 06:45:47
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,208 ms / 2,000 ms
コード長 3,853 bytes
コンパイル時間 396 ms
コンパイル使用メモリ 82,328 KB
実行使用メモリ 137,988 KB
最終ジャッジ日時 2024-04-25 19:46:58
合計ジャッジ時間 27,946 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
54,528 KB
testcase_01 AC 1,197 ms
137,584 KB
testcase_02 AC 260 ms
135,140 KB
testcase_03 AC 388 ms
83,352 KB
testcase_04 AC 523 ms
105,868 KB
testcase_05 AC 757 ms
130,220 KB
testcase_06 AC 629 ms
97,624 KB
testcase_07 AC 1,208 ms
136,772 KB
testcase_08 AC 1,172 ms
136,900 KB
testcase_09 AC 1,182 ms
136,904 KB
testcase_10 AC 1,157 ms
137,068 KB
testcase_11 AC 1,171 ms
136,508 KB
testcase_12 AC 1,174 ms
136,728 KB
testcase_13 AC 1,162 ms
137,568 KB
testcase_14 AC 1,161 ms
137,268 KB
testcase_15 AC 492 ms
93,308 KB
testcase_16 AC 662 ms
125,380 KB
testcase_17 AC 580 ms
105,340 KB
testcase_18 AC 529 ms
99,972 KB
testcase_19 AC 604 ms
114,328 KB
testcase_20 AC 1,166 ms
136,588 KB
testcase_21 AC 570 ms
107,092 KB
testcase_22 AC 1,188 ms
136,768 KB
testcase_23 AC 1,149 ms
137,104 KB
testcase_24 AC 1,162 ms
137,680 KB
testcase_25 AC 1,161 ms
137,988 KB
testcase_26 AC 1,166 ms
137,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.readline
from collections import deque
class LCA_wp():
    
    def __init__(self,e,root=0):
        
        self.N = len(e)
        self.e = e
        self.root = root
        self.IN = [-1]*N
        self.OUT = [-1]*N
        self.DIST = [-1]*N
        self.time = 0
        self.DIST[root] = 0
        self.parents = [-1]*N
        
        v = deque()
        v.append(~root)
        v.append(root)
        
        while v:
            x = v.pop()
            if x>=0:
                self.IN[x] = self.time
                self.time += 1
            else:
                x = ~x
                self.OUT[x] = self.time
                self.time += 1
                continue
            for ix,c in e[x]:
                if self.DIST[ix] != -1:
                    self.parents[x] = ix
                    continue
                self.DIST[ix] = self.DIST[x] + c
                v.append(~ix)
                v.append(ix)

        self.depth = [(self.DIST[i],i) for i in range(N)]
        self.init_val = [-1]*(2*N)
        for i in range(N):
            x = self.IN[i]
            y = self.OUT[i]
            self.init_val[x] = self.depth[i]
            self.init_val[y] = self.depth[self.parents[i]]
        
        self.T = self.SegTree(self.init_val)

    def lca(self,l,r):
        L = self.IN[l]
        R = self.IN[r]
        if R<L:
            L,R = R,L
        R += 1
        return self.T.query(L,R)[1]
        
        
    def distance_between(self,l,r):
        
        p = self.lca(l,r)
        return self.DIST[l]+ self.DIST[r] - 2*self.DIST[p]
        
    def distance_from_root(self,x):
        return self.DIST[x]

    class SegTree:
        """
        Segment Tree
        """
    
        def __init__(self, init_val, segfunc = None, ide_ele= None):
            """
            初期化
            
            init_val: 配列の初期値
            """
            if segfunc is None:
                def segfunc(x,y):
                    return min(x,y)
                ide_ele = (float('inf'),-1)
                
            
            n = len(init_val)
            self.segfunc = segfunc
            self.ide_ele = ide_ele
            self.num = 1 << (n - 1).bit_length()
            self.tree = [ide_ele] * 2 * self.num
            # 配列の値を葉にセット
            for i in range(n):
                self.tree[self.num + i] = init_val[i]
            # 構築していく
            for i in range(self.num - 1, 0, -1):
                self.tree[i] = segfunc(self.tree[2 * i], self.tree[2 * i + 1])
    
        def update(self, k, x):
            """
            k番目の値をxに更新
    
            k: index(0-index)
            x: update value
            """
            k += self.num
            self.tree[k] = x
            while k > 1:
                self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
                k >>= 1
    
        def query(self, l, r):
            """
            [l, r)のsegfuncしたものを得る
    
            l: index(0-index)
            r: index(0-index)
            """
            res = self.ide_ele
    
            l += self.num
            r += self.num
            while l < r:
                if l & 1:
                    res = self.segfunc(res, self.tree[l])
                    l += 1
                if r & 1:
                    res = self.segfunc(res, self.tree[r - 1])
                l >>= 1
                r >>= 1
            return res        


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

L = LCA_wp(e)
for _ in range(int(input())):
    s,t = map(int,input().split())
    s -= 1
    t -= 1
    print(L.distance_between(s,t))
0