結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー titia
提出日時 2023-02-05 00:13:40
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 891 ms / 2,000 ms
コード長 817 bytes
コンパイル時間 89 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 70,356 KB
最終ジャッジ日時 2024-07-03 19:33:42
合計ジャッジ時間 24,900 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

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

ROOT=0

QUE=[ROOT] 
Parent=[-1]*(N+1)
Parent[ROOT]=N # ROOTの親を定めておく.
Child=[[] for i in range(N+1)]
TOP_SORT=[] # トポロジカルソート

while QUE: # トポロジカルソートと同時に親を見つける
    x=QUE.pop()
    TOP_SORT.append(x)
    for to in E[x]:
        if Parent[to]==-1:
            Parent[to]=x
            Child[x].append(to)
            QUE.append(to)

A=list(map(int,input().split()))
ANS=0
for x in TOP_SORT[::-1]:
    if x==ROOT:
        break

    if A[x]==0:
        A[x]^=1
        A[Parent[x]]^=1
        ANS+=1

if A[ROOT]==1:
    print(ANS)
else:
    print(-1)
        
0