結果
問題 | No.1094 木登り / Climbing tree |
ユーザー | 双六 |
提出日時 | 2020-08-06 16:10:28 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,063 ms / 2,000 ms |
コード長 | 2,202 bytes |
コンパイル時間 | 330 ms |
コンパイル使用メモリ | 82,584 KB |
実行使用メモリ | 165,816 KB |
最終ジャッジ日時 | 2024-04-25 19:22:27 |
合計ジャッジ時間 | 23,260 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 41 ms
54,744 KB |
testcase_01 | AC | 955 ms
165,656 KB |
testcase_02 | AC | 270 ms
159,228 KB |
testcase_03 | AC | 227 ms
82,396 KB |
testcase_04 | AC | 356 ms
112,712 KB |
testcase_05 | AC | 562 ms
149,440 KB |
testcase_06 | AC | 457 ms
102,952 KB |
testcase_07 | AC | 1,056 ms
164,576 KB |
testcase_08 | AC | 932 ms
164,700 KB |
testcase_09 | AC | 956 ms
164,764 KB |
testcase_10 | AC | 916 ms
164,808 KB |
testcase_11 | AC | 925 ms
164,880 KB |
testcase_12 | AC | 926 ms
164,108 KB |
testcase_13 | AC | 1,038 ms
165,024 KB |
testcase_14 | AC | 1,033 ms
165,056 KB |
testcase_15 | AC | 333 ms
95,356 KB |
testcase_16 | AC | 543 ms
142,212 KB |
testcase_17 | AC | 444 ms
113,316 KB |
testcase_18 | AC | 416 ms
104,860 KB |
testcase_19 | AC | 518 ms
127,740 KB |
testcase_20 | AC | 931 ms
164,956 KB |
testcase_21 | AC | 446 ms
115,444 KB |
testcase_22 | AC | 930 ms
164,888 KB |
testcase_23 | AC | 1,063 ms
165,340 KB |
testcase_24 | AC | 1,052 ms
165,816 KB |
testcase_25 | AC | 935 ms
165,768 KB |
testcase_26 | AC | 1,035 ms
165,512 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()