結果
問題 | No.898 tri-βutree |
ユーザー | sgsw |
提出日時 | 2021-08-15 21:17:31 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,156 ms / 4,000 ms |
コード長 | 3,417 bytes |
コンパイル時間 | 266 ms |
コンパイル使用メモリ | 82,324 KB |
実行使用メモリ | 137,132 KB |
最終ジャッジ日時 | 2024-10-08 06:02:08 |
合計ジャッジ時間 | 21,344 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 352 ms
137,132 KB |
testcase_01 | AC | 38 ms
53,684 KB |
testcase_02 | AC | 40 ms
55,932 KB |
testcase_03 | AC | 41 ms
55,752 KB |
testcase_04 | AC | 41 ms
55,004 KB |
testcase_05 | AC | 41 ms
54,320 KB |
testcase_06 | AC | 42 ms
55,312 KB |
testcase_07 | AC | 1,125 ms
131,480 KB |
testcase_08 | AC | 1,088 ms
130,352 KB |
testcase_09 | AC | 1,105 ms
131,012 KB |
testcase_10 | AC | 1,156 ms
132,220 KB |
testcase_11 | AC | 1,104 ms
131,452 KB |
testcase_12 | AC | 1,113 ms
131,952 KB |
testcase_13 | AC | 1,097 ms
131,004 KB |
testcase_14 | AC | 1,062 ms
131,736 KB |
testcase_15 | AC | 1,062 ms
131,608 KB |
testcase_16 | AC | 1,107 ms
131,488 KB |
testcase_17 | AC | 1,019 ms
130,836 KB |
testcase_18 | AC | 1,100 ms
131,124 KB |
testcase_19 | AC | 1,078 ms
131,620 KB |
testcase_20 | AC | 1,132 ms
132,192 KB |
testcase_21 | AC | 1,150 ms
130,008 KB |
ソースコード
import types _atcoder_code = """ # Python port of AtCoder Library. __all__ = ["string","lazysegtree","convolution","maxflow","modint" ,"mincostflow","segtree","_scc","_math","math","dsu","twosat","fenwicktree","scc","_bit","lca","unverified","graph","matrix","algebra","combinatorics"] __version__ = '0.0.1' """ atcoder = types.ModuleType('atcoder') exec(_atcoder_code, atcoder.__dict__) _atcoder_lca_code = """ # Lowest Common Ancestor # 0-indexed class LowestCommonAncestor: ''' assert given_graph is tree. ''' def __init__(self, edges): self.n = len(edges) self.edges = edges self.dfs() self.doubling() def dfs(self): parents = [-1] * self.n depths = [-1] * self.n depths[0] = 0 todo = [0] while todo: a = todo.pop() for b in self.edges[a]: if depths[b] + 1: continue parents[b] = a depths[b] = depths[a] + 1 todo.append(b) self.parents = parents self.depths = depths def doubling(self): dbl = [self.parents] for _ in range(max(self.depths).bit_length()-1): parents = dbl[-1] new_parents = [-1] * self.n for i in range(self.n): if parents[i] + 1: new_parents[i] = parents[parents[i]] dbl.append(new_parents) self.dbl = dbl def lca(self, a, b): if self.depths[a] < self.depths[b]: a, b = b, a for i in range((self.depths[a]-self.depths[b]).bit_length()): if self.depths[a]-self.depths[b] >> i & 1: a = self.dbl[i][a] if a == b: return a for i in range(self.depths[a].bit_length())[::-1]: if self.dbl[i][a] == self.dbl[i][b]: continue a, b = self.dbl[i][a], self.dbl[i][b] assert self.dbl[0][a] == self.dbl[0][b] return self.dbl[0][a] """ atcoder.lca = types.ModuleType('atcoder.lca') exec(_atcoder_lca_code, atcoder.lca.__dict__) LowestCommonAncestor = atcoder.lca.LowestCommonAncestor # from atcoder.lca import LowestCommonAncestor import sys def input(): return sys.stdin.readline().rstrip() DXY = [(0, -1), (1, 0), (0, 1), (-1, 0)] # LDRU mod = 998244353 inf = 1 << 64 def main(): n = int(input()) g = [[] for i in range(n)] lca_g = [[] for i in range(n)] for i in range(n - 1): u, v, w = map(int, input().split()) g[u].append((v, w)) g[v].append((u, w)) lca_g[u].append(v) lca_g[v].append(u) lca = LowestCommonAncestor(lca_g) q = int(input()) Q = [tuple(map(int, input().split())) for i in range(q)] dist = [inf]*(n) S = 0 dist[S] = 0 que = [~S, S] while que: p = que.pop() if p >= 0: for q, w in g[p]: if dist[q] == inf: dist[q] = dist[p] + w que.append(~q) que.append(q) for x, y, z in Q: ans = 0 xy, yz, zx = lca.lca(x, y), lca.lca(y, z), lca.lca(z, x) ans += (dist[x] + dist[y] - 2 * dist[xy]) ans += (dist[y] + dist[z] - 2 * dist[yz]) ans += (dist[z] + dist[x] - 2 * dist[zx]) ans //= 2 print(ans) return 0 if __name__ == "__main__": main()