結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー tamatotamato
提出日時 2023-02-03 21:44:14
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 281 ms / 2,000 ms
コード長 1,063 bytes
コンパイル時間 258 ms
コンパイル使用メモリ 86,952 KB
実行使用メモリ 165,576 KB
最終ジャッジ日時 2023-09-15 17:25:23
合計ジャッジ時間 10,804 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 87 ms
71,412 KB
testcase_01 AC 88 ms
71,792 KB
testcase_02 AC 89 ms
71,492 KB
testcase_03 AC 87 ms
71,788 KB
testcase_04 AC 92 ms
71,624 KB
testcase_05 AC 87 ms
71,428 KB
testcase_06 AC 86 ms
71,596 KB
testcase_07 AC 86 ms
71,796 KB
testcase_08 AC 87 ms
71,712 KB
testcase_09 AC 88 ms
71,436 KB
testcase_10 AC 87 ms
71,788 KB
testcase_11 AC 276 ms
165,576 KB
testcase_12 AC 281 ms
165,404 KB
testcase_13 AC 278 ms
165,276 KB
testcase_14 AC 267 ms
162,884 KB
testcase_15 AC 258 ms
147,136 KB
testcase_16 AC 205 ms
121,976 KB
testcase_17 AC 204 ms
121,980 KB
testcase_18 AC 88 ms
71,220 KB
testcase_19 AC 273 ms
147,332 KB
testcase_20 AC 264 ms
145,368 KB
testcase_21 AC 260 ms
143,996 KB
testcase_22 AC 261 ms
140,720 KB
testcase_23 AC 267 ms
142,776 KB
testcase_24 AC 268 ms
145,820 KB
testcase_25 AC 267 ms
142,708 KB
testcase_26 AC 262 ms
145,668 KB
testcase_27 AC 274 ms
148,504 KB
testcase_28 AC 262 ms
140,544 KB
testcase_29 AC 270 ms
149,288 KB
testcase_30 AC 266 ms
145,820 KB
testcase_31 AC 267 ms
147,636 KB
testcase_32 AC 271 ms
145,144 KB
testcase_33 AC 271 ms
145,304 KB
testcase_34 AC 265 ms
143,580 KB
testcase_35 AC 262 ms
144,912 KB
testcase_36 AC 262 ms
144,676 KB
testcase_37 AC 263 ms
143,852 KB
testcase_38 AC 259 ms
144,332 KB
testcase_39 AC 263 ms
148,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 998244353


def main():
    import sys
    from collections import deque
    input = sys.stdin.readline

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

    que = deque()
    que.append(1)
    seen = [-1] * (N+1)
    seen[1] = 0
    par = [0] * (N+1)
    child = [[] for _ in range(N+1)]
    seq = []
    while que:
        v = que.popleft()
        seq.append(v)
        for u in adj[v]:
            if seen[u] == -1:
                seen[u] = seen[v] + 1
                par[u] = v
                child[v].append(u)
                que.append(u)
    seq.reverse()

    ans = 0
    for v in seq:
        if v == 1:
            if C[v] == 0:
                print(-1)
                exit()
        else:
            p = par[v]
            if C[v] == 0:
                C[v] = 1
                C[p] ^= 1
                ans += 1
    print(ans)


if __name__ == '__main__':
    main()
0