結果

問題 No.898 tri-βutree
ユーザー Coki628Coki628
提出日時 2020-08-07 18:31:34
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 716 ms / 4,000 ms
コード長 2,567 bytes
コンパイル時間 706 ms
コンパイル使用メモリ 87,380 KB
実行使用メモリ 111,348 KB
最終ジャッジ日時 2023-08-08 16:53:12
合計ジャッジ時間 13,857 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 326 ms
110,140 KB
testcase_01 AC 71 ms
71,508 KB
testcase_02 AC 79 ms
75,976 KB
testcase_03 AC 79 ms
75,960 KB
testcase_04 AC 79 ms
76,196 KB
testcase_05 AC 79 ms
76,128 KB
testcase_06 AC 79 ms
76,096 KB
testcase_07 AC 670 ms
110,684 KB
testcase_08 AC 674 ms
111,312 KB
testcase_09 AC 657 ms
111,260 KB
testcase_10 AC 660 ms
110,960 KB
testcase_11 AC 667 ms
111,300 KB
testcase_12 AC 716 ms
111,068 KB
testcase_13 AC 662 ms
110,812 KB
testcase_14 AC 696 ms
111,056 KB
testcase_15 AC 664 ms
111,136 KB
testcase_16 AC 663 ms
111,016 KB
testcase_17 AC 675 ms
111,340 KB
testcase_18 AC 665 ms
111,304 KB
testcase_19 AC 676 ms
111,060 KB
testcase_20 AC 664 ms
111,348 KB
testcase_21 AC 679 ms
110,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 19
MOD = 10 ** 9 + 7
EPS = 10 ** -10

class LCA:
    """ LCA(最小共通祖先) """

    def __init__(self, nodes, root):
        self.N = len(nodes)
        nv = 1
        MAX = 0
        while nv < self.N:
            nv *= 2
            MAX += 1
        self.MAX = MAX
        self.nxt = list2d(MAX, self.N, -1)
        self.depths, self.cost = self.dfs(self.N, nodes, root)
        for k in range(1, MAX):
            for v in range(self.N):
                if self.nxt[k-1][v] == -1: 
                    continue
                self.nxt[k][v] = self.nxt[k-1][self.nxt[k-1][v]]

    def dfs(self, N, nodes, src):
        stack = [(src, -1)]
        depths = [0] * N
        cost = [0] * N
        while stack:
            u, prev = stack.pop()
            for v, c in nodes[u]:
                if v != prev:
                    self.nxt[0][v] = u
                    depths[v] = depths[u] + 1
                    cost[v] = cost[u] + c
                    stack.append((v, u))
        return depths, cost

    def get_lca(self, a, b):
        if self.depths[a] > self.depths[b]:
            a, b = b, a
        gap = self.depths[b] - self.depths[a]
        for i in range(self.MAX):
            if gap & 1<<i:
                b = self.nxt[i][b]
        if a == b:
            return a
        else:
            for i in range(self.MAX-1, -1, -1):
                a2 = self.nxt[i][a]
                b2 = self.nxt[i][b]
                if a2 != b2:
                    a = a2
                    b = b2
            return self.nxt[0][a]

N = INT()
nodes = [[] for i in range(N)]
for i in range(N-1):
    a, b, c = MAP()
    nodes[a].append((b, c))
    nodes[b].append((a, c))

lca = LCA(nodes, 0)

Q = INT()
for _ in range(Q):
    xyz = LIST()

    ans = 0
    for i in range(3):
        a, b = xyz[i], xyz[(i+1)%3]
        l = lca.get_lca(a, b)
        ans += lca.cost[a] + lca.cost[b] - lca.cost[l]*2
    ans //= 2
    print(ans)
0