結果

問題 No.763 Noelちゃんと木遊び
ユーザー tktk_snsntktk_snsn
提出日時 2021-02-08 20:59:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 232 ms / 2,000 ms
コード長 804 bytes
コンパイル時間 471 ms
コンパイル使用メモリ 87,144 KB
実行使用メモリ 96,784 KB
最終ジャッジ日時 2023-09-20 02:37:33
合計ジャッジ時間 7,112 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 137 ms
96,748 KB
testcase_01 AC 135 ms
83,156 KB
testcase_02 AC 198 ms
92,512 KB
testcase_03 AC 157 ms
86,276 KB
testcase_04 AC 145 ms
83,736 KB
testcase_05 AC 154 ms
85,796 KB
testcase_06 AC 232 ms
96,408 KB
testcase_07 AC 223 ms
95,516 KB
testcase_08 AC 167 ms
86,496 KB
testcase_09 AC 137 ms
83,772 KB
testcase_10 AC 120 ms
79,680 KB
testcase_11 AC 225 ms
96,784 KB
testcase_12 AC 210 ms
94,536 KB
testcase_13 AC 200 ms
94,196 KB
testcase_14 AC 193 ms
92,280 KB
testcase_15 AC 153 ms
85,908 KB
testcase_16 AC 113 ms
78,824 KB
testcase_17 AC 163 ms
86,164 KB
testcase_18 AC 231 ms
96,560 KB
testcase_19 AC 214 ms
94,488 KB
testcase_20 AC 207 ms
94,748 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)

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

topo = []
par = [-1] * N
q = [0]
while q:
    s = q.pop()
    topo.append(s)
    for t in G[s]:
        if t == par[s]:
            continue
        G[t].remove(s)
        par[t] = s
        q.append(t)

X = [0] * N  # 根付き木iの根が残ってるときの最大
Y = [0] * N  # 根を消した時の最大

for s in reversed(topo):
    if not G[s]:
        X[s] = 1
        continue

    sumY = 0
    sumXY = 0
    for t in G[s]:
        sumY += Y[t]
        sumXY += max(X[t], Y[t])
    X[s] = max(sumY + 1, sumXY)
    Y[s] = sumXY

print(max(X[0], Y[0]))
0