結果

問題 No.1098 LCAs
ユーザー neterukunneterukun
提出日時 2020-06-26 22:47:14
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 628 ms / 2,000 ms
コード長 847 bytes
コンパイル時間 322 ms
コンパイル使用メモリ 86,896 KB
実行使用メモリ 168,268 KB
最終ジャッジ日時 2023-09-18 06:37:20
合計ジャッジ時間 11,318 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,060 KB
testcase_01 AC 75 ms
71,188 KB
testcase_02 AC 75 ms
71,400 KB
testcase_03 AC 76 ms
71,232 KB
testcase_04 AC 75 ms
71,244 KB
testcase_05 AC 75 ms
71,324 KB
testcase_06 AC 74 ms
71,276 KB
testcase_07 AC 76 ms
71,228 KB
testcase_08 AC 74 ms
71,372 KB
testcase_09 AC 76 ms
71,304 KB
testcase_10 AC 75 ms
71,352 KB
testcase_11 AC 76 ms
71,244 KB
testcase_12 AC 75 ms
71,348 KB
testcase_13 AC 108 ms
76,912 KB
testcase_14 AC 107 ms
76,724 KB
testcase_15 AC 109 ms
76,900 KB
testcase_16 AC 108 ms
76,624 KB
testcase_17 AC 106 ms
76,896 KB
testcase_18 AC 594 ms
132,864 KB
testcase_19 AC 594 ms
134,688 KB
testcase_20 AC 607 ms
134,768 KB
testcase_21 AC 594 ms
134,740 KB
testcase_22 AC 603 ms
134,480 KB
testcase_23 AC 513 ms
163,760 KB
testcase_24 AC 538 ms
161,456 KB
testcase_25 AC 473 ms
165,148 KB
testcase_26 AC 496 ms
168,268 KB
testcase_27 AC 513 ms
167,696 KB
testcase_28 AC 626 ms
138,696 KB
testcase_29 AC 628 ms
138,440 KB
testcase_30 AC 626 ms
138,688 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(input())
info = [list(map(int, input().split())) for i in range(n - 1)]


root = 0
graph = [[] for i in range(n)]
for a, b in info:
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)


stack = [root]
tp_sort = [root]
visited = [False] * n
visited[root] = True
while stack:
    v = stack.pop()
    for nxt_v in graph[v]:
        if visited[nxt_v]:
            continue
        visited[nxt_v] = True
        stack.append(nxt_v)
        tp_sort.append(nxt_v)

dp = [-1] * n
dp_cnt = [0] * n
while tp_sort:
    v = tp_sort.pop()
    dp_sum = 0
    dp[v] = 0
    for chi_v in graph[v]:
        if dp[chi_v] == -1:
            continue
        dp[v] += dp_sum * dp_cnt[chi_v] * 2
        dp_cnt[v] += dp_cnt[chi_v] 
        dp_sum += dp_cnt[chi_v]
    dp[v] += dp_sum * 2 + 1
    dp_cnt[v] += 1

for val in dp:
    print(val)   
0