結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー ThetaTheta
提出日時 2023-02-06 15:16:14
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 848 ms / 2,000 ms
コード長 1,246 bytes
コンパイル時間 839 ms
コンパイル使用メモリ 10,840 KB
実行使用メモリ 73,392 KB
最終ジャッジ日時 2023-09-18 02:36:26
合計ジャッジ時間 24,533 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,752 KB
testcase_01 AC 16 ms
7,868 KB
testcase_02 AC 15 ms
7,964 KB
testcase_03 AC 15 ms
7,824 KB
testcase_04 AC 16 ms
7,804 KB
testcase_05 AC 16 ms
7,964 KB
testcase_06 AC 16 ms
7,924 KB
testcase_07 AC 16 ms
7,796 KB
testcase_08 AC 16 ms
7,748 KB
testcase_09 AC 15 ms
7,800 KB
testcase_10 AC 16 ms
7,828 KB
testcase_11 AC 792 ms
73,392 KB
testcase_12 AC 778 ms
73,304 KB
testcase_13 AC 638 ms
45,648 KB
testcase_14 AC 743 ms
67,136 KB
testcase_15 AC 809 ms
71,648 KB
testcase_16 AC 506 ms
42,088 KB
testcase_17 AC 500 ms
42,132 KB
testcase_18 AC 15 ms
7,952 KB
testcase_19 AC 776 ms
65,672 KB
testcase_20 AC 629 ms
44,140 KB
testcase_21 AC 822 ms
64,432 KB
testcase_22 AC 750 ms
65,984 KB
testcase_23 AC 794 ms
64,428 KB
testcase_24 AC 795 ms
64,892 KB
testcase_25 AC 825 ms
64,436 KB
testcase_26 AC 848 ms
64,416 KB
testcase_27 AC 772 ms
65,888 KB
testcase_28 AC 760 ms
66,044 KB
testcase_29 AC 767 ms
65,892 KB
testcase_30 AC 835 ms
64,272 KB
testcase_31 AC 773 ms
65,732 KB
testcase_32 AC 814 ms
65,044 KB
testcase_33 AC 803 ms
64,992 KB
testcase_34 AC 762 ms
64,280 KB
testcase_35 AC 829 ms
64,412 KB
testcase_36 AC 829 ms
64,436 KB
testcase_37 AC 767 ms
64,344 KB
testcase_38 AC 638 ms
44,308 KB
testcase_39 AC 635 ms
45,532 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    N = int(input())

    graphs = [[] for _ in range(N)]
    for _ in range(N-1):
        dep, dest = map(lambda n: int(n)-1, input().split())
        graphs[dep].append(dest)
        graphs[dest].append(dep)
    color = list(map(int, input().split()))
    if color.count(0) % 2:
        print(-1)
        return

    moved = set()
    searched = set()
    stack = []
    stack.append([0, None])
    moved.add(0)
    change_ctr = 0
    while stack:
        ctr_child = 0
        current_notice_node, notice_node_parent = stack[-1]
        for node_idx in graphs[current_notice_node]:
            if node_idx not in moved:
                stack.append((node_idx, current_notice_node))
                moved.add(node_idx)
                ctr_child += 1
        if ctr_child == 0:
            current_node_idx, parent_idx = stack.pop()
            if color[current_node_idx] == 0:
                if parent_idx is not None:
                    color[current_node_idx] = 1
                    color[parent_idx] ^= 1
                    change_ctr += 1
                else:
                    print(-1)
                    return
            searched.add(current_node_idx)

    print(change_ctr)


if __name__ == "__main__":
    main()
0