結果
問題 | No.1094 木登り / Climbing tree |
ユーザー | 双六 |
提出日時 | 2020-08-06 16:10:28 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,322 ms / 2,000 ms |
コード長 | 2,202 bytes |
コンパイル時間 | 391 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 165,880 KB |
最終ジャッジ日時 | 2024-11-08 07:00:36 |
合計ジャッジ時間 | 27,935 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 47 ms
54,144 KB |
testcase_01 | AC | 1,158 ms
165,520 KB |
testcase_02 | AC | 317 ms
159,308 KB |
testcase_03 | AC | 285 ms
82,176 KB |
testcase_04 | AC | 439 ms
112,640 KB |
testcase_05 | AC | 691 ms
149,192 KB |
testcase_06 | AC | 568 ms
102,912 KB |
testcase_07 | AC | 1,283 ms
164,444 KB |
testcase_08 | AC | 1,172 ms
165,080 KB |
testcase_09 | AC | 1,147 ms
164,712 KB |
testcase_10 | AC | 1,135 ms
164,596 KB |
testcase_11 | AC | 1,148 ms
164,992 KB |
testcase_12 | AC | 1,137 ms
164,360 KB |
testcase_13 | AC | 1,302 ms
164,988 KB |
testcase_14 | AC | 1,302 ms
165,004 KB |
testcase_15 | AC | 438 ms
95,488 KB |
testcase_16 | AC | 648 ms
142,188 KB |
testcase_17 | AC | 538 ms
113,024 KB |
testcase_18 | AC | 501 ms
105,216 KB |
testcase_19 | AC | 613 ms
127,744 KB |
testcase_20 | AC | 1,169 ms
164,736 KB |
testcase_21 | AC | 558 ms
115,328 KB |
testcase_22 | AC | 1,136 ms
164,760 KB |
testcase_23 | AC | 1,291 ms
165,620 KB |
testcase_24 | AC | 1,304 ms
165,880 KB |
testcase_25 | AC | 1,174 ms
165,652 KB |
testcase_26 | AC | 1,322 ms
165,404 KB |
ソースコード
import sys; input = sys.stdin.buffer.readline sys.setrecursionlimit(10**7) from collections import defaultdict con = 10 ** 9 + 7; INF = float("inf") from collections import deque def getlist(): return list(map(int, input().split())) class Graph(object): def __init__(self): self.graph = defaultdict(list) def __len__(self): return len(self.graph) def add_edge(self, a, b, w): self.graph[a].append((b, w)) class BFS(object): def __init__(self, graph, N, s): self.g = graph.graph self.Q = deque(); self.Q.append(s) self.depth = [INF] * N; self.depth[s] = 0 self.prev = [None] * N; self.prev[s] = -1 self.dist = [INF] * N; self.dist[s] = 0 #深さ、距離、親確定 while self.Q: v = self.Q.popleft() for i, w in self.g[v]: if self.depth[i] == INF: self.depth[i] = self.depth[v] + 1 self.dist[i] = self.dist[v] + w self.prev[i] = v self.Q.append(i) #ダブリング self.nex = [[0] * N for i in range(N.bit_length())] for i in range(N): self.nex[0][i] = self.prev[i] for i in range(1, N.bit_length()): for j in range(N): var = self.nex[i - 1][j] if var == -1: self.nex[i][j] = -1 else: self.nex[i][j] = self.nex[i - 1][var] def LCA(a, b, N, depth, nex): x, y = a, b #深さ揃え if depth[x] > depth[y]: x, y = y, x if depth[x] < depth[y]: dif = depth[y] - depth[x] X = dif.bit_length() for i in range(X): if dif & 1 == 1: y = nex[i][y] dif = dif >> 1 #深さ同じで一致するなら終了 if x == y: lca = x return lca #LCA計算 for i in range(N.bit_length() - 1, -1, -1): if nex[i][x] != nex[i][y]: x = nex[i][x]; y = nex[i][y] lca = nex[0][x] return lca #処理内容 def main(): N = int(input()) G = Graph() for i in range(N - 1): a, b, w = getlist() a -= 1; b -= 1 G.add_edge(a, b, w) G.add_edge(b, a, w) BF = BFS(G, N, 0) depth, nex, dist = BF.depth, BF.nex, BF.dist #頂点a-bの距離 Q = int(input()) for i in range(Q): a, b = getlist() a -= 1; b -= 1 dis = dist[a] + dist[b] - 2 * dist[LCA(a, b, N, depth, nex)] # print(depth[a], dist[b], 2 * depth[LCA(a, b, N, depth, nex)]) print(dis) if __name__ == '__main__': main()