結果
問題 | No.1094 木登り / Climbing tree |
ユーザー | McGregorsh |
提出日時 | 2023-07-13 00:13:03 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 3,382 bytes |
コンパイル時間 | 235 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 475,960 KB |
最終ジャッジ日時 | 2024-09-14 12:54:48 |
合計ジャッジ時間 | 51,422 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 153 ms
88,448 KB |
testcase_01 | AC | 1,924 ms
214,116 KB |
testcase_02 | AC | 1,218 ms
475,960 KB |
testcase_03 | AC | 624 ms
98,560 KB |
testcase_04 | AC | 859 ms
141,608 KB |
testcase_05 | AC | 1,180 ms
195,408 KB |
testcase_06 | AC | 1,097 ms
129,920 KB |
testcase_07 | AC | 1,988 ms
211,236 KB |
testcase_08 | AC | 1,935 ms
211,760 KB |
testcase_09 | AC | 1,927 ms
210,604 KB |
testcase_10 | TLE | - |
testcase_11 | TLE | - |
testcase_12 | TLE | - |
testcase_13 | TLE | - |
testcase_14 | AC | 1,941 ms
211,688 KB |
testcase_15 | AC | 970 ms
167,040 KB |
testcase_16 | AC | 1,666 ms
371,096 KB |
testcase_17 | AC | 1,238 ms
255,616 KB |
testcase_18 | AC | 1,185 ms
220,288 KB |
testcase_19 | AC | 1,446 ms
329,728 KB |
testcase_20 | TLE | - |
testcase_21 | AC | 1,306 ms
256,896 KB |
testcase_22 | TLE | - |
testcase_23 | TLE | - |
testcase_24 | TLE | - |
testcase_25 | AC | 1,946 ms
215,164 KB |
testcase_26 | TLE | - |
ソースコード
import sys from sys import stdin from fractions import Fraction import math from math import ceil, floor, sqrt, pi, factorial, gcd from copy import deepcopy from collections import Counter, deque, defaultdict from heapq import heapify, heappop, heappush from itertools import accumulate, product, combinations, combinations_with_replacement, permutations from bisect import bisect, bisect_left, bisect_right from functools import reduce, lru_cache from decimal import Decimal, getcontext, ROUND_HALF_UP def i_input(): return int(stdin.readline()) def i_map(): return map(int, stdin.readline().split()) def i_list(): return list(i_map()) def s_input(): return stdin.readline()[:-1] def s_map(): return s_input().split() def s_list(): return list(s_map()) def lcm(a, b): return a * b // gcd(a, b) def get_distance(x1, y1, x2, y2): d = sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2) return d def rotate(table): n_fild = [] for x in zip(*table[::-1]): n_fild.append(x) return n_fild sys.setrecursionlimit(10 ** 7) INF = float('inf') MOD = 10 ** 9 + 7 MOD2 = 998244353 alpa = 'abcdefghijklmnopqrstuvwxyz' ALPA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' N = int(input()) nums = [[] for i in range(N)] cou = [0] * N for i in range(N-1): a, b, c = i_map() a -= 1 b -= 1 cou[b] += 2 cou[a] += 2 nums[a].append([b, c]) nums[b].append([a, c]) start = 0 for i in range(N): if cou[i] == 2: start = i break G = [[] for i in range(N)] costs = [INF] * N costs[start] = 0 que = deque() que.append(start) while que: p = que.popleft() for nxt, cost in nums[p]: if costs[nxt] != INF: continue costs[nxt] = cost + costs[p] G[p].append(nxt) que.append(nxt) #print(costs) # Euler Tour の構築 def EuletTour(N, G, start): # 注意!! #G[v]: 頂点Vの子頂点(親頂点は含まない) #start 根。基本は0だが問題によりそれ以外になる。 def dfs(v, d): F[v] = len(S) depth[v] = d S.append(v) for w in G[v]: dfs(w, d+1) S.append(v) S = [] F = [0]*N depth = [0]*N dfs(start, 0) return S, F, depth S, F, depth = EuletTour(N, G, start) # 存在しない範囲は深さが他よりも大きくなるようにする INF = (N, None) # LCAを計算するクエリの前計算 M = 2*N M0 = 2**(M-1).bit_length() data = [INF]*(2*M0) for i, v in enumerate(S): data[M0-1+i] = (depth[v], i) for i in range(M0-2, -1, -1): data[i] = min(data[2*i+1], data[2*i+2]) # LCAの計算 (generatorで最小値を求める) def _query(a, b): yield INF a += M0; b += M0 while a < b: if b & 1: b -= 1 yield data[b-1] if a & 1: yield data[a-1] a += 1 a >>= 1; b >>= 1 # LCAの計算 (外から呼び出す関数) def query(u, v): fu = F[u]; fv = F[v] if fu > fv: fu, fv = fv, fu return S[min(_query(fu, fv+1))[1]] Q = int(input()) for i in range(Q): a, b = i_map() a -= 1 b -= 1 flag = query(a, b) if flag != a and flag != b: c1 = costs[a] - costs[flag] c2 = costs[b] - costs[flag] print(c1 + c2) else: if flag == a: print(costs[b] - costs[a]) else: print(costs[a] - costs[b]) #def main(): #if __name__ == '__main__': # main()