結果

問題 No.2205 Lights Out on Christmas Tree
コンテスト
ユーザー 回転
提出日時 2025-10-29 15:00:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 604 ms / 2,000 ms
コード長 650 bytes
コンパイル時間 329 ms
コンパイル使用メモリ 82,336 KB
実行使用メモリ 283,276 KB
最終ジャッジ日時 2025-10-29 15:00:54
合計ジャッジ時間 12,476 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

import pypyjit
pypyjit.set_param("max_unroll_recursion=-1")
import sys
sys.setrecursionlimit(10**7)

N = int(input())
edge = [[] for _ in range(N)]
for _ in range(N-1):
    a,b = list(map(int,input().split()))
    a -= 1;b -= 1
    edge[a].append(b)
    edge[b].append(a)

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

if(C.count(0)%2 == 1):
    print(-1)
    exit()

# 部分木の根元に白を持ってくる (必要手数, 根は白?)
def f(n,pre):
    ret = 0
    is_white = C[n] == 0
    for i in edge[n]:
        if(i == pre):continue
        count,w = f(i,n)
        ret += count + w
        is_white ^= w
    return ret, is_white

print(f(0,-1)[0])
0