結果
問題 | No.898 tri-βutree |
ユーザー | 山本信二 |
提出日時 | 2022-04-18 14:54:53 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 3,530 ms / 4,000 ms |
コード長 | 2,439 bytes |
コンパイル時間 | 230 ms |
コンパイル使用メモリ | 12,800 KB |
実行使用メモリ | 93,568 KB |
最終ジャッジ日時 | 2024-06-09 06:39:05 |
合計ジャッジ時間 | 59,448 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2,320 ms
93,568 KB |
testcase_01 | AC | 28 ms
11,136 KB |
testcase_02 | AC | 27 ms
11,008 KB |
testcase_03 | AC | 26 ms
11,008 KB |
testcase_04 | AC | 27 ms
11,008 KB |
testcase_05 | AC | 26 ms
11,008 KB |
testcase_06 | AC | 26 ms
11,008 KB |
testcase_07 | AC | 3,470 ms
91,648 KB |
testcase_08 | AC | 3,362 ms
91,520 KB |
testcase_09 | AC | 3,509 ms
91,136 KB |
testcase_10 | AC | 3,353 ms
89,600 KB |
testcase_11 | AC | 3,530 ms
90,880 KB |
testcase_12 | AC | 3,483 ms
91,392 KB |
testcase_13 | AC | 3,401 ms
89,984 KB |
testcase_14 | AC | 3,456 ms
91,008 KB |
testcase_15 | AC | 3,430 ms
91,008 KB |
testcase_16 | AC | 3,390 ms
90,368 KB |
testcase_17 | AC | 3,409 ms
91,136 KB |
testcase_18 | AC | 3,377 ms
90,496 KB |
testcase_19 | AC | 3,397 ms
90,624 KB |
testcase_20 | AC | 3,384 ms
90,624 KB |
testcase_21 | AC | 3,382 ms
90,240 KB |
ソースコード
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)