結果

問題 No.1103 Directed Length Sum
ユーザー qibqib
提出日時 2023-04-30 17:19:46
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 674 bytes
コンパイル時間 169 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 314,616 KB
最終ジャッジ日時 2024-04-29 22:48:26
合計ジャッジ時間 20,538 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
51,968 KB
testcase_01 AC 40 ms
52,096 KB
testcase_02 WA -
testcase_03 AC 950 ms
314,616 KB
testcase_04 AC 1,302 ms
143,488 KB
testcase_05 AC 2,393 ms
196,096 KB
testcase_06 AC 861 ms
123,008 KB
testcase_07 AC 212 ms
85,760 KB
testcase_08 AC 318 ms
92,544 KB
testcase_09 AC 163 ms
82,432 KB
testcase_10 AC 430 ms
99,968 KB
testcase_11 AC 1,448 ms
152,192 KB
testcase_12 AC 817 ms
123,776 KB
testcase_13 AC 433 ms
98,944 KB
testcase_14 AC 143 ms
81,152 KB
testcase_15 AC 628 ms
110,848 KB
testcase_16 AC 1,553 ms
157,312 KB
testcase_17 AC 1,666 ms
169,856 KB
testcase_18 AC 403 ms
100,224 KB
testcase_19 AC 1,471 ms
158,080 KB
testcase_20 AC 171 ms
84,480 KB
testcase_21 AC 267 ms
89,856 KB
testcase_22 AC 1,195 ms
144,768 KB
testcase_23 AC 662 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])

print(ans)
0