結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー aaaaaaaaaa2230
提出日時 2023-02-03 23:31:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 368 ms / 2,000 ms
コード長 599 bytes
コンパイル時間 185 ms
コンパイル使用メモリ 82,200 KB
実行使用メモリ 171,768 KB
最終ジャッジ日時 2024-07-02 21:34:18
合計ジャッジ時間 11,700 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(input())
e = [[] for i in range(n)]
for _ in range(n-1):
    a,b = [int(x)-1 for x in input().split()]
    e[a].append(b)
    e[b].append(a)
C = list(map(int,input().split()))
ans = 0

topo = []
vis = [0]*n
par = [-1]*n
q = [0]
vis[0] = 1
while q:
    now = q.pop()
    topo.append(now)
    for nex in e[now]:
        if vis[nex]:
            continue
        vis[nex] = 1
        par[nex] = now
        q.append(nex)

for now in topo[::-1]:
    if now == 0:
        break
    if C[now]:
        continue
    C[now] = 1
    C[par[now]] ^= 1
    ans += 1
if C[0] == 0:
    ans = -1
print(ans)
0