結果

問題 No.1103 Directed Length Sum
ユーザー convexineqconvexineq
提出日時 2021-05-10 05:35:42
言語 PyPy3
(7.3.13)
結果
WA  
実行時間 -
コード長 633 bytes
コンパイル時間 193 ms
コンパイル使用メモリ 81,740 KB
実行使用メモリ 267,748 KB
最終ジャッジ日時 2023-10-19 13:20:38
合計ジャッジ時間 13,602 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,444 KB
testcase_01 AC 38 ms
53,444 KB
testcase_02 WA -
testcase_03 AC 475 ms
267,748 KB
testcase_04 AC 805 ms
164,916 KB
testcase_05 AC 1,420 ms
239,040 KB
testcase_06 AC 534 ms
143,480 KB
testcase_07 AC 128 ms
90,996 KB
testcase_08 AC 191 ms
100,228 KB
testcase_09 AC 102 ms
84,268 KB
testcase_10 AC 265 ms
108,808 KB
testcase_11 AC 878 ms
182,440 KB
testcase_12 AC 507 ms
143,864 KB
testcase_13 AC 267 ms
110,748 KB
testcase_14 AC 92 ms
82,248 KB
testcase_15 AC 424 ms
122,052 KB
testcase_16 AC 979 ms
181,864 KB
testcase_17 AC 1,020 ms
211,988 KB
testcase_18 AC 251 ms
108,704 KB
testcase_19 AC 916 ms
195,648 KB
testcase_20 AC 109 ms
87,476 KB
testcase_21 AC 178 ms
98,032 KB
testcase_22 AC 733 ms
152,580 KB
testcase_23 AC 450 ms
133,972 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
n = int(input())
g = [[] for _ in range(n)]
is_child = [0]*n
for i in range(n-1):
    a,b = map(int,sys.stdin.readline().split())
    g[a-1].append(b-1)
    is_child[b-1] = 1
root = is_child.index(0)

order = []
st = [root]
parent = [-1]*n
depth = [1]*n
while st:
    v = st.pop()
    order.append(v)
    for c in g[v]:
        if c != parent[v]:
            st.append(c)
            parent[c] = v
            depth[c] = depth[v] + 1

size = [1]*n
for i in order[::-1]:
    if i==root: break
    size[parent[i]] += size[i]

ans = 0
for i in range(n):
    if i==root: continue
    ans += size[i]*depth[parent[i]]
print(ans)
0