結果
問題 | No.898 tri-βutree |
ユーザー | mkawa2 |
提出日時 | 2021-03-24 12:15:00 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,742 bytes |
コンパイル時間 | 342 ms |
コンパイル使用メモリ | 12,928 KB |
実行使用メモリ | 62,080 KB |
最終ジャッジ日時 | 2024-05-05 04:52:39 |
合計ジャッジ時間 | 26,439 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,052 ms
62,080 KB |
testcase_01 | AC | 28 ms
11,136 KB |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
ソースコード
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)] 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.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, _ in self.to[u]: if v == pu: continue self.parents[v] = u self.depth[v] = du+1 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] n = II() to = [[] for _ in range(n)] for _ in range(n-1): u, v, w = LI() to[u].append((v, w)) to[v].append((u, w)) lca = LCA(to) stack = [(0, -1)] cost = [0]*n while stack: u, pu = stack.pop() for v, w in to[u]: if v == pu: continue cost[v] = cost[u]+w stack.append((v, u)) def cal(u, v): w = lca.anc(u, v) return w, cost[u]+cost[v]-cost[w]*2 for _ in range(II()): ans = 0 x, y, z = LI() if lca.depth[x] > lca.depth[y]: x, y = y, x if lca.depth[x] > lca.depth[z]: x, z = z, x p, c = cal(y, z) ans += c _, c = cal(p, x) ans += c print(ans)