結果
問題 | No.898 tri-βutree |
ユーザー | Coki628 |
提出日時 | 2020-08-07 18:31:34 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 816 ms / 4,000 ms |
コード長 | 2,567 bytes |
コンパイル時間 | 663 ms |
コンパイル使用メモリ | 82,432 KB |
実行使用メモリ | 110,464 KB |
最終ジャッジ日時 | 2024-11-08 23:48:15 |
合計ジャッジ時間 | 15,652 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 330 ms
108,928 KB |
testcase_01 | AC | 44 ms
53,248 KB |
testcase_02 | AC | 56 ms
61,184 KB |
testcase_03 | AC | 55 ms
60,544 KB |
testcase_04 | AC | 54 ms
60,800 KB |
testcase_05 | AC | 55 ms
60,800 KB |
testcase_06 | AC | 57 ms
60,928 KB |
testcase_07 | AC | 803 ms
110,080 KB |
testcase_08 | AC | 786 ms
109,952 KB |
testcase_09 | AC | 779 ms
109,824 KB |
testcase_10 | AC | 768 ms
109,824 KB |
testcase_11 | AC | 777 ms
110,080 KB |
testcase_12 | AC | 811 ms
109,824 KB |
testcase_13 | AC | 772 ms
109,184 KB |
testcase_14 | AC | 816 ms
109,568 KB |
testcase_15 | AC | 772 ms
109,696 KB |
testcase_16 | AC | 778 ms
109,568 KB |
testcase_17 | AC | 788 ms
109,568 KB |
testcase_18 | AC | 789 ms
109,824 KB |
testcase_19 | AC | 793 ms
110,464 KB |
testcase_20 | AC | 776 ms
110,080 KB |
testcase_21 | AC | 802 ms
109,824 KB |
ソースコード
import sys def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') sys.setrecursionlimit(10 ** 9) INF = 10 ** 19 MOD = 10 ** 9 + 7 EPS = 10 ** -10 class LCA: """ LCA(最小共通祖先) """ def __init__(self, nodes, root): self.N = len(nodes) nv = 1 MAX = 0 while nv < self.N: nv *= 2 MAX += 1 self.MAX = MAX self.nxt = list2d(MAX, self.N, -1) self.depths, self.cost = self.dfs(self.N, nodes, root) for k in range(1, MAX): for v in range(self.N): if self.nxt[k-1][v] == -1: continue self.nxt[k][v] = self.nxt[k-1][self.nxt[k-1][v]] def dfs(self, N, nodes, src): stack = [(src, -1)] depths = [0] * N cost = [0] * N while stack: u, prev = stack.pop() for v, c in nodes[u]: if v != prev: self.nxt[0][v] = u depths[v] = depths[u] + 1 cost[v] = cost[u] + c stack.append((v, u)) return depths, cost def get_lca(self, a, b): if self.depths[a] > self.depths[b]: a, b = b, a gap = self.depths[b] - self.depths[a] for i in range(self.MAX): if gap & 1<<i: b = self.nxt[i][b] if a == b: return a else: for i in range(self.MAX-1, -1, -1): a2 = self.nxt[i][a] b2 = self.nxt[i][b] if a2 != b2: a = a2 b = b2 return self.nxt[0][a] N = INT() nodes = [[] for i in range(N)] for i in range(N-1): a, b, c = MAP() nodes[a].append((b, c)) nodes[b].append((a, c)) lca = LCA(nodes, 0) Q = INT() for _ in range(Q): xyz = LIST() ans = 0 for i in range(3): a, b = xyz[i], xyz[(i+1)%3] l = lca.get_lca(a, b) ans += lca.cost[a] + lca.cost[b] - lca.cost[l]*2 ans //= 2 print(ans)