結果

問題 No.1098 LCAs
ユーザー stngstng
提出日時 2022-08-29 20:30:07
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 628 bytes
コンパイル時間 1,154 ms
コンパイル使用メモリ 81,800 KB
実行使用メモリ 485,048 KB
最終ジャッジ日時 2024-04-24 10:28:21
合計ジャッジ時間 13,020 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
53,240 KB
testcase_01 AC 35 ms
53,216 KB
testcase_02 AC 34 ms
53,356 KB
testcase_03 AC 37 ms
53,280 KB
testcase_04 AC 36 ms
53,688 KB
testcase_05 AC 36 ms
52,120 KB
testcase_06 AC 35 ms
52,204 KB
testcase_07 AC 35 ms
52,704 KB
testcase_08 AC 35 ms
53,232 KB
testcase_09 AC 35 ms
52,224 KB
testcase_10 AC 35 ms
52,648 KB
testcase_11 AC 34 ms
52,652 KB
testcase_12 AC 34 ms
53,676 KB
testcase_13 AC 61 ms
69,984 KB
testcase_14 AC 63 ms
69,360 KB
testcase_15 AC 61 ms
70,440 KB
testcase_16 AC 55 ms
67,664 KB
testcase_17 AC 59 ms
69,996 KB
testcase_18 AC 463 ms
111,220 KB
testcase_19 AC 479 ms
111,352 KB
testcase_20 AC 457 ms
111,484 KB
testcase_21 AC 488 ms
111,348 KB
testcase_22 AC 464 ms
112,244 KB
testcase_23 AC 393 ms
114,708 KB
testcase_24 AC 372 ms
114,844 KB
testcase_25 AC 334 ms
113,876 KB
testcase_26 AC 339 ms
113,708 KB
testcase_27 AC 350 ms
114,068 KB
testcase_28 AC 1,335 ms
322,436 KB
testcase_29 AC 1,292 ms
321,328 KB
testcase_30 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10**7)
n = int(input())
vw = [[] for i in range(n)]

for i in range(n-1):
    v,w = map(int,input().split())
    v -= 1
    w -= 1
    vw[v].append(w)
    vw[w].append(v)

dp = [0]*n
chk = [0]*n
ans = [0]*n

def dfs(v,pre):
    if chk[v] == 1:
        return dp[v]
    tmp = 1
    rui2 = 0
    for i in range(len(vw[v])):
        if vw[v][i] == pre:
            continue
        t = dfs(vw[v][i],v)
        dp[v] += t
        tmp += t
        rui2 += t**2
        chk[v] = 1
    dp[v] += 1
    ans[v] = tmp**2-rui2
    return dp[v]

dfs(0,-1)
#print(dp)
print(*ans,sep="\n")
#for i in range(n):
0