結果
問題 | No.898 tri-βutree |
ユーザー | tktk_snsn |
提出日時 | 2020-06-05 19:34:29 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,331 ms / 4,000 ms |
コード長 | 1,724 bytes |
コンパイル時間 | 434 ms |
コンパイル使用メモリ | 82,816 KB |
実行使用メモリ | 202,588 KB |
最終ジャッジ日時 | 2024-11-08 23:44:24 |
合計ジャッジ時間 | 22,515 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 270 ms
117,248 KB |
testcase_01 | AC | 46 ms
53,760 KB |
testcase_02 | AC | 59 ms
62,336 KB |
testcase_03 | AC | 59 ms
62,080 KB |
testcase_04 | AC | 59 ms
62,720 KB |
testcase_05 | AC | 58 ms
62,208 KB |
testcase_06 | AC | 60 ms
62,208 KB |
testcase_07 | AC | 1,314 ms
199,936 KB |
testcase_08 | AC | 1,275 ms
200,960 KB |
testcase_09 | AC | 1,281 ms
199,612 KB |
testcase_10 | AC | 1,279 ms
200,056 KB |
testcase_11 | AC | 1,281 ms
199,556 KB |
testcase_12 | AC | 1,295 ms
201,376 KB |
testcase_13 | AC | 1,255 ms
199,544 KB |
testcase_14 | AC | 1,269 ms
200,936 KB |
testcase_15 | AC | 1,293 ms
200,984 KB |
testcase_16 | AC | 1,242 ms
199,764 KB |
testcase_17 | AC | 1,307 ms
202,588 KB |
testcase_18 | AC | 1,325 ms
201,468 KB |
testcase_19 | AC | 1,331 ms
200,280 KB |
testcase_20 | AC | 1,305 ms
201,064 KB |
testcase_21 | AC | 1,290 ms
200,560 KB |
ソースコード
from collections import defaultdict import sys input = sys.stdin.buffer.readline sys.setrecursionlimit(10 ** 7) N = int(input()) D = N.bit_length() edge = [[] for _ in range(N)] for _ in range(N - 1): a, b, c = map(int, input().split()) edge[a].append((b, c)) edge[b].append((a, c)) Q = int(input()) query = [sorted(list(map(int, input().split()))) for _ in range(Q)] root = 0 dist = [-1] * N dist[root] = 0 depth = [0] * N parent = [[-1] * N for _ in range(D)] node = [root] while node: s = node.pop() d = dist[s] dep = depth[s] for t, c in edge[s]: if dist[t] != -1: continue dist[t] = d + c depth[t] = dep + 1 node.append(t) parent[0][t] = s for i in range(D - 1): for j in range(N): parent[i + 1][j] = parent[i][parent[i][j]] def lower_ancester(x, h): for i in reversed(range(D)): if h >= (1 << i): h -= (1 << i) x = parent[i][x] return x def LCA(u, v): if depth[u] < depth[v]: u, v = v, u u = lower_ancester(u, depth[u] - depth[v]) if u == v: return u for i in reversed(range(D)): if parent[i][u] != parent[i][v]: u = parent[i][u] v = parent[i][v] return parent[0][u] path = defaultdict(int) for x, y, z in query: if not path[(x, y)]: lca = LCA(x, y) path[(x, y)] = dist[x] + dist[y] - 2 * dist[lca] if not path[(y, z)]: lca = LCA(y, z) path[(y, z)] = dist[y] + dist[z] - 2 * dist[lca] if not path[(x, z)]: lca = LCA(x, z) path[(x, z)] = dist[x] + dist[z] - 2 * dist[lca] size = (path[(x, y)] + path[(y, z)] + path[(x, z)]) // 2 print(size)