結果
問題 | No.1094 木登り / Climbing tree |
ユーザー | ayaoni |
提出日時 | 2021-01-13 06:31:28 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,044 ms / 2,000 ms |
コード長 | 2,054 bytes |
コンパイル時間 | 908 ms |
コンパイル使用メモリ | 82,444 KB |
実行使用メモリ | 146,464 KB |
最終ジャッジ日時 | 2024-04-25 19:36:01 |
合計ジャッジ時間 | 22,069 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 36 ms
54,936 KB |
testcase_01 | AC | 915 ms
146,464 KB |
testcase_02 | AC | 239 ms
142,616 KB |
testcase_03 | AC | 217 ms
81,512 KB |
testcase_04 | AC | 326 ms
106,200 KB |
testcase_05 | AC | 534 ms
134,644 KB |
testcase_06 | AC | 433 ms
98,180 KB |
testcase_07 | AC | 1,044 ms
145,812 KB |
testcase_08 | AC | 945 ms
144,052 KB |
testcase_09 | AC | 1,042 ms
143,972 KB |
testcase_10 | AC | 1,035 ms
144,040 KB |
testcase_11 | AC | 1,011 ms
144,144 KB |
testcase_12 | AC | 885 ms
143,844 KB |
testcase_13 | AC | 899 ms
143,940 KB |
testcase_14 | AC | 923 ms
143,740 KB |
testcase_15 | AC | 320 ms
92,044 KB |
testcase_16 | AC | 548 ms
128,644 KB |
testcase_17 | AC | 441 ms
107,052 KB |
testcase_18 | AC | 375 ms
99,928 KB |
testcase_19 | AC | 495 ms
118,676 KB |
testcase_20 | AC | 1,018 ms
144,252 KB |
testcase_21 | AC | 457 ms
109,136 KB |
testcase_22 | AC | 920 ms
143,888 KB |
testcase_23 | AC | 891 ms
146,188 KB |
testcase_24 | AC | 890 ms
146,196 KB |
testcase_25 | AC | 875 ms
146,000 KB |
testcase_26 | AC | 910 ms
145,756 KB |
ソースコード
import sys from collections import deque sys.setrecursionlimit(10**7) def I(): return int(sys.stdin.readline().rstrip()) def MI(): return map(int,sys.stdin.readline().rstrip().split()) def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) def LI2(): return list(map(int,sys.stdin.readline().rstrip())) def S(): return sys.stdin.readline().rstrip() def LS(): return list(sys.stdin.readline().rstrip().split()) def LS2(): return list(sys.stdin.readline().rstrip()) N = I() Graph = [[] for _ in range(N)] # 0-index for _ in range(N-1): a,b,c = MI() a -= 1 b -= 1 Graph[a].append((b,c)) Graph[b].append((a,c)) # 0を頂点とした根付き木 depth = [-1]*N parent = [-1]*N dist = [-1]*N # 根からの距離 depth[0] = 0 dist[0] = 0 deq = deque([(0,-1)]) while deq: i,p = deq.pop() for j,c in Graph[i]: if j == p: continue depth[j] = depth[i]+1 parent[j] = i dist[j] = dist[i]+c deq.appendleft((j,i)) K = (N-1).bit_length()+1 ancestor = [[-1]*N for k in range(K)] # parents[k][x] = 頂点xの2**k世代先祖 for k in range(K): if k == 0: ancestor[k] = parent else: for x in range(N): if ancestor[k-1][x] == -1: ancestor[k][x] = -1 else: ancestor[k][x] = ancestor[k-1][ancestor[k-1][x]] def lca(x,y): # xとyの最近共通祖先 depth_difference = depth[x] - depth[y] if depth_difference < 0: x,y = y,x depth_difference = -depth_difference # depth[x] >= depth[y] としてよい for k in range(K): if depth_difference & 1: x = ancestor[k][x] depth_difference >>= 1 # depth[x] == depth[y] としてよい if x == y: return x for k in range(K-1,-1,-1): px,py = ancestor[k][x],ancestor[k][y] if px != py: x,y = px,py return ancestor[0][x] Q = I() for _ in range(Q): s,t = MI() s -= 1 t -= 1 a = lca(s,t) print(dist[s]+dist[t]-2*dist[a])