結果

問題 No.763 Noelちゃんと木遊び
ユーザー neterukunneterukun
提出日時 2019-05-06 00:36:26
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 497 ms / 2,000 ms
コード長 1,160 bytes
コンパイル時間 77 ms
コンパイル使用メモリ 11,004 KB
実行使用メモリ 46,852 KB
最終ジャッジ日時 2023-09-08 14:17:58
合計ジャッジ時間 8,576 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 454 ms
46,852 KB
testcase_01 AC 164 ms
14,432 KB
testcase_02 AC 396 ms
22,984 KB
testcase_03 AC 263 ms
18,292 KB
testcase_04 AC 186 ms
15,688 KB
testcase_05 AC 249 ms
17,900 KB
testcase_06 AC 485 ms
26,060 KB
testcase_07 AC 462 ms
25,468 KB
testcase_08 AC 271 ms
18,720 KB
testcase_09 AC 179 ms
15,252 KB
testcase_10 AC 77 ms
11,004 KB
testcase_11 AC 497 ms
26,608 KB
testcase_12 AC 427 ms
24,468 KB
testcase_13 AC 416 ms
24,132 KB
testcase_14 AC 364 ms
22,416 KB
testcase_15 AC 252 ms
18,128 KB
testcase_16 AC 54 ms
9,996 KB
testcase_17 AC 258 ms
18,128 KB
testcase_18 AC 488 ms
26,108 KB
testcase_19 AC 433 ms
24,724 KB
testcase_20 AC 444 ms
24,624 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(0)
print(dp[0])
#print(dp_bool)
0