結果
問題 | No.898 tri-βutree |
ユーザー | tktk_snsn |
提出日時 | 2020-06-05 19:33:57 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 3,789 ms / 4,000 ms |
コード長 | 1,724 bytes |
コンパイル時間 | 127 ms |
コンパイル使用メモリ | 10,960 KB |
実行使用メモリ | 118,740 KB |
最終ジャッジ日時 | 2023-08-22 10:53:21 |
合計ジャッジ時間 | 62,789 ms |
ジャッジサーバーID (参考情報) |
judge11 / judge15 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,084 ms
72,412 KB |
testcase_01 | AC | 19 ms
8,832 KB |
testcase_02 | AC | 20 ms
8,708 KB |
testcase_03 | AC | 21 ms
8,808 KB |
testcase_04 | AC | 21 ms
8,812 KB |
testcase_05 | AC | 21 ms
8,876 KB |
testcase_06 | AC | 20 ms
8,796 KB |
testcase_07 | AC | 3,653 ms
118,668 KB |
testcase_08 | AC | 3,615 ms
118,720 KB |
testcase_09 | AC | 3,726 ms
118,656 KB |
testcase_10 | AC | 3,479 ms
118,692 KB |
testcase_11 | AC | 3,688 ms
118,660 KB |
testcase_12 | AC | 3,692 ms
118,660 KB |
testcase_13 | AC | 3,695 ms
118,652 KB |
testcase_14 | AC | 3,674 ms
118,716 KB |
testcase_15 | AC | 3,746 ms
118,624 KB |
testcase_16 | AC | 3,789 ms
118,740 KB |
testcase_17 | AC | 3,694 ms
118,624 KB |
testcase_18 | AC | 3,630 ms
118,728 KB |
testcase_19 | AC | 3,628 ms
118,740 KB |
testcase_20 | AC | 3,745 ms
118,720 KB |
testcase_21 | AC | 3,719 ms
118,736 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)