結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー tassei903tassei903
提出日時 2023-02-04 11:08:55
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 297 ms / 2,000 ms
コード長 962 bytes
コンパイル時間 684 ms
コンパイル使用メモリ 86,676 KB
実行使用メモリ 160,028 KB
最終ジャッジ日時 2023-09-16 08:12:50
合計ジャッジ時間 11,384 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
70,836 KB
testcase_01 AC 70 ms
71,092 KB
testcase_02 AC 70 ms
71,128 KB
testcase_03 AC 70 ms
70,848 KB
testcase_04 AC 69 ms
71,160 KB
testcase_05 AC 70 ms
70,836 KB
testcase_06 AC 70 ms
71,160 KB
testcase_07 AC 70 ms
70,992 KB
testcase_08 AC 70 ms
71,096 KB
testcase_09 AC 70 ms
71,044 KB
testcase_10 AC 71 ms
70,936 KB
testcase_11 AC 293 ms
160,028 KB
testcase_12 AC 271 ms
154,796 KB
testcase_13 AC 280 ms
158,396 KB
testcase_14 AC 262 ms
154,920 KB
testcase_15 AC 245 ms
130,668 KB
testcase_16 AC 219 ms
116,556 KB
testcase_17 AC 215 ms
116,468 KB
testcase_18 AC 71 ms
71,140 KB
testcase_19 AC 278 ms
142,028 KB
testcase_20 AC 281 ms
135,148 KB
testcase_21 AC 285 ms
135,280 KB
testcase_22 AC 264 ms
131,848 KB
testcase_23 AC 270 ms
138,724 KB
testcase_24 AC 286 ms
138,484 KB
testcase_25 AC 285 ms
133,788 KB
testcase_26 AC 282 ms
130,528 KB
testcase_27 AC 276 ms
143,832 KB
testcase_28 AC 268 ms
132,044 KB
testcase_29 AC 272 ms
145,904 KB
testcase_30 AC 293 ms
130,700 KB
testcase_31 AC 279 ms
142,684 KB
testcase_32 AC 279 ms
135,796 KB
testcase_33 AC 279 ms
136,280 KB
testcase_34 AC 275 ms
139,380 KB
testcase_35 AC 292 ms
131,576 KB
testcase_36 AC 291 ms
131,588 KB
testcase_37 AC 275 ms
140,168 KB
testcase_38 AC 297 ms
130,596 KB
testcase_39 AC 276 ms
143,164 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda :sys.stdin.readline()[:-1]
ni = lambda :int(input())
na = lambda :list(map(int,input().split()))
yes = lambda :print("yes");Yes = lambda :print("Yes");YES = lambda : print("YES")
no = lambda :print("no");No = lambda :print("No");NO = lambda : print("NO")
#######################################################################
n = ni()
g = [[] for i in range(n)]
deg = [0] * n
for _ in range(n-1):
    a,b = na()
    a-=1
    b-=1
    g[a].append(b)
    g[b].append(a)
    deg[a] += 1
    deg[b] += 1
c = [i^1 for i in na()]
p = [i for i in range(n) if deg[i] == 1]
ans = 0
while p:
    x = p.pop()
    #print(p, x)
    deg[x] -= 1
    for y in g[x]:
        if deg[y] >= 1:
            deg[y] -= 1
        if deg[y] == 1:
            p.append(y)
        if deg[y] >= 1 and c[x] == 1:
            c[x] ^= 1
            c[y] ^= 1
            ans += 1
if sum(c) == 2:
    print(ans+1)
elif sum(c) == 1:
    print(-1)
else:
    print(ans)
0