結果
問題 | No.1094 木登り / Climbing tree |
ユーザー | mkawa2 |
提出日時 | 2021-06-10 18:44:59 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,141 ms / 2,000 ms |
コード長 | 2,502 bytes |
コンパイル時間 | 335 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 249,180 KB |
最終ジャッジ日時 | 2024-11-08 07:22:01 |
合計ジャッジ時間 | 26,280 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 44 ms
52,736 KB |
testcase_01 | AC | 1,113 ms
246,704 KB |
testcase_02 | AC | 537 ms
240,528 KB |
testcase_03 | AC | 249 ms
84,608 KB |
testcase_04 | AC | 503 ms
153,900 KB |
testcase_05 | AC | 882 ms
221,464 KB |
testcase_06 | AC | 501 ms
125,676 KB |
testcase_07 | AC | 1,113 ms
246,788 KB |
testcase_08 | AC | 1,107 ms
246,424 KB |
testcase_09 | AC | 1,115 ms
246,892 KB |
testcase_10 | AC | 1,140 ms
246,436 KB |
testcase_11 | AC | 1,141 ms
247,072 KB |
testcase_12 | AC | 1,132 ms
246,636 KB |
testcase_13 | AC | 1,111 ms
243,784 KB |
testcase_14 | AC | 1,118 ms
246,812 KB |
testcase_15 | AC | 375 ms
110,136 KB |
testcase_16 | AC | 642 ms
220,312 KB |
testcase_17 | AC | 474 ms
159,044 KB |
testcase_18 | AC | 412 ms
131,100 KB |
testcase_19 | AC | 582 ms
179,044 KB |
testcase_20 | AC | 1,126 ms
243,544 KB |
testcase_21 | AC | 513 ms
172,928 KB |
testcase_22 | AC | 1,112 ms
246,448 KB |
testcase_23 | AC | 1,115 ms
243,352 KB |
testcase_24 | AC | 1,125 ms
249,180 KB |
testcase_25 | AC | 1,106 ms
246,960 KB |
testcase_26 | AC | 1,102 ms
243,672 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)] dij = [(0, 1), (1, 0), (1, 1), (1, -1)] inf = 10**16 md = 998244353 # md = 10**9+7 class SparseTableMin: def __init__(self, aa): w = len(aa) h = (w-1).bit_length() table = [aa]+[[-1]*w for _ in range(h-1)] tablei1 = table[0] for i in range(1, h): tablei = table[i] for j in range(w-(1 << i)+1): rj = j+(1 << i-1) tablei[j] = min(tablei1[j], tablei1[rj]) tablei1 = tablei self.table = table def min(self, l, r): i = (r-l).bit_length()-1 tablei = self.table[i] Lmin = tablei[l] Rmin = tablei[r-(1 << i)] if Lmin < Rmin: Rmin = Lmin return Rmin class LCA: def __init__(self, et, depth, first): n = len(depth) self.sp = SparseTableMin([(depth[u], u) for u in et]) self.first = first def get_lca(self, u, v): l, r = self.first[u], self.first[v] if l > r: l, r = r, l return self.sp.min(l, r+1)[1] def DFS(to, root=0): stack = [root << 1 | 1] while stack: u, f = divmod(stack.pop(), 2) euler.append(u) if f: first[u] = len(euler)-1 for v, c in to[u]: if v == parent[u]: continue parent[v] = u depth[v] = depth[u]+1 cost[v] = cost[u]+c stack.append(u << 1) stack.append(v << 1 | 1) n = II() to = [[] for _ in range(n)] for _ in range(n-1): u, v, c = LI1() to[u].append((v, c+1)) to[v].append((u, c+1)) cost = [0]*n euler = [] depth = [0]*n first = [0]*n parent = [-1]*n DFS(to) lca = LCA(euler, depth, first) # print(euler) for _ in range(II()): u, v = LI1() w = lca.get_lca(u, v) # print(u,v,w) print(cost[u]+cost[v]-cost[w]*2)