結果
問題 | No.898 tri-βutree |
ユーザー | mkawa2 |
提出日時 | 2021-03-24 12:28:22 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 1,806 ms / 4,000 ms |
コード長 | 2,603 bytes |
コンパイル時間 | 102 ms |
コンパイル使用メモリ | 13,184 KB |
実行使用メモリ | 61,952 KB |
最終ジャッジ日時 | 2024-11-08 23:54:52 |
合計ジャッジ時間 | 31,663 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,328 ms
61,952 KB |
testcase_01 | AC | 32 ms
11,136 KB |
testcase_02 | AC | 33 ms
11,008 KB |
testcase_03 | AC | 33 ms
11,264 KB |
testcase_04 | AC | 34 ms
11,136 KB |
testcase_05 | AC | 34 ms
11,136 KB |
testcase_06 | AC | 34 ms
11,008 KB |
testcase_07 | AC | 1,771 ms
55,296 KB |
testcase_08 | AC | 1,784 ms
55,168 KB |
testcase_09 | AC | 1,774 ms
55,296 KB |
testcase_10 | AC | 1,740 ms
55,296 KB |
testcase_11 | AC | 1,774 ms
55,296 KB |
testcase_12 | AC | 1,785 ms
55,424 KB |
testcase_13 | AC | 1,763 ms
55,168 KB |
testcase_14 | AC | 1,789 ms
55,424 KB |
testcase_15 | AC | 1,784 ms
55,296 KB |
testcase_16 | AC | 1,786 ms
55,168 KB |
testcase_17 | AC | 1,787 ms
55,296 KB |
testcase_18 | AC | 1,806 ms
55,296 KB |
testcase_19 | AC | 1,774 ms
55,296 KB |
testcase_20 | AC | 1,789 ms
55,168 KB |
testcase_21 | AC | 1,752 ms
55,168 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)] 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)) for _ in range(II()): x, y, z = LI() a=lca.anc(x,y) b=lca.anc(x,z) c=lca.anc(z,y) ans = cost[x]+cost[y]+cost[z]-cost[a]-cost[b]-cost[c] print(ans)