結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー titiatitia
提出日時 2023-02-05 00:13:40
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 812 ms / 2,000 ms
コード長 817 bytes
コンパイル時間 321 ms
コンパイル使用メモリ 10,832 KB
実行使用メモリ 67,756 KB
最終ジャッジ日時 2023-09-16 20:17:34
合計ジャッジ時間 22,661 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,848 KB
testcase_01 AC 16 ms
7,756 KB
testcase_02 AC 16 ms
7,880 KB
testcase_03 AC 16 ms
7,812 KB
testcase_04 AC 16 ms
7,756 KB
testcase_05 AC 16 ms
7,756 KB
testcase_06 AC 16 ms
7,760 KB
testcase_07 AC 16 ms
7,788 KB
testcase_08 AC 16 ms
7,756 KB
testcase_09 AC 16 ms
7,852 KB
testcase_10 AC 16 ms
7,888 KB
testcase_11 AC 746 ms
64,648 KB
testcase_12 AC 734 ms
64,636 KB
testcase_13 AC 678 ms
64,672 KB
testcase_14 AC 658 ms
59,020 KB
testcase_15 AC 722 ms
67,756 KB
testcase_16 AC 448 ms
45,468 KB
testcase_17 AC 439 ms
45,396 KB
testcase_18 AC 16 ms
7,848 KB
testcase_19 AC 706 ms
65,652 KB
testcase_20 AC 740 ms
65,780 KB
testcase_21 AC 775 ms
66,284 KB
testcase_22 AC 683 ms
64,968 KB
testcase_23 AC 718 ms
64,376 KB
testcase_24 AC 703 ms
66,020 KB
testcase_25 AC 757 ms
65,996 KB
testcase_26 AC 812 ms
67,560 KB
testcase_27 AC 715 ms
65,276 KB
testcase_28 AC 707 ms
65,548 KB
testcase_29 AC 713 ms
65,144 KB
testcase_30 AC 810 ms
67,396 KB
testcase_31 AC 707 ms
65,576 KB
testcase_32 AC 752 ms
66,664 KB
testcase_33 AC 719 ms
66,788 KB
testcase_34 AC 680 ms
64,120 KB
testcase_35 AC 783 ms
67,008 KB
testcase_36 AC 802 ms
67,028 KB
testcase_37 AC 702 ms
64,644 KB
testcase_38 AC 803 ms
67,664 KB
testcase_39 AC 678 ms
65,720 KB
権限があれば一括ダウンロードができます

ソースコード

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