結果

問題 No.763 Noelちゃんと木遊び
ユーザー neterukunneterukun
提出日時 2019-05-06 00:55:25
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 536 ms / 2,000 ms
コード長 1,173 bytes
コンパイル時間 101 ms
コンパイル使用メモリ 10,992 KB
実行使用メモリ 38,616 KB
最終ジャッジ日時 2023-09-08 14:53:42
合計ジャッジ時間 9,291 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 452 ms
38,616 KB
testcase_01 AC 173 ms
14,548 KB
testcase_02 AC 414 ms
22,900 KB
testcase_03 AC 271 ms
18,516 KB
testcase_04 AC 194 ms
15,528 KB
testcase_05 AC 261 ms
17,940 KB
testcase_06 AC 514 ms
26,084 KB
testcase_07 AC 482 ms
25,300 KB
testcase_08 AC 288 ms
18,708 KB
testcase_09 AC 184 ms
15,328 KB
testcase_10 AC 79 ms
11,052 KB
testcase_11 AC 536 ms
26,684 KB
testcase_12 AC 473 ms
24,560 KB
testcase_13 AC 457 ms
24,172 KB
testcase_14 AC 392 ms
22,412 KB
testcase_15 AC 266 ms
18,312 KB
testcase_16 AC 55 ms
9,864 KB
testcase_17 AC 269 ms
18,252 KB
testcase_18 AC 534 ms
26,304 KB
testcase_19 AC 487 ms
24,624 KB
testcase_20 AC 472 ms
24,796 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys


sys.setrecursionlimit(1000000)

n = int(input())
tree = [[] for i in range(n)]
for i in range(n-1):
    tmp1, tmp2 = map(int, input().split())
    tree[tmp1-1].append(tmp2-1)
    tree[tmp2-1].append(tmp1-1)
visited = [False]*n

#dpはその時点における最大の木の個数を表す
#dp_boolはその頂点が消されるか(=False)残るか(=True)を示す
dp = [0]*n
dp_bool = [True]*n

def dfs(i):
    visited[i] = True
    #葉のとき
    if all([visited[j] for j in tree[i]]):
        dp[i] = 1
        dp_bool[i] = True
    #葉でないとき
    else:
        tmp = True
        cnt_True = 0
        cnt_False = 0
        for j in tree[i]:
            if not visited[j]:
                dfs(j)
                if dp_bool[j]:
                    cnt_True += dp[j] - 1
                    cnt_False += dp[j]
                else:
                    cnt_True += dp[j]
                    cnt_False += dp[j]
        cnt_True += 1
        if cnt_True > cnt_False:
            dp[i] = cnt_True
            dp_bool[i] = True
        else:
            dp[i] = cnt_False
            dp_bool[i] = False

dfs((n-1)//2)
print(dp[(n-1)//2])
#print(dp_bool)
0