結果

問題 No.898 tri-βutree
ユーザー toyuzukotoyuzuko
提出日時 2020-05-05 17:20:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,963 ms / 4,000 ms
コード長 2,912 bytes
コンパイル時間 336 ms
コンパイル使用メモリ 86,836 KB
実行使用メモリ 157,900 KB
最終ジャッジ日時 2023-08-08 16:47:54
合計ジャッジ時間 31,067 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 389 ms
138,416 KB
testcase_01 AC 71 ms
71,600 KB
testcase_02 AC 80 ms
76,496 KB
testcase_03 AC 80 ms
76,240 KB
testcase_04 AC 79 ms
76,348 KB
testcase_05 AC 79 ms
76,076 KB
testcase_06 AC 80 ms
76,408 KB
testcase_07 AC 1,952 ms
155,888 KB
testcase_08 AC 1,849 ms
155,232 KB
testcase_09 AC 1,898 ms
157,168 KB
testcase_10 AC 1,812 ms
154,692 KB
testcase_11 AC 1,690 ms
157,220 KB
testcase_12 AC 1,827 ms
154,460 KB
testcase_13 AC 1,681 ms
157,900 KB
testcase_14 AC 1,797 ms
155,844 KB
testcase_15 AC 1,763 ms
155,992 KB
testcase_16 AC 1,658 ms
154,060 KB
testcase_17 AC 1,815 ms
154,096 KB
testcase_18 AC 1,735 ms
154,700 KB
testcase_19 AC 1,947 ms
156,416 KB
testcase_20 AC 1,963 ms
155,980 KB
testcase_21 AC 1,773 ms
157,324 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