結果
| 問題 | 
                            No.1098 LCAs
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2021-12-25 10:45:44 | 
| 言語 | Python3  (3.13.1 + numpy 2.2.1 + scipy 1.14.1)  | 
                    
| 結果 | 
                             
                                TLE
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 1,050 bytes | 
| コンパイル時間 | 190 ms | 
| コンパイル使用メモリ | 12,800 KB | 
| 実行使用メモリ | 48,032 KB | 
| 最終ジャッジ日時 | 2024-09-21 12:35:17 | 
| 合計ジャッジ時間 | 13,922 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge3 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 20 TLE * 1 -- * 7 | 
ソースコード
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 v in G[k]:
                for w in G[k]:
                    if v != w:
                        ans += memo[v] * memo[w]
            print(ans)
    else:
        if len(G[k]) <= 2:
            print(ans)
            continue
        else:
            for v in G[k]:
                for w in G[k]:
                    if memo[v] < memo[k] and memo[w] < memo[k] and v != w:
                        ans += memo[v] * memo[w]
            print(ans)