結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー gr1msl3ygr1msl3y
提出日時 2023-06-08 22:11:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 351 ms / 2,000 ms
コード長 753 bytes
コンパイル時間 388 ms
コンパイル使用メモリ 87,284 KB
実行使用メモリ 182,932 KB
最終ジャッジ日時 2023-08-30 01:41:59
合計ジャッジ時間 15,366 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,408 KB
testcase_01 AC 71 ms
71,160 KB
testcase_02 AC 71 ms
71,272 KB
testcase_03 AC 70 ms
71,088 KB
testcase_04 AC 71 ms
71,420 KB
testcase_05 AC 71 ms
71,164 KB
testcase_06 AC 71 ms
71,512 KB
testcase_07 AC 70 ms
71,164 KB
testcase_08 AC 70 ms
71,408 KB
testcase_09 AC 73 ms
71,164 KB
testcase_10 AC 70 ms
71,448 KB
testcase_11 AC 351 ms
180,528 KB
testcase_12 AC 346 ms
180,932 KB
testcase_13 AC 341 ms
179,928 KB
testcase_14 AC 315 ms
182,932 KB
testcase_15 AC 301 ms
144,640 KB
testcase_16 AC 243 ms
121,812 KB
testcase_17 AC 240 ms
121,768 KB
testcase_18 AC 70 ms
71,408 KB
testcase_19 AC 320 ms
146,840 KB
testcase_20 AC 332 ms
144,840 KB
testcase_21 AC 331 ms
144,584 KB
testcase_22 AC 310 ms
142,952 KB
testcase_23 AC 313 ms
144,604 KB
testcase_24 AC 312 ms
145,556 KB
testcase_25 AC 329 ms
144,368 KB
testcase_26 AC 342 ms
144,388 KB
testcase_27 AC 311 ms
147,016 KB
testcase_28 AC 307 ms
142,848 KB
testcase_29 AC 307 ms
132,516 KB
testcase_30 AC 345 ms
144,588 KB
testcase_31 AC 320 ms
146,556 KB
testcase_32 AC 326 ms
145,432 KB
testcase_33 AC 325 ms
145,532 KB
testcase_34 AC 299 ms
144,636 KB
testcase_35 AC 340 ms
144,644 KB
testcase_36 AC 339 ms
144,596 KB
testcase_37 AC 311 ms
144,624 KB
testcase_38 AC 337 ms
144,444 KB
testcase_39 AC 318 ms
147,004 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

C = [0]+list(map(int, input().split()))
parent = [-1]*(N+1)
task = [1]
for v in task:
    for u in graph[v]:
        if parent[v] == u:
            continue
        task.append(u)
        parent[u] = v

ans = 0
task = [N+1, 1]
while task:
    t = task.pop()
    if t > N:
        t -= N
        if t == 1:
            continue
        if not C[t]:
            C[t] ^= 1
            C[parent[t]] ^= 1
            ans += 1
    else:
        for u in graph[t]:
            if u == parent[t]:
                continue
            task.append(u+N)
            task.append(u)

print(ans if C[1] else -1)
0