結果

問題 No.1098 LCAs
ユーザー stngstng
提出日時 2022-08-29 20:30:42
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,668 ms / 2,000 ms
コード長 628 bytes
コンパイル時間 121 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 87,808 KB
最終ジャッジ日時 2024-04-30 01:54:16
合計ジャッジ時間 22,495 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
10,624 KB
testcase_01 AC 32 ms
10,752 KB
testcase_02 AC 30 ms
10,752 KB
testcase_03 AC 33 ms
10,752 KB
testcase_04 AC 31 ms
10,624 KB
testcase_05 AC 30 ms
10,752 KB
testcase_06 AC 31 ms
10,752 KB
testcase_07 AC 31 ms
10,624 KB
testcase_08 AC 31 ms
10,624 KB
testcase_09 AC 30 ms
10,624 KB
testcase_10 AC 31 ms
10,624 KB
testcase_11 AC 30 ms
10,752 KB
testcase_12 AC 30 ms
10,752 KB
testcase_13 AC 35 ms
11,008 KB
testcase_14 AC 36 ms
11,008 KB
testcase_15 AC 36 ms
11,008 KB
testcase_16 AC 37 ms
11,008 KB
testcase_17 AC 37 ms
11,008 KB
testcase_18 AC 1,442 ms
53,888 KB
testcase_19 AC 1,486 ms
53,888 KB
testcase_20 AC 1,491 ms
53,888 KB
testcase_21 AC 1,448 ms
53,888 KB
testcase_22 AC 1,412 ms
53,888 KB
testcase_23 AC 1,280 ms
49,024 KB
testcase_24 AC 1,297 ms
49,024 KB
testcase_25 AC 1,246 ms
48,512 KB
testcase_26 AC 1,322 ms
54,784 KB
testcase_27 AC 1,316 ms
54,912 KB
testcase_28 AC 1,665 ms
84,608 KB
testcase_29 AC 1,668 ms
87,808 KB
testcase_30 AC 1,661 ms
86,080 KB
権限があれば一括ダウンロードができます

ソースコード

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