結果
問題 | 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,532 ms / 4,000 ms |
コード長 | 1,724 bytes |
コンパイル時間 | 360 ms |
コンパイル使用メモリ | 13,184 KB |
実行使用メモリ | 121,404 KB |
最終ジャッジ日時 | 2024-05-09 16:33:42 |
合計ジャッジ時間 | 31,080 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,175 ms
74,752 KB |
testcase_01 | AC | 29 ms
11,136 KB |
testcase_02 | AC | 30 ms
11,136 KB |
testcase_03 | AC | 30 ms
10,880 KB |
testcase_04 | AC | 31 ms
10,880 KB |
testcase_05 | AC | 30 ms
11,008 KB |
testcase_06 | AC | 31 ms
11,136 KB |
testcase_07 | AC | 3,427 ms
121,152 KB |
testcase_08 | AC | 3,377 ms
121,156 KB |
testcase_09 | AC | 3,189 ms
121,144 KB |
testcase_10 | AC | 3,401 ms
121,156 KB |
testcase_11 | AC | 3,462 ms
121,288 KB |
testcase_12 | AC | 3,361 ms
121,144 KB |
testcase_13 | AC | 3,454 ms
121,140 KB |
testcase_14 | AC | 3,532 ms
121,136 KB |
testcase_15 | AC | 3,318 ms
121,156 KB |
testcase_16 | AC | 3,501 ms
121,404 KB |
testcase_17 | AC | 3,413 ms
121,356 KB |
testcase_18 | AC | 3,284 ms
121,392 KB |
testcase_19 | AC | 3,271 ms
121,200 KB |
testcase_20 | AC | 3,307 ms
121,404 KB |
testcase_21 | AC | 3,385 ms
121,196 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)