結果

問題 No.2638 Initial fare
ユーザー ゼットゼット
提出日時 2024-02-19 21:37:26
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 970 bytes
コンパイル時間 134 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 149,180 KB
最終ジャッジ日時 2024-02-19 21:37:42
合計ジャッジ時間 6,272 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
55,608 KB
testcase_01 AC 36 ms
55,608 KB
testcase_02 AC 38 ms
55,608 KB
testcase_03 AC 38 ms
55,604 KB
testcase_04 AC 853 ms
139,136 KB
testcase_05 AC 964 ms
137,160 KB
testcase_06 AC 38 ms
55,608 KB
testcase_07 TLE -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
権限があれば一括ダウンロードができます

ソースコード

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])
  for pos in range(len(G[i])-1):
    x=G[i][pos]
    if dist[x]<dist[i]:
      continue
    for pos2 in range(pos+1,len(G[i])):
      y=G[i][pos2]
      if dist[y]<dist[i]:
        continue
      for j in range(2):
        for k in range(2):
          if j+k<=1:
            result+=dp[x][j]*dp[y][k]
print(result)
0