結果

問題 No.763 Noelちゃんと木遊び
ユーザー titia
提出日時 2025-02-11 08:56:24
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 796 ms / 2,000 ms
コード長 929 bytes
コンパイル時間 169 ms
コンパイル使用メモリ 12,160 KB
実行使用メモリ 47,104 KB
最終ジャッジ日時 2025-03-22 10:44:24
合計ジャッジ時間 13,586 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

n=int(input())

E=[[] for i in range(n)]

for i in range(n-1):
    x,y=map(int,input().split())
    x-=1
    y-=1
    E[x].append(y)
    E[y].append(x)

# 木のHL分解+LCA

ROOT=0

QUE=[ROOT] 
Parent=[-1]*n
Parent[ROOT]=n # ROOTの親を定めておく.
Child=[[] for i in range(n)]
TOP_SORT=[] # トポロジカルソート

while QUE: # トポロジカルソートと同時に親を見つける
    x=QUE.pop()
    TOP_SORT.append(x)
    for to in E[x]:
        if Parent[to]==-1:
            Parent[to]=x
            Child[x].append(to)
            QUE.append(to)

DP=[0]*n
DP2=[0]*n

for x in TOP_SORT[::-1]:
    if Child[x]==[]:
        DP[x]=1
        DP2[x]=0
    else:
        score=0
        for c in Child[x]:
            score+=max(DP2[c],DP[c])
        DP2[x]=score

        score=0
        for c in Child[x]:
            score+=max(DP2[c],DP[c]-1)
        DP[x]=score+1

print(max(DP[0],DP2[0]))
            
    
0