結果

問題 No.898 tri-βutree
ユーザー 👑 tamatotamato
提出日時 2020-02-18 11:37:23
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,316 ms / 4,000 ms
コード長 2,861 bytes
コンパイル時間 1,634 ms
コンパイル使用メモリ 86,708 KB
実行使用メモリ 164,324 KB
最終ジャッジ日時 2023-08-08 16:39:05
合計ジャッジ時間 23,381 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 489 ms
149,516 KB
testcase_01 AC 73 ms
71,360 KB
testcase_02 AC 78 ms
75,900 KB
testcase_03 AC 78 ms
75,968 KB
testcase_04 AC 78 ms
76,024 KB
testcase_05 AC 79 ms
75,924 KB
testcase_06 AC 78 ms
76,200 KB
testcase_07 AC 1,199 ms
160,996 KB
testcase_08 AC 1,258 ms
162,240 KB
testcase_09 AC 1,316 ms
163,232 KB
testcase_10 AC 1,243 ms
162,184 KB
testcase_11 AC 1,259 ms
160,356 KB
testcase_12 AC 1,163 ms
160,388 KB
testcase_13 AC 1,161 ms
160,444 KB
testcase_14 AC 1,181 ms
160,556 KB
testcase_15 AC 1,041 ms
158,292 KB
testcase_16 AC 1,194 ms
159,620 KB
testcase_17 AC 1,224 ms
160,580 KB
testcase_18 AC 1,211 ms
161,416 KB
testcase_19 AC 1,259 ms
161,316 KB
testcase_20 AC 1,307 ms
164,324 KB
testcase_21 AC 1,171 ms
161,156 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    import sys
    input = sys.stdin.readline

    # min
    def STfunc(a, b):
        if a < b:
            return a
        else:
            return b

    # クエリは0-indexedで[l, r)
    class SparseTable():
        def __init__(self, A):
            # A: 処理したい数列
            self.N = len(A)
            self.K = self.N.bit_length() - 1
            self.table = [[0] * (self.K + 1) for _ in range(self.N)]
            for i, a in enumerate(A):
                self.table[i][0] = A[i]
            for k in range(1, self.K + 1):
                for i in range(self.N):
                    j = i + (1 << (k - 1))
                    if j <= self.N - 1:
                        self.table[i][k] = STfunc(self.table[i][k - 1], self.table[j][k - 1])
                    else:
                        self.table[i][k] = self.table[i][k - 1]

        def query(self, l, r):
            # [l, r)の最小値を求める
            k = (r - l).bit_length() - 1
            return STfunc(self.table[l][k], self.table[r - (1 << k)][k])

    # adj[0] must be empty list
    def EulerTour(adj, root):
        st = [root]
        ret = []
        seen = [0] * len(adj)
        par = [0] * len(adj)
        depth = [0] * len(adj)
        dist_from_root = [0] * len(adj)
        while st:
            v = st.pop()
            if seen[v]:
                ret.append(v)
                continue
            ret.append(v)
            seen[v] = 1
            if par[v] != 0:
                st.append(par[v])
            for u in adj[v]:
                if seen[u] == 0:
                    st.append(u)
                    par[u] = v
                    depth[u] = depth[v] + 1
                    dist_from_root[u] = dist_from_root[v] + weight[v*(N+1)+u]

        return ret, depth, dist_from_root

    N = int(input())
    adj = [[] for _ in range(N+1)]
    weight = {}
    for i in range(N-1):
        u, v, w = map(int, input().split())
        u += 1
        v += 1
        adj[u].append(v)
        adj[v].append(u)
        weight[u*(N+1)+v] = w
        weight[v*(N+1)+u] = w

    et, depth, dist_from_root = EulerTour(adj, 1)
    depth_list = [0] * len(et)
    left = [-1] * (N + 1)
    right = [-1] * (N + 1)
    for i, v in enumerate(et):
        depth_list[i] = depth[v] * (N+1) + v
        if left[v] < 0:
            left[v] = i
        right[v] = i
    ST = SparseTable(depth_list)

    def dist(a, b):
        lca_info = ST.query(min(left[a], left[b]), max(right[a], right[b]) + 1)
        lca_depth, lca_v = divmod(lca_info, N+1)
        return dist_from_root[a] + dist_from_root[b] - dist_from_root[lca_v]*2

    for _ in range(int(input())):
        x, y, z = map(int, input().split())
        x += 1
        y += 1
        z += 1
        print((dist(x, y) + dist(y, z) + dist(z, x)) // 2)


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