結果
問題 | No.898 tri-βutree |
ユーザー | terasa |
提出日時 | 2022-05-31 11:26:56 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 912 ms / 4,000 ms |
コード長 | 2,655 bytes |
コンパイル時間 | 149 ms |
コンパイル使用メモリ | 82,296 KB |
実行使用メモリ | 204,484 KB |
最終ジャッジ日時 | 2024-09-21 01:15:40 |
合計ジャッジ時間 | 17,198 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 592 ms
204,484 KB |
testcase_01 | AC | 45 ms
55,468 KB |
testcase_02 | AC | 52 ms
64,564 KB |
testcase_03 | AC | 52 ms
64,700 KB |
testcase_04 | AC | 52 ms
63,304 KB |
testcase_05 | AC | 52 ms
63,432 KB |
testcase_06 | AC | 53 ms
63,040 KB |
testcase_07 | AC | 866 ms
114,284 KB |
testcase_08 | AC | 879 ms
113,860 KB |
testcase_09 | AC | 876 ms
114,076 KB |
testcase_10 | AC | 879 ms
113,912 KB |
testcase_11 | AC | 849 ms
113,928 KB |
testcase_12 | AC | 906 ms
113,716 KB |
testcase_13 | AC | 861 ms
113,924 KB |
testcase_14 | AC | 897 ms
113,960 KB |
testcase_15 | AC | 875 ms
113,868 KB |
testcase_16 | AC | 871 ms
113,960 KB |
testcase_17 | AC | 871 ms
114,120 KB |
testcase_18 | AC | 905 ms
114,092 KB |
testcase_19 | AC | 912 ms
114,332 KB |
testcase_20 | AC | 877 ms
114,060 KB |
testcase_21 | AC | 887 ms
113,996 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)