結果

問題 No.1103 Directed Length Sum
ユーザー qibqib
提出日時 2023-04-30 17:15:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,700 ms / 3,000 ms
コード長 723 bytes
コンパイル時間 316 ms
コンパイル使用メモリ 82,496 KB
実行使用メモリ 305,628 KB
最終ジャッジ日時 2024-04-29 22:46:02
合計ジャッジ時間 17,384 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
52,224 KB
testcase_01 AC 35 ms
52,096 KB
testcase_02 AC 917 ms
282,932 KB
testcase_03 AC 778 ms
305,628 KB
testcase_04 AC 916 ms
145,280 KB
testcase_05 AC 1,700 ms
200,192 KB
testcase_06 AC 613 ms
124,672 KB
testcase_07 AC 159 ms
85,964 KB
testcase_08 AC 235 ms
91,136 KB
testcase_09 AC 125 ms
82,176 KB
testcase_10 AC 330 ms
100,608 KB
testcase_11 AC 1,018 ms
154,112 KB
testcase_12 AC 603 ms
125,568 KB
testcase_13 AC 312 ms
99,072 KB
testcase_14 AC 114 ms
80,896 KB
testcase_15 AC 438 ms
108,828 KB
testcase_16 AC 1,148 ms
158,336 KB
testcase_17 AC 1,263 ms
174,208 KB
testcase_18 AC 310 ms
101,232 KB
testcase_19 AC 1,083 ms
162,048 KB
testcase_20 AC 136 ms
84,048 KB
testcase_21 AC 197 ms
89,728 KB
testcase_22 AC 860 ms
148,224 KB
testcase_23 AC 505 ms
117,024 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 10 ** 9 + 7

n = int(input())

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

src = None
for v in range(n):
  if deg[v] == 0:
    src = v
    break

dist = [None for _ in range(n)]
dist[src] = 0
par = [None for _ in range(n)]
dp = [0 for _ in range(n)]
ans = 0
st = [~ src, src]
while len(st) > 0:
  cur = st.pop()
  if cur >= 0:
    for nxt in g[cur]:
      dist[nxt] = dist[cur] + 1
      par[nxt] = cur
      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] * dist[~ cur]) % MOD

print(ans)
0