結果

問題 No.763 Noelちゃんと木遊び
ユーザー neterukunneterukun
提出日時 2019-05-06 00:33:11
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
RE  
実行時間 -
コード長 1,159 bytes
コンパイル時間 89 ms
コンパイル使用メモリ 10,708 KB
実行使用メモリ 55,568 KB
最終ジャッジ日時 2023-09-08 14:13:40
合計ジャッジ時間 9,689 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 AC 175 ms
14,480 KB
testcase_02 AC 428 ms
22,960 KB
testcase_03 AC 291 ms
18,508 KB
testcase_04 AC 205 ms
15,720 KB
testcase_05 AC 279 ms
17,792 KB
testcase_06 AC 544 ms
26,116 KB
testcase_07 AC 514 ms
25,288 KB
testcase_08 AC 305 ms
18,620 KB
testcase_09 AC 197 ms
15,272 KB
testcase_10 AC 81 ms
10,920 KB
testcase_11 AC 555 ms
26,528 KB
testcase_12 AC 481 ms
24,580 KB
testcase_13 AC 477 ms
24,016 KB
testcase_14 AC 415 ms
22,392 KB
testcase_15 AC 282 ms
18,268 KB
testcase_16 AC 57 ms
9,888 KB
testcase_17 AC 291 ms
18,124 KB
testcase_18 AC 547 ms
26,104 KB
testcase_19 AC 486 ms
24,664 KB
testcase_20 AC 477 ms
24,396 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys


sys.setrecursionlimit(10**5+1)

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