結果
問題 | No.898 tri-βutree |
ユーザー | terasa |
提出日時 | 2022-05-31 10:55:18 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,091 ms / 4,000 ms |
コード長 | 2,651 bytes |
コンパイル時間 | 276 ms |
コンパイル使用メモリ | 82,340 KB |
実行使用メモリ | 206,208 KB |
最終ジャッジ日時 | 2024-09-21 01:12:07 |
合計ジャッジ時間 | 22,140 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 692 ms
206,208 KB |
testcase_01 | AC | 45 ms
55,552 KB |
testcase_02 | AC | 53 ms
63,360 KB |
testcase_03 | AC | 54 ms
62,848 KB |
testcase_04 | AC | 52 ms
62,976 KB |
testcase_05 | AC | 52 ms
63,104 KB |
testcase_06 | AC | 52 ms
62,592 KB |
testcase_07 | AC | 1,081 ms
114,944 KB |
testcase_08 | AC | 1,032 ms
115,456 KB |
testcase_09 | AC | 1,091 ms
115,328 KB |
testcase_10 | AC | 1,055 ms
115,712 KB |
testcase_11 | AC | 1,057 ms
115,812 KB |
testcase_12 | AC | 1,083 ms
115,200 KB |
testcase_13 | AC | 1,051 ms
115,840 KB |
testcase_14 | AC | 1,078 ms
116,224 KB |
testcase_15 | AC | 1,071 ms
115,200 KB |
testcase_16 | AC | 1,076 ms
115,564 KB |
testcase_17 | AC | 1,083 ms
116,040 KB |
testcase_18 | AC | 1,088 ms
115,212 KB |
testcase_19 | AC | 1,088 ms
115,840 KB |
testcase_20 | AC | 1,071 ms
115,328 KB |
testcase_21 | AC | 1,080 ms
115,584 KB |
ソースコード
import sys import pypyjit import itertools import heapq import math from collections import deque, defaultdict import bisect input = sys.stdin.readline sys.setrecursionlimit(10 ** 6) pypyjit.set_param('max_unroll_recursion=-1') def index_lt(a, x): 'return largest index s.t. A[i] < x or -1 if it does not exist' return bisect.bisect_left(a, x) - 1 def index_le(a, x): 'return largest index s.t. A[i] <= x or -1 if it does not exist' return bisect.bisect_right(a, x) - 1 def index_gt(a, x): 'return smallest index s.t. A[i] > x or len(a) if it does not exist' return bisect.bisect_right(a, x) def index_ge(a, x): 'return smallest index s.t. A[i] >= x or len(a) if it does not exist' return bisect.bisect_left(a, x) class LCA: def __init__(self, N, E, root=0): self.N = N self.E = E self.K = N.bit_length() self.par = [[-1 for _ in range(N)] for _ in range(self.K)] self.depth = [None] * N self.order = [None] * N self._cnt = 0 self.cost = [0] * N self.dfs(root, 0, -1) for k in range(self.K - 1): for i in range(self.N): if self.par[k][i] < 0: continue self.par[k + 1][i] = self.par[k][self.par[k][i]] def dfs(self, v, d, p): self.par[0][v] = p self.depth[v] = d self.order[v] = self._cnt self._cnt += 1 for c, dest in self.E[v]: if dest == p: continue self.cost[dest] = self.cost[v] + c self.dfs(dest, d + 1, v) def lca(self, u, v): if self.depth[u] > self.depth[v]: u, v = v, u d = self.depth[v] - self.depth[u] for k in range(self.K): if d & (1 << k): v = self.par[k][v] if u == v: return u for k in range(self.K)[::-1]: if self.par[k][u] != self.par[k][v]: u = self.par[k][u] v = self.par[k][v] return self.par[0][v] def dist(self, u, v): return self.cost[u] + self.cost[v] - 2 * self.cost[self.lca(u, v)] N = int(input()) E = [[] for _ in range(N)] for _ in range(N - 1): u, v, w = map(int, input().split()) E[u].append((w, v)) E[v].append((w, u)) solver = LCA(N, E) Q = int(input()) for _ in range(Q): x, y, z = map(int, input().split()) xo, yo, zo = solver.order[x], solver.order[y], solver.order[z] x, y, z = map(lambda x: x[0], sorted([(x, xo), (y, yo), (z, zo)], key=lambda x: x[1])) print((solver.dist(x, y) + solver.dist(y, z) + solver.dist(z, x)) // 2)