結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー wolgnikwolgnik
提出日時 2021-03-05 22:10:36
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 222 ms / 2,000 ms
コード長 649 bytes
コンパイル時間 369 ms
コンパイル使用メモリ 82,464 KB
実行使用メモリ 103,144 KB
最終ジャッジ日時 2024-04-16 09:43:20
合計ジャッジ時間 6,549 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
51,712 KB
testcase_01 AC 38 ms
52,096 KB
testcase_02 AC 36 ms
52,096 KB
testcase_03 AC 222 ms
95,744 KB
testcase_04 AC 212 ms
96,756 KB
testcase_05 AC 221 ms
96,540 KB
testcase_06 AC 212 ms
96,884 KB
testcase_07 AC 214 ms
96,552 KB
testcase_08 AC 153 ms
89,516 KB
testcase_09 AC 93 ms
81,688 KB
testcase_10 AC 98 ms
81,876 KB
testcase_11 AC 88 ms
79,324 KB
testcase_12 AC 128 ms
86,692 KB
testcase_13 AC 145 ms
89,132 KB
testcase_14 AC 149 ms
89,424 KB
testcase_15 AC 123 ms
85,840 KB
testcase_16 AC 73 ms
74,232 KB
testcase_17 AC 69 ms
74,624 KB
testcase_18 AC 192 ms
94,464 KB
testcase_19 AC 79 ms
78,716 KB
testcase_20 AC 66 ms
71,808 KB
testcase_21 AC 114 ms
84,388 KB
testcase_22 AC 103 ms
83,096 KB
testcase_23 AC 64 ms
71,424 KB
testcase_24 AC 63 ms
71,168 KB
testcase_25 AC 70 ms
72,704 KB
testcase_26 AC 67 ms
70,784 KB
testcase_27 AC 46 ms
60,032 KB
testcase_28 AC 56 ms
66,432 KB
testcase_29 AC 67 ms
73,984 KB
testcase_30 AC 67 ms
73,472 KB
testcase_31 AC 60 ms
69,376 KB
testcase_32 AC 66 ms
72,584 KB
testcase_33 AC 78 ms
80,144 KB
testcase_34 AC 179 ms
93,440 KB
testcase_35 AC 96 ms
83,472 KB
testcase_36 AC 66 ms
78,312 KB
testcase_37 AC 154 ms
103,144 KB
testcase_38 AC 149 ms
100,496 KB
testcase_39 AC 35 ms
52,756 KB
testcase_40 AC 35 ms
52,712 KB
testcase_41 AC 34 ms
52,788 KB
testcase_42 AC 36 ms
52,952 KB
testcase_43 AC 37 ms
53,364 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
N = int(input())
e = [[] for _ in range(N + 1)]
for _ in range(N - 1):
  u, v = map(int, input().split())
  e[u].append(v)
  e[v].append(u)

s = [1]
vis = [0] * (N + 1)
parent = [0] * (N + 1)
vis[1] = 1
order = []
while len(s):
  x = s.pop()
  order.append(x)
  for y in e[x]:
    if vis[y]: continue
    parent[y] = x
    vis[y] = 1
    s.append(y)

size = [1] * (N + 1)
for y in order[: : -1]:
  x = parent[y]
  size[x] += size[y]

res = N ** 2
for x in range(1, N + 1):
  for y in e[x]:
    if y == parent[x]:
      res += (N - size[x]) * size[x]
      continue
    res += size[y] * (N - size[y])

print(res)
0