結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー flygonflygon
提出日時 2023-02-03 22:28:29
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 576 ms / 2,000 ms
コード長 657 bytes
コンパイル時間 223 ms
コンパイル使用メモリ 82,688 KB
実行使用メモリ 269,188 KB
最終ジャッジ日時 2024-07-02 20:32:30
合計ジャッジ時間 9,995 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(5*10**5)
input = sys.stdin.readline
from collections import defaultdict, deque, Counter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right
from math import gcd

n = int(input())
graph = [[] for i in range(n+1)]
for i in range(n-1):
  a,b = map(int,input().split())
  graph[a-1].append(b-1)
  graph[b-1].append(a-1)

c = list(map(int,input().split()))
cnt = 0
def dfs(crr,pre):
  global cnt
  for nxt in graph[crr]:
    if nxt == pre: continue
    dfs(nxt,crr)
  if crr != 0 and c[crr] == 0:
    cnt += 1
    c[crr] ^= 1
    c[pre] ^= 1

dfs(0, -1)
if sum(c) == n:
  print(cnt)
else:
  print(-1)
0