結果

問題 No.763 Noelちゃんと木遊び
ユーザー neterukunneterukun
提出日時 2021-02-15 01:30:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 317 ms / 2,000 ms
コード長 1,009 bytes
コンパイル時間 815 ms
コンパイル使用メモリ 86,960 KB
実行使用メモリ 108,080 KB
最終ジャッジ日時 2023-09-29 19:15:11
合計ジャッジ時間 8,240 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 226 ms
108,080 KB
testcase_01 AC 178 ms
87,496 KB
testcase_02 AC 265 ms
101,288 KB
testcase_03 AC 210 ms
93,320 KB
testcase_04 AC 183 ms
86,624 KB
testcase_05 AC 214 ms
93,424 KB
testcase_06 AC 310 ms
106,876 KB
testcase_07 AC 295 ms
105,668 KB
testcase_08 AC 213 ms
91,920 KB
testcase_09 AC 180 ms
87,376 KB
testcase_10 AC 161 ms
81,400 KB
testcase_11 AC 317 ms
104,268 KB
testcase_12 AC 294 ms
104,248 KB
testcase_13 AC 279 ms
103,824 KB
testcase_14 AC 240 ms
95,988 KB
testcase_15 AC 219 ms
93,556 KB
testcase_16 AC 132 ms
78,376 KB
testcase_17 AC 212 ms
93,448 KB
testcase_18 AC 302 ms
107,176 KB
testcase_19 AC 294 ms
104,500 KB
testcase_20 AC 284 ms
104,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def topological_sorted(tree, root=None):
    n = len(tree)
    par = [-1] * n
    tp_order = []
    for v in range(n):
        if par[v] != -1 or (root is not None and v != root):
            continue
        stack = [v]
        while stack:
            v = stack.pop()
            tp_order.append(v)
            for nxt_v in tree[v]:
                if nxt_v == par[v]:
                    continue
                par[nxt_v] = v
                stack.append(nxt_v)
    return tp_order, par


n = int(input())
edges = [list(map(int, input().split())) for i in range(n - 1)]


tree = [[] for i in range(n)]
for u, v in edges:
    u -= 1
    v -= 1
    tree[u].append(v)
    tree[v].append(u)

tp_order, par = topological_sorted(tree, 0)
dp0 = [0] * n
dp1 = [0] * n

for v in tp_order[::-1]:
    for nxt_v in tree[v]:
        if nxt_v == par[v]:
            continue
        dp0[v] += max(dp0[nxt_v], dp1[nxt_v])
        dp1[v] += max(dp0[nxt_v], dp1[nxt_v] - 1)
    dp1[v] += 1

print(max(max(dp0), max(dp1)))
0