結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー qibqib
提出日時 2022-12-13 00:03:34
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 309 ms / 2,000 ms
コード長 676 bytes
コンパイル時間 170 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 103,232 KB
最終ジャッジ日時 2024-04-24 13:27:41
合計ジャッジ時間 9,025 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
51,712 KB
testcase_01 AC 41 ms
51,968 KB
testcase_02 AC 39 ms
52,224 KB
testcase_03 AC 302 ms
89,472 KB
testcase_04 AC 309 ms
89,856 KB
testcase_05 AC 299 ms
90,088 KB
testcase_06 AC 288 ms
89,216 KB
testcase_07 AC 282 ms
89,212 KB
testcase_08 AC 204 ms
85,376 KB
testcase_09 AC 138 ms
80,640 KB
testcase_10 AC 148 ms
81,280 KB
testcase_11 AC 119 ms
78,488 KB
testcase_12 AC 180 ms
82,432 KB
testcase_13 AC 224 ms
84,992 KB
testcase_14 AC 231 ms
85,376 KB
testcase_15 AC 190 ms
82,304 KB
testcase_16 AC 114 ms
77,440 KB
testcase_17 AC 111 ms
77,952 KB
testcase_18 AC 272 ms
88,496 KB
testcase_19 AC 114 ms
78,208 KB
testcase_20 AC 100 ms
77,184 KB
testcase_21 AC 173 ms
82,108 KB
testcase_22 AC 160 ms
81,920 KB
testcase_23 AC 94 ms
77,184 KB
testcase_24 AC 98 ms
77,184 KB
testcase_25 AC 97 ms
76,928 KB
testcase_26 AC 96 ms
77,312 KB
testcase_27 AC 55 ms
62,848 KB
testcase_28 AC 70 ms
69,632 KB
testcase_29 AC 103 ms
77,312 KB
testcase_30 AC 99 ms
77,440 KB
testcase_31 AC 90 ms
77,184 KB
testcase_32 AC 99 ms
77,120 KB
testcase_33 AC 113 ms
79,232 KB
testcase_34 AC 255 ms
95,872 KB
testcase_35 AC 146 ms
84,224 KB
testcase_36 AC 92 ms
78,336 KB
testcase_37 AC 197 ms
103,232 KB
testcase_38 AC 197 ms
102,468 KB
testcase_39 AC 38 ms
52,224 KB
testcase_40 AC 40 ms
52,352 KB
testcase_41 AC 39 ms
52,096 KB
testcase_42 AC 41 ms
52,224 KB
testcase_43 AC 40 ms
52,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(input())
g = [[] for _ in range(n)]
for _ in range(n - 1):
  u, v = map(int, input().split())
  u -= 1
  v -= 1
  g[u].append(v)
  g[v].append(u)

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

print(ans)
0