結果

問題 No.1103 Directed Length Sum
ユーザー takakintakakin
提出日時 2020-07-05 20:50:59
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 761 bytes
コンパイル時間 555 ms
コンパイル使用メモリ 81,648 KB
実行使用メモリ 363,468 KB
最終ジャッジ日時 2023-10-24 04:21:37
合計ジャッジ時間 15,445 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
55,712 KB
testcase_01 AC 40 ms
55,552 KB
testcase_02 AC 1,080 ms
363,468 KB
testcase_03 AC 972 ms
322,532 KB
testcase_04 AC 1,914 ms
232,108 KB
testcase_05 TLE -
testcase_06 AC 1,200 ms
182,332 KB
testcase_07 AC 260 ms
101,056 KB
testcase_08 AC 430 ms
113,976 KB
testcase_09 AC 176 ms
91,248 KB
testcase_10 AC 581 ms
127,544 KB
testcase_11 AC 2,076 ms
233,256 KB
testcase_12 AC 1,194 ms
174,212 KB
testcase_13 AC 581 ms
131,732 KB
testcase_14 AC 149 ms
86,728 KB
testcase_15 AC 925 ms
157,484 KB
testcase_16 AC 2,441 ms
260,068 KB
testcase_17 AC 2,528 ms
263,120 KB
testcase_18 AC 581 ms
128,204 KB
testcase_19 AC 2,127 ms
247,188 KB
testcase_20 AC 208 ms
94,444 KB
testcase_21 AC 365 ms
110,396 KB
testcase_22 AC 1,724 ms
203,388 KB
testcase_23 AC 1,023 ms
152,796 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input=lambda: sys.stdin.readline().rstrip()

n=int(input())
deg=[0]*n
edge=[[] for _ in range(n)]
edge_r=[[] for _ in range(n)]
for _ in range(n-1):
  a,b=map(int,input().split())
  edge[a-1].append(b-1)
  edge_r[b-1].append(a-1)
  deg[b-1]+=1

from collections import deque
TS = list(v for v in range(n) if deg[v]==0)
deq = deque(TS)
used = [0]*n

while deq:
  v = deq.popleft()
  for t in edge[v]:
    deg[t] -= 1
    if deg[t]==0:
      deq.append(t)
      TS.append(t)

L,R=[0]*n,[0]*n
for i in range(n):
  v=TS[i]
  for g in edge[v]:
    L[g]+=L[v]+1

for i in range(n)[::-1]:
  v=TS[i]
  for g in edge_r[v]:
    R[g]+=R[v]+1
ans=0
mod=10**9+7
for i in range(n):
  l=L[i]+1
  r=0
  for g in edge[i]:
    r+=R[g]+1
  ans=(ans+l*r)%mod
print(ans)
0