結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 AC 183 ms
14,432 KB
testcase_02 AC 422 ms
22,892 KB
testcase_03 AC 288 ms
18,420 KB
testcase_04 AC 205 ms
15,496 KB
testcase_05 AC 285 ms
17,936 KB
testcase_06 AC 537 ms
26,208 KB
testcase_07 AC 511 ms
25,368 KB
testcase_08 AC 306 ms
18,744 KB
testcase_09 AC 203 ms
15,420 KB
testcase_10 AC 84 ms
11,028 KB
testcase_11 AC 545 ms
26,584 KB
testcase_12 AC 472 ms
24,368 KB
testcase_13 AC 459 ms
24,084 KB
testcase_14 AC 408 ms
22,368 KB
testcase_15 AC 279 ms
18,136 KB
testcase_16 AC 56 ms
10,052 KB
testcase_17 AC 282 ms
18,144 KB
testcase_18 AC 541 ms
26,100 KB
testcase_19 AC 481 ms
24,588 KB
testcase_20 AC 479 ms
24,508 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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 = [0]*n
dp_bool = [False]*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