結果
| 問題 | 
                            No.1098 LCAs
                             | 
                    
| コンテスト | |
| ユーザー | 
                             norioc
                         | 
                    
| 提出日時 | 2025-04-01 20:46:46 | 
| 言語 | PyPy3  (7.3.15)  | 
                    
| 結果 | 
                             
                                MLE
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 554 bytes | 
| コンパイル時間 | 451 ms | 
| コンパイル使用メモリ | 82,104 KB | 
| 実行使用メモリ | 522,688 KB | 
| 最終ジャッジ日時 | 2025-04-01 20:46:59 | 
| 合計ジャッジ時間 | 12,647 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 27 MLE * 1 | 
ソースコード
from collections import defaultdict
import sys
sys.setrecursionlimit(10 ** 6)
N = int(input())
adj = defaultdict(list)
for _ in range(N-1):
    u, v = map(lambda x: int(x)-1, input().split())
    adj[u].append(v)
    adj[v].append(u)
def dfs(v, par):
    ans[v] += 1
    cs = []
    nsub = 0
    for to in adj[v]:
        if to == par: continue
        r = dfs(to, v)
        # v と子
        ans[v] += r * 2
        # 子同士
        ans[v] += nsub * r * 2
        nsub += r
    return nsub + 1
ans = [0] * N
dfs(0, -1)
print(*ans, sep='\n')
            
            
            
        
            
norioc