結果
問題 | No.898 tri-βutree |
ユーザー | Navier_Boltzmann |
提出日時 | 2022-01-03 21:18:11 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,893 bytes |
コンパイル時間 | 190 ms |
コンパイル使用メモリ | 12,928 KB |
実行使用メモリ | 84,324 KB |
最終ジャッジ日時 | 2024-10-13 09:22:50 |
合計ジャッジ時間 | 14,981 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2,941 ms
82,636 KB |
testcase_01 | AC | 29 ms
10,880 KB |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | TLE | - |
testcase_08 | TLE | - |
testcase_09 | TLE | - |
testcase_10 | TLE | - |
testcase_11 | TLE | - |
testcase_12 | TLE | - |
testcase_13 | TLE | - |
testcase_14 | TLE | - |
testcase_15 | TLE | - |
testcase_16 | TLE | - |
testcase_17 | TLE | - |
testcase_18 | TLE | - |
testcase_19 | TLE | - |
testcase_20 | TLE | - |
testcase_21 | TLE | - |
ソースコード
import sys sys.setrecursionlimit(10**6) class LCA(): def __init__(self,e,root=0): self.N = len(e) self.e = e self.root = root self.IN = [-1]*N self.OUT = [-1]*N self.DIST = [-1]*N self.time = 0 self.DIST[root] = 0 self.parents = [-1]*N self.dfs(root) self.depth = [(self.DIST[i],i) for i in range(N)] self.init_val = [-1]*(2*N) for i in range(N): x = self.IN[i] y = self.OUT[i] self.init_val[x] = self.depth[i] self.init_val[y] = self.depth[self.parents[i]] self.T = self.SegTree(self.init_val) def dfs(self,x): self.IN[x] = self.time self.time += 1 for ix in e[x]: if self.DIST[ix] != -1: continue self.parents[ix] = x self.DIST[ix] = self.DIST[x] + 1 self.dfs(ix) self.OUT[x] = self.time self.time += 1 def lca(self,l,r): L = self.IN[l] R = self.IN[r] if R<L: L,R = R,L R += 1 return self.T.query(L,R)[1] def distance_between(self,l,r): p = self.lca(l,r) return self.DIST[l]+ self.DIST[r] - 2*self.DIST[p] def distance_from_root(self,x): return self.DIST[x] class SegTree: """ Segment Tree """ def __init__(self, init_val, segfunc = None, ide_ele= None): """ 初期化 init_val: 配列の初期値 """ if segfunc is None: def segfunc(x,y): return min(x,y) ide_ele = (float('inf'),-1) n = len(init_val) self.segfunc = segfunc self.ide_ele = ide_ele self.num = 1 << (n - 1).bit_length() self.tree = [ide_ele] * 2 * self.num # 配列の値を葉にセット for i in range(n): self.tree[self.num + i] = init_val[i] # 構築していく for i in range(self.num - 1, 0, -1): self.tree[i] = segfunc(self.tree[2 * i], self.tree[2 * i + 1]) def update(self, k, x): """ k番目の値をxに更新 k: index(0-index) x: update value """ k += self.num self.tree[k] = x while k > 1: self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1]) k >>= 1 def query(self, l, r): """ [l, r)のsegfuncしたものを得る l: index(0-index) r: index(0-index) """ res = self.ide_ele l += self.num r += self.num while l < r: if l & 1: res = self.segfunc(res, self.tree[l]) l += 1 if r & 1: res = self.segfunc(res, self.tree[r - 1]) l >>= 1 r >>= 1 return res N = int(input()) e = [[] for _ in range(N)] from collections import defaultdict dist = defaultdict(lambda:0) for _ in range(N-1): a,b,c = map(int,input().split()) e[a].append(b) e[b].append(a) dist[(a,b)] = c dist[(b,a)] = c W = [-1]*N tW = 0 W[0] = 0 def dfs(x): for ix in e[x]: if W[ix] != -1: continue W[ix] = W[x] + dist[(x,ix)] dfs(ix) dfs(0) L = LCA(e) Q = int(input()) for _ in range(Q): x,y,z = map(int,input().split()) p1 = L.lca(x,y) p2 = L.lca(y,z) p = L.lca(p1,p2) print(W[x]+W[y]+W[z]-W[p1]-W[p2]-W[p])