結果
問題 | No.1094 木登り / Climbing tree |
ユーザー | mkawa2 |
提出日時 | 2021-06-10 16:38:07 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,022 ms / 2,000 ms |
コード長 | 2,562 bytes |
コンパイル時間 | 350 ms |
コンパイル使用メモリ | 82,432 KB |
実行使用メモリ | 140,800 KB |
最終ジャッジ日時 | 2024-11-08 07:21:02 |
合計ジャッジ時間 | 24,486 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 44 ms
52,480 KB |
testcase_01 | AC | 980 ms
129,944 KB |
testcase_02 | AC | 266 ms
140,800 KB |
testcase_03 | AC | 278 ms
80,420 KB |
testcase_04 | AC | 411 ms
99,724 KB |
testcase_05 | AC | 575 ms
122,240 KB |
testcase_06 | AC | 501 ms
94,648 KB |
testcase_07 | AC | 991 ms
129,348 KB |
testcase_08 | AC | 989 ms
130,724 KB |
testcase_09 | AC | 961 ms
129,516 KB |
testcase_10 | AC | 944 ms
129,044 KB |
testcase_11 | AC | 943 ms
129,240 KB |
testcase_12 | AC | 983 ms
129,552 KB |
testcase_13 | AC | 982 ms
129,520 KB |
testcase_14 | AC | 965 ms
129,376 KB |
testcase_15 | AC | 458 ms
91,600 KB |
testcase_16 | AC | 639 ms
127,272 KB |
testcase_17 | AC | 569 ms
106,328 KB |
testcase_18 | AC | 526 ms
99,512 KB |
testcase_19 | AC | 608 ms
118,736 KB |
testcase_20 | AC | 1,004 ms
129,804 KB |
testcase_21 | AC | 551 ms
108,288 KB |
testcase_22 | AC | 1,022 ms
128,860 KB |
testcase_23 | AC | 969 ms
130,232 KB |
testcase_24 | AC | 972 ms
130,352 KB |
testcase_25 | AC | 971 ms
129,944 KB |
testcase_26 | AC | 991 ms
129,936 KB |
ソースコード
import sys # sys.setrecursionlimit(10**6) int1 = lambda x: int(x)-1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.buffer.readline()) def LI(): return list(map(int, sys.stdin.buffer.readline().split())) def LI1(): return list(map(int1, sys.stdin.buffer.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def BI(): return sys.stdin.buffer.readline().rstrip() def SI(): return sys.stdin.buffer.readline().rstrip().decode() # dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] # dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] dij = [(0, 1), (1, 0), (1, 1), (1, -1)] inf = 10**16 md = 998244353 # md = 10**9+7 class LCA: # 頂点は0~n-1 def __init__(self, to, root=0): self.to = to self.n = len(to) self.parents = [-1]*(self.n+1) self.cost = [0]*self.n self.depth = [0]*self.n self.__dfs(root) self.max_level = max(self.depth).bit_length() self.ancestor = [self.parents]+[[-1]*(self.n+1) for _ in range(self.max_level)] row0 = self.ancestor[0] for lv in range(self.max_level): row1 = self.ancestor[lv+1] for u in range(self.n): row1[u] = row0[row0[u]] row0 = row1 def __dfs(self, root): stack = [root] while stack: u = stack.pop() pu = self.parents[u] du = self.depth[u] for v, c in self.to[u]: if v == pu: continue self.parents[v] = u self.depth[v] = du+1 self.cost[v] = self.cost[u]+c stack.append(v) # 最小共通祖先 def anc(self, u, v): diff = self.depth[u]-self.depth[v] if diff < 0: u, v = v, u diff = abs(diff) lv = 0 while diff: if diff & 1: u = self.ancestor[lv][u] lv, diff = lv+1, diff >> 1 if u == v: return u for lv in range(self.depth[u].bit_length()-1, -1, -1): anclv = self.ancestor[lv] if anclv[u] != anclv[v]: u, v = anclv[u], anclv[v] return self.parents[u] def get_cost(self, u, v): w = self.anc(u, v) return self.cost[u]+self.cost[v]-2*self.cost[w] n = II() to = [[] for _ in range(n)] for _ in range(n-1): u, v, c = LI1() to[u].append((v, c+1)) to[v].append((u, c+1)) lca = LCA(to) for _ in range(II()): u, v = LI1() print(lca.get_cost(u, v))