結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
55,608 KB
testcase_01 AC 41 ms
55,608 KB
testcase_02 AC 36 ms
55,608 KB
testcase_03 AC 37 ms
55,604 KB
testcase_04 AC 987 ms
139,132 KB
testcase_05 AC 1,058 ms
137,160 KB
testcase_06 AC 38 ms
55,608 KB
testcase_07 AC 960 ms
142,504 KB
testcase_08 AC 552 ms
143,772 KB
testcase_09 AC 754 ms
146,028 KB
testcase_10 AC 588 ms
146,700 KB
testcase_11 AC 38 ms
55,604 KB
testcase_12 AC 920 ms
141,588 KB
testcase_13 AC 675 ms
137,752 KB
testcase_14 AC 829 ms
146,504 KB
testcase_15 AC 760 ms
146,032 KB
testcase_16 AC 43 ms
55,592 KB
testcase_17 AC 1,045 ms
142,848 KB
testcase_18 AC 796 ms
140,152 KB
testcase_19 AC 1,140 ms
142,088 KB
testcase_20 AC 1,157 ms
141,060 KB
testcase_21 AC 1,122 ms
140,420 KB
testcase_22 AC 39 ms
55,604 KB
testcase_23 AC 1,162 ms
138,776 KB
testcase_24 AC 1,259 ms
140,360 KB
testcase_25 AC 39 ms
55,604 KB
testcase_26 AC 1,282 ms
133,536 KB
testcase_27 AC 1,323 ms
137,028 KB
権限があれば一括ダウンロードができます

ソースコード

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)
0