結果

問題 No.1098 LCAs
ユーザー ああいいああいい
提出日時 2021-12-25 10:55:04
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,213 bytes
コンパイル時間 191 ms
コンパイル使用メモリ 81,836 KB
実行使用メモリ 109,532 KB
最終ジャッジ日時 2023-10-21 11:31:29
合計ジャッジ時間 8,629 ms
ジャッジサーバーID
(参考情報)
judge14 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
57,868 KB
testcase_01 AC 44 ms
53,504 KB
testcase_02 AC 42 ms
53,504 KB
testcase_03 AC 40 ms
53,504 KB
testcase_04 AC 40 ms
53,504 KB
testcase_05 AC 39 ms
53,504 KB
testcase_06 AC 39 ms
53,504 KB
testcase_07 AC 40 ms
53,504 KB
testcase_08 AC 40 ms
53,504 KB
testcase_09 AC 39 ms
53,504 KB
testcase_10 AC 39 ms
53,504 KB
testcase_11 AC 39 ms
53,504 KB
testcase_12 AC 40 ms
53,504 KB
testcase_13 AC 71 ms
70,660 KB
testcase_14 AC 71 ms
70,636 KB
testcase_15 AC 75 ms
72,748 KB
testcase_16 AC 72 ms
70,680 KB
testcase_17 AC 73 ms
70,608 KB
testcase_18 AC 602 ms
102,184 KB
testcase_19 AC 589 ms
102,476 KB
testcase_20 AC 607 ms
102,472 KB
testcase_21 AC 601 ms
102,060 KB
testcase_22 AC 621 ms
102,172 KB
testcase_23 TLE -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

N = int(input())
G = [[] for _ in range(N+1)]
for _ in range(N-1):
    u,v = map(int,input().split())
    G[u].append(v)
    G[v].append(u)

memo = [0] * (N+1)
import sys
sys.setrecursionlimit(10 ** 6)
def calc(now = 1,parent = 0):
    if now != 1 and len(G[now]) == 1:
        memo[now] = 1
        return 1
    ans = 1
    for v in G[now]:
        if v != parent:
            ans += calc(v,now)
    memo[now] = ans
    return ans
calc()
for k in range(1,N+1):
    ans = memo[k] * 2 - 1
    if k == 1:
        if len(G[k]) == 1:
            print(ans)
            continue
        else:
            for u in range(len(G[k])-1):
                for w in range(u+1,len(G[k])):
                    v = G[k][u]
                    w = G[k][w]
                    ans += memo[v] * memo[w] * 2
            print(ans)
    else:
        if len(G[k]) <= 2:
            print(ans)
            continue
        else:
            for u in range(len(G[k]) - 1):
                for w in range(u+1,len(G[k])):
                    v = G[k][u]
                    w = G[k][w]
                    if memo[v] < memo[k] and memo[w] < memo[k] and v != w:
                        ans += memo[v] * memo[w] * 2
            print(ans)

0