結果

問題 No.2205 Lights Out on Christmas Tree
コンテスト
ユーザー norioc
提出日時 2026-11-26 04:19:05
言語 PyPy3
(8.0.1 + ACL)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 567 ms / 2,000 ms
+ 81µs
コード長 602 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 79 ms
コンパイル使用メモリ 64,384 KB
実行使用メモリ 295,184 KB
最終ジャッジ日時 2026-11-26 04:19:18
合計ジャッジ時間 12,871 ms
ジャッジサーバーID
(参考情報)
judge5_0 / judge4_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import sys
sys.setrecursionlimit(10**6)

INF = 1 << 62
N = int(input())

adj = [[] for _ in range(N)]
for _ in range(N-1):
    U, V = map(lambda x: int(x)-1, input().split())
    adj[U].append(V)
    adj[V].append(U)

C = list(map(int, input().split()))


def tree_dp(v, par):
    parity = 1 if C[v] == 0 else 0  # 部分木に含まれる白の数の偶奇

    res = 0
    for to in adj[v]:
        if to == par: continue

        p, r = tree_dp(to, v)
        parity ^= p
        res += r + p

    return parity, res


parity, ans = tree_dp(0, -1)
if parity == 0:
    print(ans)
else:
    print(-1)
0