結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー rlangevinrlangevin
提出日時 2023-02-03 22:40:04
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 332 ms / 2,000 ms
コード長 778 bytes
コンパイル時間 300 ms
コンパイル使用メモリ 87,244 KB
実行使用メモリ 148,516 KB
最終ジャッジ日時 2023-09-15 18:48:44
合計ジャッジ時間 11,892 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,276 KB
testcase_01 AC 71 ms
71,352 KB
testcase_02 AC 72 ms
71,468 KB
testcase_03 AC 71 ms
71,548 KB
testcase_04 AC 71 ms
71,676 KB
testcase_05 AC 73 ms
71,544 KB
testcase_06 AC 71 ms
71,560 KB
testcase_07 AC 72 ms
71,356 KB
testcase_08 AC 72 ms
71,148 KB
testcase_09 AC 71 ms
71,416 KB
testcase_10 AC 70 ms
71,412 KB
testcase_11 AC 301 ms
148,412 KB
testcase_12 AC 299 ms
148,380 KB
testcase_13 AC 300 ms
148,368 KB
testcase_14 AC 292 ms
148,516 KB
testcase_15 AC 280 ms
134,548 KB
testcase_16 AC 228 ms
112,356 KB
testcase_17 AC 226 ms
112,448 KB
testcase_18 AC 73 ms
71,420 KB
testcase_19 AC 295 ms
131,588 KB
testcase_20 AC 306 ms
129,444 KB
testcase_21 AC 308 ms
129,404 KB
testcase_22 AC 291 ms
127,872 KB
testcase_23 AC 290 ms
129,248 KB
testcase_24 AC 292 ms
130,600 KB
testcase_25 AC 308 ms
129,324 KB
testcase_26 AC 324 ms
129,280 KB
testcase_27 AC 289 ms
131,888 KB
testcase_28 AC 285 ms
127,580 KB
testcase_29 AC 287 ms
131,204 KB
testcase_30 AC 323 ms
129,564 KB
testcase_31 AC 299 ms
131,020 KB
testcase_32 AC 301 ms
130,300 KB
testcase_33 AC 306 ms
130,484 KB
testcase_34 AC 281 ms
129,192 KB
testcase_35 AC 332 ms
129,652 KB
testcase_36 AC 319 ms
129,480 KB
testcase_37 AC 293 ms
129,188 KB
testcase_38 AC 317 ms
129,136 KB
testcase_39 AC 301 ms
131,284 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def non_rec_dfs(s):
    stack = []
    stack.append(~s)
    stack.append(s)
    par = [-1] * N
    ans = 0
    while stack:
        u = stack.pop()
        if u >= 0:
            stack.append(~u)
            for v in G[u]:
                if v == par[u]:
                    continue
                par[v] = u
                stack.append(v)
        else:
            u = ~u
            if C[u] == 0:
                C[u] = 1
                C[par[u]] = 1 - C[par[u]]
                ans += 1
    return ans

N = int(input())
G = [[] for i in range(N)]
for i in range(N - 1):
    A, B = map(int, input().split())
    A, B = A - 1, B - 1
    G[A].append(B)
    G[B].append(A)
    
C = list(map(int, input().split()))
ans = non_rec_dfs(0)
print(ans) if sum(C) == N else print(-1)
0