結果
問題 | No.1094 木登り / Climbing tree |
ユーザー | 👑 rin204 |
提出日時 | 2022-03-23 15:53:13 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,429 ms / 2,000 ms |
コード長 | 2,427 bytes |
コンパイル時間 | 333 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 169,936 KB |
最終ジャッジ日時 | 2024-11-08 07:25:18 |
合計ジャッジ時間 | 28,006 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 44 ms
52,480 KB |
testcase_01 | AC | 1,171 ms
169,012 KB |
testcase_02 | AC | 305 ms
165,632 KB |
testcase_03 | AC | 291 ms
81,732 KB |
testcase_04 | AC | 484 ms
115,328 KB |
testcase_05 | AC | 658 ms
155,392 KB |
testcase_06 | AC | 649 ms
104,824 KB |
testcase_07 | AC | 1,182 ms
168,412 KB |
testcase_08 | AC | 1,190 ms
168,184 KB |
testcase_09 | AC | 1,193 ms
167,936 KB |
testcase_10 | AC | 1,209 ms
167,956 KB |
testcase_11 | AC | 1,206 ms
168,320 KB |
testcase_12 | AC | 1,204 ms
168,272 KB |
testcase_13 | AC | 1,213 ms
168,448 KB |
testcase_14 | AC | 1,184 ms
168,192 KB |
testcase_15 | AC | 498 ms
96,220 KB |
testcase_16 | AC | 713 ms
145,880 KB |
testcase_17 | AC | 601 ms
117,008 KB |
testcase_18 | AC | 564 ms
107,232 KB |
testcase_19 | AC | 661 ms
132,736 KB |
testcase_20 | AC | 1,188 ms
168,064 KB |
testcase_21 | AC | 622 ms
120,056 KB |
testcase_22 | AC | 1,237 ms
168,064 KB |
testcase_23 | AC | 1,210 ms
169,296 KB |
testcase_24 | AC | 1,245 ms
169,600 KB |
testcase_25 | AC | 1,212 ms
169,012 KB |
testcase_26 | AC | 1,429 ms
169,936 KB |
ソースコード
import sys input = sys.stdin.readline 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(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): a, b, c = map(int, input().split()) a -= 1 b -= 1 edges[a].append((b, c)) edges[b].append((a, c)) lca = WeightedLCA(n, edges, 0, lambda x, y: x + y) Q = int(input()) for _ in range(Q): u, v = map(int, input().split()) print(lca.get(u - 1, v - 1)[0])