結果

問題 No.386 貪欲な領主
ユーザー rlangevinrlangevin
提出日時 2023-10-03 00:01:29
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 695 ms / 2,000 ms
コード長 2,353 bytes
コンパイル時間 530 ms
コンパイル使用メモリ 87,156 KB
実行使用メモリ 118,136 KB
最終ジャッジ日時 2023-10-03 00:01:36
合計ジャッジ時間 6,600 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,264 KB
testcase_01 AC 71 ms
71,644 KB
testcase_02 AC 74 ms
71,160 KB
testcase_03 AC 73 ms
71,480 KB
testcase_04 AC 695 ms
114,572 KB
testcase_05 AC 618 ms
117,608 KB
testcase_06 AC 661 ms
118,136 KB
testcase_07 AC 107 ms
77,844 KB
testcase_08 AC 178 ms
81,792 KB
testcase_09 AC 121 ms
77,804 KB
testcase_10 AC 72 ms
71,312 KB
testcase_11 AC 72 ms
71,312 KB
testcase_12 AC 119 ms
77,784 KB
testcase_13 AC 141 ms
79,400 KB
testcase_14 AC 636 ms
117,008 KB
testcase_15 AC 587 ms
114,440 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline


class LowestCommonAncestor:
    def __init__(self, n):
        self._n = n
        self._logn = 0
        v = 1
        while v <= self._n:
            v *= 2
            self._logn += 1
        self._depth = [0] * self._n
        self._distance = [0] * self._n
        self._ancestor = [[-1] * self._n for _ in range(self._logn)]
        self._G = [[] for i in range(self._n)]

    def add_edge(self, u, v, w=1):
        self._G[u].append((v, w))
        self._G[v].append((u, w))

    def build(self, root=0):
        stack = [root]
        self._distance[root] = U[root]
        while stack:
            cur = stack.pop()
            for nex, _ in self._G[cur]:
                if self._ancestor[0][nex] != cur and self._ancestor[0][cur] != nex:
                    self._ancestor[0][nex] = cur
                    self._depth[nex] = self._depth[cur] + 1
                    self._distance[nex] = self._distance[cur] + U[nex]
                    stack.append(nex)

        for k in range(1, self._logn):
            for i in range(self._n):
                if self._ancestor[k - 1][i] == -1:
                    self._ancestor[k][i] = -1
                else:
                    self._ancestor[k][i] = self._ancestor[k -
                                                          1][self._ancestor[k - 1][i]]

    def lca(self, u, v):
        if self._depth[u] > self._depth[v]:
            u, v = v, u

        for k in range(self._logn - 1, -1, -1):
            if ((self._depth[v] - self._depth[u]) >> k) & 1:
                v = self._ancestor[k][v]

        if u == v:
            return u

        for k in range(self._logn - 1, -1, -1):
            if self._ancestor[k][u] != self._ancestor[k][v]:
                u = self._ancestor[k][u]
                v = self._ancestor[k][v]

        return self._ancestor[0][u]

    def distance(self, u, v):
        return self._distance[u] + self._distance[v] - 2 * self._distance[self.lca(u, v)] + U[self.lca(u, v)]


N = int(input())
G = LowestCommonAncestor(N)
for i in range(N - 1):
    u, v = map(int, input().split())
    G.add_edge(u, v)
    G.add_edge(v, u)

U = [0] * N
for i in range(N):
    U[i] = int(input())

G.build(0)

M = int(input())
ans = 0
for _ in range(M):
    A, B, C = map(int, input().split())
    ans += C * G.distance(A, B)

print(ans)
0