結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
53,344 KB
testcase_01 AC 38 ms
53,344 KB
testcase_02 AC 1,070 ms
269,684 KB
testcase_03 AC 905 ms
313,016 KB
testcase_04 AC 1,395 ms
150,856 KB
testcase_05 AC 2,420 ms
203,372 KB
testcase_06 AC 862 ms
122,916 KB
testcase_07 AC 212 ms
85,616 KB
testcase_08 AC 302 ms
91,872 KB
testcase_09 AC 150 ms
81,860 KB
testcase_10 AC 431 ms
99,696 KB
testcase_11 AC 1,491 ms
157,660 KB
testcase_12 AC 861 ms
123,512 KB
testcase_13 AC 428 ms
98,548 KB
testcase_14 AC 131 ms
80,840 KB
testcase_15 AC 646 ms
109,988 KB
testcase_16 AC 1,664 ms
168,020 KB
testcase_17 AC 1,782 ms
169,372 KB
testcase_18 AC 428 ms
100,016 KB
testcase_19 AC 1,504 ms
157,972 KB
testcase_20 AC 173 ms
83,928 KB
testcase_21 AC 284 ms
92,256 KB
testcase_22 AC 1,227 ms
144,116 KB
testcase_23 AC 723 ms
115,552 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]
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 = sum(depth[v] * dp[v] for v in range(n)) % MOD
print(ans)
0