結果

問題 No.898 tri-βutree
ユーザー hedwig100hedwig100
提出日時 2020-04-14 10:58:44
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 2,695 ms / 4,000 ms
コード長 2,036 bytes
コンパイル時間 143 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 84,096 KB
最終ジャッジ日時 2024-04-26 09:24:47
合計ジャッジ時間 43,620 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,330 ms
84,096 KB
testcase_01 AC 34 ms
11,008 KB
testcase_02 AC 34 ms
11,136 KB
testcase_03 AC 34 ms
11,008 KB
testcase_04 AC 34 ms
11,136 KB
testcase_05 AC 34 ms
11,136 KB
testcase_06 AC 34 ms
11,008 KB
testcase_07 AC 2,566 ms
62,976 KB
testcase_08 AC 2,543 ms
63,104 KB
testcase_09 AC 2,530 ms
62,976 KB
testcase_10 AC 2,508 ms
62,976 KB
testcase_11 AC 2,538 ms
62,976 KB
testcase_12 AC 2,695 ms
62,976 KB
testcase_13 AC 2,574 ms
62,976 KB
testcase_14 AC 2,493 ms
62,976 KB
testcase_15 AC 2,562 ms
62,976 KB
testcase_16 AC 2,497 ms
62,976 KB
testcase_17 AC 2,547 ms
62,976 KB
testcase_18 AC 2,546 ms
62,976 KB
testcase_19 AC 2,581 ms
62,976 KB
testcase_20 AC 2,547 ms
62,976 KB
testcase_21 AC 2,575 ms
62,976 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000000)
dy = (-1,0,1,0)
dx = (0,1,0,-1)

class LCA():

    def __init__(self,G,root = 0): #O(NlogN)
        self.n = len(G)
        self.G = G
        self.root = root
        self.K = 1 #2 ** K > nとなる最小のK
        while (1<<self.K) <= self.n:
            self.K += 1
        self.parents = [[-1] * self.n for _ in range(self.K)]
        self.dist = [0] * self.n
        self.depth = [0] * self.n
        
        self._dfs(root,0,0,-1)
        for k in range(self.K - 1):
            for v in range(self.n):
                if self.parents[k][v] >= 0:
                    self.parents[k + 1][v] = self.parents[k][self.parents[k][v]]
    
    def _dfs(self,i,d,d2,p):
        self.parents[0][i] = p
        self.dist[i] = d  
        self.depth[i] = d2

        for e,c in self.G[i]:
            if e != p:
                self._dfs(e,d + 1,d2 + c,i)
    
    def qry(self,u,v): #O(logN)
        if self.dist[u] < self.dist[v]:
            u,v = v,u
        s,t = u,v
        
        dif = self.dist[u] - self.dist[v]
        for k in range(self.K):
            if dif >> k & 1:
                u = self.parents[k][u]
        
        if u == v:
            return self.depth[s] + self.depth[t] - 2 * self.depth[u]
        
        for k in range(self.K - 1,-1,-1):
            if self.parents[k][u] != self.parents[k][v]:
                u = self.parents[k][u]
                v = self.parents[k][v]
        u = self.parents[0][u]
        
        return self.depth[s] + self.depth[t] - 2 * self.depth[u]

def main():
    n = int(input())
    G = [[] for _ in range(n)]
    for _ in range(n - 1):
        a,b,c = map(int,input().split())
        G[a].append((b,c))
        G[b].append((a,c))
    
    lca = LCA(G)
    q = int(input())
    for _ in range(q):
        x,y,z = map(int,input().split())
        ans = 0
        ans += lca.qry(x,y)
        ans += lca.qry(y,z)
        ans += lca.qry(z,x)
        print(ans//2)

if __name__ == '__main__':
    main()
0