結果

問題 No.1103 Directed Length Sum
ユーザー qibqib
提出日時 2023-04-30 17:21:58
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,513 ms / 3,000 ms
コード長 680 bytes
コンパイル時間 173 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 314,232 KB
最終ジャッジ日時 2024-04-29 22:49:46
合計ジャッジ時間 22,004 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
52,352 KB
testcase_01 AC 40 ms
51,840 KB
testcase_02 AC 1,091 ms
270,208 KB
testcase_03 AC 967 ms
314,232 KB
testcase_04 AC 1,386 ms
143,104 KB
testcase_05 AC 2,513 ms
195,712 KB
testcase_06 AC 856 ms
123,136 KB
testcase_07 AC 224 ms
85,888 KB
testcase_08 AC 341 ms
92,160 KB
testcase_09 AC 173 ms
82,432 KB
testcase_10 AC 452 ms
99,840 KB
testcase_11 AC 1,519 ms
152,192 KB
testcase_12 AC 861 ms
123,648 KB
testcase_13 AC 431 ms
98,816 KB
testcase_14 AC 156 ms
81,152 KB
testcase_15 AC 661 ms
110,848 KB
testcase_16 AC 1,695 ms
157,184 KB
testcase_17 AC 1,837 ms
169,728 KB
testcase_18 AC 419 ms
100,224 KB
testcase_19 AC 1,568 ms
158,208 KB
testcase_20 AC 199 ms
84,224 KB
testcase_21 AC 307 ms
89,984 KB
testcase_22 AC 1,290 ms
144,256 KB
testcase_23 AC 727 ms
115,840 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 = (ans + dp[~ cur] * depth[~ cur]) % MOD

print(ans)
0