結果

問題 No.1098 LCAs
ユーザー kohei2019kohei2019
提出日時 2022-07-25 23:50:01
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,973 ms / 2,000 ms
コード長 616 bytes
コンパイル時間 80 ms
コンパイル使用メモリ 10,940 KB
実行使用メモリ 96,656 KB
最終ジャッジ日時 2023-09-22 10:46:47
合計ジャッジ時間 22,376 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
7,920 KB
testcase_01 AC 15 ms
7,928 KB
testcase_02 AC 18 ms
7,860 KB
testcase_03 AC 18 ms
7,864 KB
testcase_04 AC 17 ms
7,812 KB
testcase_05 AC 16 ms
7,816 KB
testcase_06 AC 16 ms
7,832 KB
testcase_07 AC 15 ms
7,932 KB
testcase_08 AC 16 ms
7,752 KB
testcase_09 AC 16 ms
7,928 KB
testcase_10 AC 15 ms
7,928 KB
testcase_11 AC 16 ms
7,864 KB
testcase_12 AC 16 ms
7,804 KB
testcase_13 AC 21 ms
8,564 KB
testcase_14 AC 20 ms
8,568 KB
testcase_15 AC 21 ms
8,624 KB
testcase_16 AC 21 ms
8,672 KB
testcase_17 AC 21 ms
8,672 KB
testcase_18 AC 1,490 ms
66,892 KB
testcase_19 AC 1,467 ms
66,836 KB
testcase_20 AC 1,512 ms
67,084 KB
testcase_21 AC 1,508 ms
66,832 KB
testcase_22 AC 1,493 ms
67,096 KB
testcase_23 AC 1,236 ms
58,748 KB
testcase_24 AC 1,234 ms
58,712 KB
testcase_25 AC 1,188 ms
56,924 KB
testcase_26 AC 1,238 ms
63,200 KB
testcase_27 AC 1,224 ms
63,308 KB
testcase_28 AC 1,945 ms
93,560 KB
testcase_29 AC 1,909 ms
96,656 KB
testcase_30 AC 1,973 ms
96,244 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10**7)
N = int(input())
lsg = [[] for i in range(N)]
for i in range(N-1):
    v,w = map(int,input().split())
    v -= 1
    w -= 1
    lsg[v].append(w)
    lsg[w].append(v)

dp = [0]*(N)
dp2 = [[1] for i in range(N)]

def dfs(k,p):
    cnt = 1
    for j in lsg[k]:
        if j == p:
            continue
        c = dfs(j,k)
        dp2[k].append(c)
        cnt += c
    dp[k] = cnt
    return cnt

dfs(0,-1)

for i in range(N):
    l = dp2[i]
    if len(l) == 1:
        print(1)
        continue
    sm = sum(l)
    ans = 1
    for j in l:
        ans += j*(sm-j)
    print(ans)

0