結果

問題 No.1103 Directed Length Sum
ユーザー qibqib
提出日時 2022-11-18 19:10:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,483 ms / 3,000 ms
コード長 680 bytes
コンパイル時間 282 ms
コンパイル使用メモリ 80,240 KB
実行使用メモリ 313,256 KB
最終ジャッジ日時 2023-10-20 04:41:08
合計ジャッジ時間 22,326 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
51,792 KB
testcase_01 AC 36 ms
51,768 KB
testcase_02 AC 1,137 ms
266,496 KB
testcase_03 AC 1,040 ms
313,256 KB
testcase_04 AC 1,399 ms
142,760 KB
testcase_05 AC 2,483 ms
195,488 KB
testcase_06 AC 864 ms
122,764 KB
testcase_07 AC 212 ms
85,548 KB
testcase_08 AC 322 ms
91,800 KB
testcase_09 AC 153 ms
81,788 KB
testcase_10 AC 434 ms
99,544 KB
testcase_11 AC 1,494 ms
151,832 KB
testcase_12 AC 882 ms
123,356 KB
testcase_13 AC 445 ms
98,388 KB
testcase_14 AC 137 ms
80,780 KB
testcase_15 AC 667 ms
109,708 KB
testcase_16 AC 1,626 ms
156,860 KB
testcase_17 AC 1,799 ms
169,212 KB
testcase_18 AC 445 ms
99,824 KB
testcase_19 AC 1,591 ms
157,788 KB
testcase_20 AC 184 ms
83,876 KB
testcase_21 AC 285 ms
89,420 KB
testcase_22 AC 1,283 ms
143,936 KB
testcase_23 AC 718 ms
115,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 10 ** 9 + 7

n = int(input())
g = [[] for _ in range(n)]
par = [None for _ in range(n)]
for _ in range(n - 1):
  a, b = map(int, input().split())
  a -= 1
  b -= 1
  g[a].append(b)
  par[b] = a

src = None
for v in range(n):
  if par[v] is None:
    src = v
    break

dp = [0 for _ in range(n)]
depth = [None for _ in range(n)]
depth[src] = 0
st = [~ src, src]
ans = 0
while len(st) > 0:
  cur = st.pop()
  if cur >= 0:
    for nxt in g[cur]:
      depth[nxt] = depth[cur] + 1
      st.append(~ nxt)
      st.append(nxt)
  else:
    dp[~ cur] += 1
    if not par[~ cur] is None:
      dp[par[~ cur]] += dp[~ cur]
      ans += depth[~ cur] * dp[~ cur]

ans %= MOD
print(ans)
0