結果

問題 No.1098 LCAs
ユーザー wattaiheiwattaihei
提出日時 2020-06-26 23:04:46
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 542 ms / 2,000 ms
コード長 741 bytes
コンパイル時間 260 ms
コンパイル使用メモリ 86,744 KB
実行使用メモリ 130,128 KB
最終ジャッジ日時 2023-09-18 07:19:05
合計ジャッジ時間 9,560 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,240 KB
testcase_01 AC 75 ms
71,020 KB
testcase_02 AC 73 ms
71,200 KB
testcase_03 AC 72 ms
71,312 KB
testcase_04 AC 73 ms
71,032 KB
testcase_05 AC 74 ms
71,316 KB
testcase_06 AC 73 ms
70,876 KB
testcase_07 AC 74 ms
70,932 KB
testcase_08 AC 73 ms
71,196 KB
testcase_09 AC 73 ms
71,268 KB
testcase_10 AC 74 ms
71,040 KB
testcase_11 AC 73 ms
71,272 KB
testcase_12 AC 73 ms
71,220 KB
testcase_13 AC 94 ms
76,372 KB
testcase_14 AC 90 ms
76,280 KB
testcase_15 AC 93 ms
76,124 KB
testcase_16 AC 95 ms
76,484 KB
testcase_17 AC 93 ms
76,552 KB
testcase_18 AC 483 ms
111,904 KB
testcase_19 AC 470 ms
111,884 KB
testcase_20 AC 485 ms
111,928 KB
testcase_21 AC 471 ms
111,604 KB
testcase_22 AC 476 ms
112,012 KB
testcase_23 AC 369 ms
129,080 KB
testcase_24 AC 367 ms
128,952 KB
testcase_25 AC 326 ms
130,128 KB
testcase_26 AC 331 ms
129,936 KB
testcase_27 AC 324 ms
130,032 KB
testcase_28 AC 542 ms
115,980 KB
testcase_29 AC 533 ms
116,096 KB
testcase_30 AC 535 ms
117,212 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

N = int(input())
graph = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)

D = [-1]*N
D[0] = 0
Childs = [1]*N
ans = [-1]*N

stack = [0]
while stack:
    p = stack.pop()
    if p >= 0:
        stack.append(~p)
        for np in graph[p]:
            if D[np] == -1:
                D[np] = D[p] + 1
                stack.append(np)
    else:
        p = ~p
        t1 = 0
        t2 = 0
        for np in graph[p]:
            if D[np] > D[p]:
                t1 += Childs[np]
                t2 += Childs[np]**2
                Childs[p] += Childs[np]
        ans[p] = (t1**2 - t2) + 2*t1 + 1


print(*ans, sep="\n")
0