結果

問題 No.2638 Initial fare
ユーザー ゼット
提出日時 2024-02-19 21:37:26
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 970 bytes
コンパイル時間 347 ms
コンパイル使用メモリ 82,336 KB
実行使用メモリ 149,704 KB
最終ジャッジ日時 2024-09-29 01:33:25
合計ジャッジ時間 6,621 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 4 TLE * 1 -- * 20
権限があれば一括ダウンロードができます

ソースコード

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