結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー gr1msl3y
提出日時 2023-06-08 22:11:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 382 ms / 2,000 ms
コード長 753 bytes
コンパイル時間 537 ms
コンパイル使用メモリ 82,416 KB
実行使用メモリ 181,920 KB
最終ジャッジ日時 2024-12-31 06:27:12
合計ジャッジ時間 12,562 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

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