結果

問題 No.1103 Directed Length Sum
ユーザー toyuzukotoyuzuko
提出日時 2020-07-07 15:36:47
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 800 bytes
コンパイル時間 638 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 396,216 KB
最終ジャッジ日時 2024-04-08 15:55:48
合計ジャッジ時間 26,964 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,460 KB
testcase_01 AC 35 ms
53,460 KB
testcase_02 WA -
testcase_03 AC 1,455 ms
396,216 KB
testcase_04 AC 1,511 ms
202,492 KB
testcase_05 TLE -
testcase_06 AC 951 ms
157,188 KB
testcase_07 AC 228 ms
94,372 KB
testcase_08 AC 327 ms
106,124 KB
testcase_09 AC 173 ms
87,588 KB
testcase_10 AC 466 ms
116,752 KB
testcase_11 AC 1,676 ms
214,000 KB
testcase_12 AC 926 ms
158,116 KB
testcase_13 AC 466 ms
116,540 KB
testcase_14 AC 150 ms
82,488 KB
testcase_15 AC 723 ms
138,844 KB
testcase_16 AC 1,941 ms
233,152 KB
testcase_17 AC 2,114 ms
243,780 KB
testcase_18 AC 454 ms
118,412 KB
testcase_19 AC 1,755 ms
217,852 KB
testcase_20 AC 198 ms
91,392 KB
testcase_21 AC 312 ms
101,420 KB
testcase_22 AC 1,379 ms
190,788 KB
testcase_23 AC 719 ms
141,844 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 1000000007

N = int(input())
edge = [tuple(map(int, input().split())) for _ in range(N - 1)]
tree = [[] for _ in range(N)]
par = [None for _ in range(N)]
dep = [None for _ in range(N)]
cum = [None for _ in range(N)]

for e in edge:
    tree[e[0] - 1].append(e[1] - 1)
    par[e[1] - 1] = e[0] - 1

for i in range(N):
    if par[i] is None:
        root = i

stack = [root]
dep[root] = 0

while stack:
    node = stack.pop()
    for adj in tree[node]:
        if dep[adj] is not None:
            continue
        dep[adj] = dep[node] + 1
        stack.append(adj)

stack = [root]
cum[root] = 0

while stack:
    node = stack.pop()
    for adj in tree[node]:
        if cum[adj] is not None:
            continue
        cum[adj] = cum[node] + dep[adj]
        stack.append(adj)

print(sum(cum))
0