結果

問題 No.898 tri-βutree
ユーザー toyuzukotoyuzuko
提出日時 2020-05-05 17:20:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,096 ms / 4,000 ms
コード長 2,912 bytes
コンパイル時間 168 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 153,504 KB
最終ジャッジ日時 2024-04-26 09:25:28
合計ジャッジ時間 31,708 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 383 ms
136,932 KB
testcase_01 AC 38 ms
52,864 KB
testcase_02 AC 50 ms
62,208 KB
testcase_03 AC 51 ms
62,080 KB
testcase_04 AC 52 ms
61,696 KB
testcase_05 AC 50 ms
61,932 KB
testcase_06 AC 49 ms
61,952 KB
testcase_07 AC 2,013 ms
151,328 KB
testcase_08 AC 1,969 ms
151,816 KB
testcase_09 AC 2,011 ms
153,104 KB
testcase_10 AC 1,882 ms
150,700 KB
testcase_11 AC 1,772 ms
151,704 KB
testcase_12 AC 1,888 ms
150,212 KB
testcase_13 AC 1,761 ms
151,540 KB
testcase_14 AC 1,875 ms
152,692 KB
testcase_15 AC 1,793 ms
153,504 KB
testcase_16 AC 1,710 ms
149,372 KB
testcase_17 AC 1,883 ms
151,000 KB
testcase_18 AC 1,815 ms
152,840 KB
testcase_19 AC 2,096 ms
151,532 KB
testcase_20 AC 2,061 ms
151,100 KB
testcase_21 AC 1,907 ms
152,284 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Tree():
    def __init__(self, n, edge):
        self.n = n
        self.tree = [[] for _ in range(n)]
        for e in edge:
            self.tree[e[0]].append((e[1], e[2]))
            self.tree[e[1]].append((e[0], e[2]))

    def setroot(self, root):
        self.root = root
        self.parent = [None for _ in range(self.n)]
        self.parent[root] = -1
        self.depth = [None for _ in range(self.n)]
        self.depth[root] = 0
        self.distance = [None for _ in range(self.n)]
        self.distance[root] = 0
        stack = [root]
        while stack:
            node = stack.pop()
            for adj, cost in self.tree[node]:
                if self.parent[adj] is None:
                    self.parent[adj] = node
                    self.depth[adj] = self.depth[node] + 1
                    self.distance[adj] = self.distance[node] + cost
                    stack.append(adj)

    def diam(self):
        u = self.distance.index(max(self.distance))
        dist_u = [None for _ in range(N)]
        dist_u[u] = 0
        stack = [u]
        while stack:
            node = stack.pop()
            for adj, cost in self.tree[node]:
                if dist_u[adj] is None:
                    dist_u[adj] = dist_u[node] + cost
                    stack.append(adj)
        d = max(dist_u)
        v = dist_u.index(d)
        return d, u, v

    def construct_lca(self):
        L = (self.n - 1).bit_length()
        self.k_parent = [self.parent]
        prev = self.parent
        for k in range(L):
            array = [0 for _ in range(self.n)]
            for i in range(self.n):
                if prev[i] == -1: continue
                array[i] = prev[prev[i]]
            self.k_parent.append(array)
            prev = array

    def lca(self, u, v):
        d = self.depth[v] - self.depth[u]
        if d < 0: u, v, d = v, u, -d
        for k in self.k_parent:
            if d & 1: v = k[v]
            d >>= 1
        if u == v: return u
        for k in reversed(self.k_parent):
            pu, pv = k[u], k[v]
            if pu != pv: u, v = pu, pv
        return self.k_parent[0][u]

    def dist(self, u, v):
        l = self.lca(u, v)
        return self.distance[u] + self.distance[v] - 2 * self.distance[l]

import sys
input = sys.stdin.readline

N = int(input())
E = [tuple(map(int, input().split())) for _ in range(N - 1)]
T = Tree(N, E)
T.setroot(0)
T.construct_lca()

Q = int(input())
res = []

for _ in range(Q):
    x, y, z = map(int, input().split())
    lca_xy = T.lca(x, y)
    lca_yz = T.lca(y, z)
    lca_zx = T.lca(z, x)
    d = [T.depth[lca_xy], T.depth[lca_yz], T.depth[lca_zx]]
    if max(d) == T.depth[lca_xy]:
        res.append(T.dist(x, y) + T.dist(lca_xy, z))
    elif max(d) == T.depth[lca_yz]:
        res.append(T.dist(y, z) + T.dist(lca_yz, x))
    else:
        res.append(T.dist(z, x) + T.dist(lca_zx, y))

print('\n'.join(map(str, res)))
0