結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー ああいい
提出日時 2023-02-04 16:16:22
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 281 ms / 2,000 ms
コード長 712 bytes
コンパイル時間 196 ms
コンパイル使用メモリ 82,528 KB
実行使用メモリ 171,208 KB
最終ジャッジ日時 2024-07-03 13:25:30
合計ジャッジ時間 10,768 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

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

stack = [(0,-1),(~0,-1)]
ans = 0
while stack:
    now,parent = stack.pop()
    if now < 0:
        now = ~now
        for v in G[now]:
            if v != parent:
                stack.append((v,now))
                stack.append((~v,now))
    else:
        if now == 0:
            if c[now] == 1:
                print(ans)
            else:
                print(-1)
            break
        else:
            if c[now] == 0:
                ans += 1
                c[now] = 1
                c[parent] ^= 1

0