結果

問題 No.898 tri-βutree
ユーザー persimmon-persimmonpersimmon-persimmon
提出日時 2021-06-17 08:43:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,409 ms / 4,000 ms
コード長 2,512 bytes
コンパイル時間 661 ms
コンパイル使用メモリ 87,160 KB
実行使用メモリ 244,112 KB
最終ジャッジ日時 2023-08-30 17:18:33
合計ジャッジ時間 27,789 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 833 ms
244,112 KB
testcase_01 AC 69 ms
71,596 KB
testcase_02 AC 79 ms
75,928 KB
testcase_03 AC 80 ms
76,168 KB
testcase_04 AC 78 ms
76,068 KB
testcase_05 AC 80 ms
76,016 KB
testcase_06 AC 79 ms
76,144 KB
testcase_07 AC 1,318 ms
157,012 KB
testcase_08 AC 1,391 ms
157,956 KB
testcase_09 AC 1,317 ms
155,364 KB
testcase_10 AC 1,325 ms
158,384 KB
testcase_11 AC 1,371 ms
157,784 KB
testcase_12 AC 1,324 ms
156,856 KB
testcase_13 AC 1,300 ms
155,600 KB
testcase_14 AC 1,317 ms
155,804 KB
testcase_15 AC 1,393 ms
159,012 KB
testcase_16 AC 1,394 ms
158,416 KB
testcase_17 AC 1,304 ms
156,816 KB
testcase_18 AC 1,361 ms
157,000 KB
testcase_19 AC 1,341 ms
157,532 KB
testcase_20 AC 1,409 ms
158,412 KB
testcase_21 AC 1,360 ms
156,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def main0(n,uvw,q,xyz):
  ki=[[] for _ in range(n)]
  for u,v,w in uvw:
    ki[u].append([v,w])
    ki[v].append([u,w])
  
import sys
sys.setrecursionlimit(10**7)
def main1(n,uvw,q,xyz):
  ki=[[] for _ in range(n)]
  for u,v,w in uvw:
    ki[u].append([v,w])
    ki[v].append([u,w])

  S=[] # Euler Tour
  F=[0]*n # F[v]:vにはじめて訪れるステップ
  depth=[0]*n # 0を根としたときの深さ
  weight=[0]*n
  def dfs(v,pare,d):
      F[v]=len(S)
      depth[v]=d
      S.append(v)
      for u,w in ki[v]:
          if u==pare:continue
          weight[u]=weight[v]+w
          dfs(u,v,d+1)
          S.append(v)
  dfs(0,-1,0)
  # Sをセグメント木に乗せる
  # u,vのLCAを求める:S[F[u]:F[v]+1]のなかでdepthが最小の頂点を探せば良い
  # F[u]:uに初めてたどるつくステップ
  # S[F[u]:F[v]+1]:はじめてuにたどり着いてつぎにvにたどるつくまでに訪れる頂点
  # 存在しない範囲は深さが他よりも大きくなるようにする
  INF = (n, None)
  # LCAを計算するクエリの前計算
  M = 2*n
  M0 = 2**(M-1).bit_length() # M以上で最小の2のべき乗
  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]]
  
  ret=[]
  for x,y,z in xyz:
    a=query(x,y)
    b=query(y,z)
    c=query(z,x)
    if a==b==c:
      tmp=weight[x]
      tmp+=weight[y]
      tmp+=weight[z]
      tmp-=weight[a]*3
    else:
      if a==b:
        d=a
        e=c
      elif b==c:
        d=b
        e=a
      elif a==c:
        d=a
        e=b
      tmp=weight[x]
      tmp+=weight[y]
      tmp+=weight[z]
      tmp-=weight[e]
      tmp-=weight[d]*2
      #print((x,y,z),(a,b,c),(d,e),tmp)
    ret.append(tmp)
  #print(weight)
  return ret

if __name__=='__main__':
  n=int(input())
  uvw=[list(map(int,input().split())) for _ in range(n-1)]
  q=int(input())
  xyz=[list(map(int,input().split())) for _ in range(q)]
  #ret0=main0(n,uvw,q,xyz)
  ret1=main1(n,uvw,q,xyz)
  #print(ret0)
  print(*ret1,sep="\n")


0