結果

問題 No.898 tri-βutree
ユーザー toyuzukotoyuzuko
提出日時 2020-05-05 17:20:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,208 ms / 4,000 ms
コード長 2,912 bytes
コンパイル時間 359 ms
コンパイル使用メモリ 82,576 KB
実行使用メモリ 153,564 KB
最終ジャッジ日時 2024-11-08 23:41:26
合計ジャッジ時間 33,926 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 400 ms
136,588 KB
testcase_01 AC 37 ms
52,736 KB
testcase_02 AC 48 ms
62,848 KB
testcase_03 AC 46 ms
61,568 KB
testcase_04 AC 47 ms
61,952 KB
testcase_05 AC 48 ms
61,952 KB
testcase_06 AC 47 ms
62,304 KB
testcase_07 AC 2,149 ms
150,820 KB
testcase_08 AC 2,046 ms
151,560 KB
testcase_09 AC 2,141 ms
153,100 KB
testcase_10 AC 2,029 ms
150,704 KB
testcase_11 AC 1,914 ms
151,320 KB
testcase_12 AC 2,015 ms
150,076 KB
testcase_13 AC 1,868 ms
151,548 KB
testcase_14 AC 1,949 ms
152,812 KB
testcase_15 AC 1,900 ms
153,564 KB
testcase_16 AC 1,801 ms
149,496 KB
testcase_17 AC 2,007 ms
151,116 KB
testcase_18 AC 1,924 ms
152,588 KB
testcase_19 AC 2,208 ms
151,528 KB
testcase_20 AC 2,105 ms
151,224 KB
testcase_21 AC 2,001 ms
151,904 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