結果
問題 | No.898 tri-βutree |
ユーザー | tamato |
提出日時 | 2020-02-18 11:37:23 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,469 ms / 4,000 ms |
コード長 | 2,861 bytes |
コンパイル時間 | 303 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 160,132 KB |
最終ジャッジ日時 | 2024-11-08 23:26:35 |
合計ジャッジ時間 | 24,153 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 520 ms
150,492 KB |
testcase_01 | AC | 44 ms
52,736 KB |
testcase_02 | AC | 53 ms
60,032 KB |
testcase_03 | AC | 52 ms
60,288 KB |
testcase_04 | AC | 52 ms
60,160 KB |
testcase_05 | AC | 53 ms
60,800 KB |
testcase_06 | AC | 52 ms
60,544 KB |
testcase_07 | AC | 1,347 ms
158,476 KB |
testcase_08 | AC | 1,394 ms
159,480 KB |
testcase_09 | AC | 1,469 ms
160,132 KB |
testcase_10 | AC | 1,414 ms
159,568 KB |
testcase_11 | AC | 1,391 ms
159,352 KB |
testcase_12 | AC | 1,277 ms
157,956 KB |
testcase_13 | AC | 1,337 ms
157,660 KB |
testcase_14 | AC | 1,348 ms
156,644 KB |
testcase_15 | AC | 1,210 ms
156,416 KB |
testcase_16 | AC | 1,312 ms
158,624 KB |
testcase_17 | AC | 1,255 ms
158,148 KB |
testcase_18 | AC | 1,201 ms
157,712 KB |
testcase_19 | AC | 1,248 ms
158,948 KB |
testcase_20 | AC | 1,378 ms
159,376 KB |
testcase_21 | AC | 1,311 ms
157,404 KB |
ソースコード
def main(): import sys input = sys.stdin.readline # min def STfunc(a, b): if a < b: return a else: return b # クエリは0-indexedで[l, r) class SparseTable(): def __init__(self, A): # A: 処理したい数列 self.N = len(A) self.K = self.N.bit_length() - 1 self.table = [[0] * (self.K + 1) for _ in range(self.N)] for i, a in enumerate(A): self.table[i][0] = A[i] for k in range(1, self.K + 1): for i in range(self.N): j = i + (1 << (k - 1)) if j <= self.N - 1: self.table[i][k] = STfunc(self.table[i][k - 1], self.table[j][k - 1]) else: self.table[i][k] = self.table[i][k - 1] def query(self, l, r): # [l, r)の最小値を求める k = (r - l).bit_length() - 1 return STfunc(self.table[l][k], self.table[r - (1 << k)][k]) # adj[0] must be empty list def EulerTour(adj, root): st = [root] ret = [] seen = [0] * len(adj) par = [0] * len(adj) depth = [0] * len(adj) dist_from_root = [0] * len(adj) while st: v = st.pop() if seen[v]: ret.append(v) continue ret.append(v) seen[v] = 1 if par[v] != 0: st.append(par[v]) for u in adj[v]: if seen[u] == 0: st.append(u) par[u] = v depth[u] = depth[v] + 1 dist_from_root[u] = dist_from_root[v] + weight[v*(N+1)+u] return ret, depth, dist_from_root N = int(input()) adj = [[] for _ in range(N+1)] weight = {} for i in range(N-1): u, v, w = map(int, input().split()) u += 1 v += 1 adj[u].append(v) adj[v].append(u) weight[u*(N+1)+v] = w weight[v*(N+1)+u] = w et, depth, dist_from_root = EulerTour(adj, 1) depth_list = [0] * len(et) left = [-1] * (N + 1) right = [-1] * (N + 1) for i, v in enumerate(et): depth_list[i] = depth[v] * (N+1) + v if left[v] < 0: left[v] = i right[v] = i ST = SparseTable(depth_list) def dist(a, b): lca_info = ST.query(min(left[a], left[b]), max(right[a], right[b]) + 1) lca_depth, lca_v = divmod(lca_info, N+1) return dist_from_root[a] + dist_from_root[b] - dist_from_root[lca_v]*2 for _ in range(int(input())): x, y, z = map(int, input().split()) x += 1 y += 1 z += 1 print((dist(x, y) + dist(y, z) + dist(z, x)) // 2) if __name__ == '__main__': main()