結果

問題 No.763 Noelちゃんと木遊び
ユーザー lloyzlloyz
提出日時 2023-10-26 19:06:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 371 ms / 2,000 ms
コード長 831 bytes
コンパイル時間 307 ms
コンパイル使用メモリ 81,840 KB
実行使用メモリ 112,040 KB
最終ジャッジ日時 2023-10-26 19:06:14
合計ジャッジ時間 7,919 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 225 ms
112,040 KB
testcase_01 AC 180 ms
85,684 KB
testcase_02 AC 295 ms
97,348 KB
testcase_03 AC 257 ms
91,604 KB
testcase_04 AC 192 ms
86,888 KB
testcase_05 AC 225 ms
91,324 KB
testcase_06 AC 339 ms
101,528 KB
testcase_07 AC 330 ms
99,792 KB
testcase_08 AC 267 ms
91,912 KB
testcase_09 AC 188 ms
86,640 KB
testcase_10 AC 137 ms
81,652 KB
testcase_11 AC 353 ms
101,776 KB
testcase_12 AC 321 ms
99,044 KB
testcase_13 AC 339 ms
99,776 KB
testcase_14 AC 289 ms
96,664 KB
testcase_15 AC 231 ms
91,384 KB
testcase_16 AC 127 ms
79,540 KB
testcase_17 AC 231 ms
91,340 KB
testcase_18 AC 371 ms
101,704 KB
testcase_19 AC 316 ms
99,656 KB
testcase_20 AC 314 ms
99,200 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict, deque

n = int(input())
G = defaultdict(list)
C = [0 for _ in range(n)]
for _ in range(n - 1):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    G[u].append(v)
    G[v].append(u)

for u in range(n):
    if C[u] >= 3:
        for v in G[u]:
            G[v].remove(u)
        G[u] = set()
        C[u] = 0

DP = [[0, 0] for _ in range(n)]
Stack = [(0, -1, 0)]
while Stack:
    cp, pp, m = Stack.pop()
    if m == 0:
        Stack.append((cp, pp, -1))
        for np in G[cp]:
            if np == pp:
                continue
            Stack.append((np, cp, 0))
    else:
        for np in G[cp]:
            if np == pp:
                continue
            DP[cp][0] += max(DP[np][0] - 1, DP[np][1])
            DP[cp][1] += max(DP[np])
        DP[cp][0] += 1
print(max(DP[0]))
0