結果

問題 No.2638 Initial fare
ユーザー ゼットゼット
提出日時 2024-02-19 21:49:43
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,052 bytes
コンパイル時間 510 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 146,700 KB
最終ジャッジ日時 2024-02-19 21:50:11
合計ジャッジ時間 21,288 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

N=int(input())
G=[[] for i in range(N)]
for i in range(N-1):
  a,b=map(int,input().split())
  G[a-1].append(b-1)
  G[b-1].append(a-1)
from collections import deque
S=deque()
S.append(0)
dist=[-1]*N
dist[0]=0
root=[-1]*N
while S:
  x=S.pop()
  for y in G[x]:
    if dist[y]>=0:
      continue
    dist[y]=dist[x]+1
    root[y]=x
    S.append(y)
dp=[[0]*4 for i in range(N)]
L=[]
for i in range(N):
  L.append((dist[i],i))
L.sort(reverse=True)
result=0
for i in range(N):
  pos=L[i][1]
  for y in G[pos]:
    if dist[y]<dist[pos]:
      continue
    for j in range(3):
      dp[pos][j+1]+=dp[y][j]
  dp[pos][0]=1
for i in range(N):
  result+=sum(dp[i][1:4])
  M=len(G[i])
  v0=[0]*(len(G[i]))
  v1=[0]*M
  for j in range(M):
    y=G[i][j]
    if dist[y]<dist[i]:
      continue
    v0[j]+=dp[y][0]
    v1[j]+=dp[y][1]
  for j in range(1,M):
    v0[j]+=v0[j-1]
    v1[j]+=v1[j-1]
  for j in range(1,M):
    y=G[i][j]
    if dist[y]<dist[i]:
      continue
    result+=dp[y][0]*(v0[j-1]+v1[j-1])
    result+=dp[y][1]*(v0[j-1])
  print(result)
print(result)
0