結果

問題 No.2638 Initial fare
ユーザー 👑 timitimi
提出日時 2024-02-19 21:51:32
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 541 ms / 2,000 ms
コード長 627 bytes
コンパイル時間 170 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 143,428 KB
最終ジャッジ日時 2024-02-19 21:51:45
合計ジャッジ時間 10,431 ms
ジャッジサーバーID
(参考情報)
judge16 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
55,612 KB
testcase_01 AC 39 ms
55,612 KB
testcase_02 AC 40 ms
55,612 KB
testcase_03 AC 42 ms
55,604 KB
testcase_04 AC 471 ms
113,160 KB
testcase_05 AC 531 ms
111,432 KB
testcase_06 AC 40 ms
55,612 KB
testcase_07 AC 458 ms
119,984 KB
testcase_08 AC 332 ms
123,260 KB
testcase_09 AC 391 ms
130,428 KB
testcase_10 AC 360 ms
130,028 KB
testcase_11 AC 40 ms
55,608 KB
testcase_12 AC 470 ms
118,436 KB
testcase_13 AC 326 ms
117,680 KB
testcase_14 AC 429 ms
122,744 KB
testcase_15 AC 357 ms
122,112 KB
testcase_16 AC 41 ms
55,604 KB
testcase_17 AC 396 ms
113,164 KB
testcase_18 AC 375 ms
116,856 KB
testcase_19 AC 453 ms
114,456 KB
testcase_20 AC 423 ms
113,040 KB
testcase_21 AC 499 ms
116,368 KB
testcase_22 AC 43 ms
55,604 KB
testcase_23 AC 497 ms
138,188 KB
testcase_24 AC 541 ms
136,716 KB
testcase_25 AC 42 ms
55,604 KB
testcase_26 AC 513 ms
134,600 KB
testcase_27 AC 538 ms
143,428 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N=int(input())
D=[[] for i in range(N)]
for i in range(N-1):
  x,y=map(int, input().split())
  x-=1;y-=1
  D[x].append(y)
  D[y].append(x)

ans=N-1
from collections import deque
d=deque()
V=[-1]*N 
V[0]=0
d.append(0)
G=[[] for i in range(N)]
P=[-1]*N
while d:
  now=d.popleft()
  for nex in D[now]:
    if V[nex]==-1:
      G[now].append(nex) 
      V[nex]=V[now]+1 
      P[nex]=now
      d.append(nex)
DD={}
for v in V:
  if v not in DD:
    DD[v]=0
  DD[v]+=1
for i in range(N):
  g=len(G[i])
  if i!=0:
    ans+=g
  ans+=g*(g-1)//2
  if V[i]>=2:
    c=P[P[i]]
    ans+=len(G[c])-1
  if V[i]>=3:
    ans+=1
print(ans)
    

0