結果

問題 No.1103 Directed Length Sum
ユーザー qibqib
提出日時 2022-11-18 19:08:43
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,404 ms / 3,000 ms
コード長 682 bytes
コンパイル時間 430 ms
コンパイル使用メモリ 81,908 KB
実行使用メモリ 313,960 KB
最終ジャッジ日時 2023-10-20 04:39:43
合計ジャッジ時間 24,905 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,468 KB
testcase_01 AC 36 ms
53,468 KB
testcase_02 AC 1,136 ms
269,284 KB
testcase_03 AC 1,025 ms
313,960 KB
testcase_04 AC 1,418 ms
142,876 KB
testcase_05 AC 2,404 ms
195,560 KB
testcase_06 AC 848 ms
122,880 KB
testcase_07 AC 196 ms
85,684 KB
testcase_08 AC 292 ms
91,952 KB
testcase_09 AC 148 ms
81,920 KB
testcase_10 AC 420 ms
99,664 KB
testcase_11 AC 1,429 ms
151,952 KB
testcase_12 AC 838 ms
123,472 KB
testcase_13 AC 417 ms
98,496 KB
testcase_14 AC 131 ms
80,916 KB
testcase_15 AC 661 ms
109,844 KB
testcase_16 AC 1,585 ms
156,920 KB
testcase_17 AC 1,783 ms
169,340 KB
testcase_18 AC 428 ms
99,920 KB
testcase_19 AC 1,538 ms
157,912 KB
testcase_20 AC 172 ms
84,012 KB
testcase_21 AC 273 ms
89,556 KB
testcase_22 AC 1,262 ms
143,956 KB
testcase_23 AC 718 ms
115,460 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 + depth[~ cur] * dp[~ cur]) % MOD

print(ans)
0