結果

問題 No.898 tri-βutree
ユーザー 👑 rin204rin204
提出日時 2022-04-17 16:28:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,240 ms / 4,000 ms
コード長 2,439 bytes
コンパイル時間 188 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 128,768 KB
最終ジャッジ日時 2024-06-07 23:12:10
合計ジャッジ時間 21,610 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 508 ms
128,768 KB
testcase_01 AC 37 ms
52,864 KB
testcase_02 AC 47 ms
62,080 KB
testcase_03 AC 48 ms
60,544 KB
testcase_04 AC 47 ms
60,928 KB
testcase_05 AC 47 ms
60,800 KB
testcase_06 AC 47 ms
61,056 KB
testcase_07 AC 1,088 ms
125,460 KB
testcase_08 AC 1,057 ms
125,808 KB
testcase_09 AC 1,124 ms
125,676 KB
testcase_10 AC 1,110 ms
125,756 KB
testcase_11 AC 1,091 ms
125,512 KB
testcase_12 AC 1,133 ms
125,352 KB
testcase_13 AC 1,075 ms
126,552 KB
testcase_14 AC 1,162 ms
126,168 KB
testcase_15 AC 1,068 ms
125,392 KB
testcase_16 AC 1,110 ms
126,032 KB
testcase_17 AC 1,089 ms
125,272 KB
testcase_18 AC 1,081 ms
125,292 KB
testcase_19 AC 1,195 ms
126,104 KB
testcase_20 AC 1,240 ms
125,136 KB
testcase_21 AC 1,113 ms
125,388 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class WeightedLCA:
    def __init__(self, n, edges, e, ope, root = 0):
        self.n = n
        self.e = e
        self.ope = ope
        self.logn = (n - 1).bit_length()
        self.root = root
        self.depth = [-1] * self.n
        self.parent = [[-1] * n for _ in range(self.logn)]
        self.weight = [[-1] * n for _ in range(self.logn)]
        self.dfs(edges)
        self.doubling()
    
    def dfs(self, edges):
        stack = [self.root]
        self.depth[self.root] = 0
        while stack:
            pos = stack.pop()
            for npos, w in edges[pos]:
                if self.depth[npos] != -1:
                    continue
                self.depth[npos] = self.depth[pos] + 1
                self.parent[0][npos] = pos
                self.weight[0][npos] = w
                stack.append(npos)
    
    def doubling(self):
        for i in range(1, self.logn):
            for j in range(self.n):
                if self.parent[i - 1][j] != -1:
                    p = self.parent[i - 1][j]
                    self.parent[i][j] = self.parent[i - 1][p]
                    self.weight[i][j] = self.ope(self.weight[i - 1][j], self.weight[i - 1][p])
    
    def get(self, u, v):
        if self.depth[v] < self.depth[u]:
            u, v = v, u
        du = self.depth[u]
        dv = self.depth[v]
        ret = self.e
        
        for i in range(self.logn):
            if (dv - du) >> i & 1:
                ret = self.ope(ret, self.weight[i][v])
                v = self.parent[i][v]
        
        if u == v:
            return ret, u
            
        for i in range(self.logn - 1, -1, -1):
            pu = self.parent[i][u]
            pv = self.parent[i][v]
            if pu != pv:
                ret = self.ope(ret, self.weight[i][u])
                ret = self.ope(ret, self.weight[i][v])
                u, v = pu, pv
        
        ret = self.ope(ret, self.weight[0][u])
        ret = self.ope(ret, self.weight[0][v])
        u = self.parent[0][u]
        return ret, u
            
n = int(input())
edges = [[] for _ in range(n)]
for _ in range(n - 1):
    u, v, w = map(int, input().split())
    edges[u].append((v, w))
    edges[v].append((u, w))

lca = WeightedLCA(n, edges, 0, lambda x, y: x + y)
Q = int(input())
for _ in range(Q):
    x, y, z = map(int, input().split())
    ans = lca.get(x, y)[0]
    ans += lca.get(y, z)[0]
    ans += lca.get(z, x)[0]
    print(ans // 2)
0