結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー jianglyjiangly
提出日時 2023-02-09 21:39:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 89 ms / 2,000 ms
コード長 932 bytes
コンパイル時間 3,236 ms
コンパイル使用メモリ 203,380 KB
実行使用メモリ 23,936 KB
最終ジャッジ日時 2023-09-21 03:51:14
合計ジャッジ時間 9,650 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,356 KB
testcase_01 AC 2 ms
4,360 KB
testcase_02 AC 1 ms
4,360 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 2 ms
4,356 KB
testcase_06 AC 1 ms
4,356 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 1 ms
4,352 KB
testcase_10 AC 1 ms
4,356 KB
testcase_11 AC 67 ms
15,608 KB
testcase_12 AC 68 ms
15,496 KB
testcase_13 AC 66 ms
15,728 KB
testcase_14 AC 62 ms
15,508 KB
testcase_15 AC 80 ms
23,936 KB
testcase_16 AC 50 ms
10,616 KB
testcase_17 AC 48 ms
10,612 KB
testcase_18 AC 1 ms
4,360 KB
testcase_19 AC 75 ms
15,484 KB
testcase_20 AC 76 ms
14,516 KB
testcase_21 AC 75 ms
14,608 KB
testcase_22 AC 66 ms
15,640 KB
testcase_23 AC 74 ms
14,696 KB
testcase_24 AC 72 ms
14,880 KB
testcase_25 AC 74 ms
14,728 KB
testcase_26 AC 88 ms
14,944 KB
testcase_27 AC 74 ms
15,396 KB
testcase_28 AC 65 ms
15,664 KB
testcase_29 AC 68 ms
15,888 KB
testcase_30 AC 85 ms
14,620 KB
testcase_31 AC 72 ms
15,376 KB
testcase_32 AC 71 ms
15,012 KB
testcase_33 AC 72 ms
15,144 KB
testcase_34 AC 71 ms
14,684 KB
testcase_35 AC 81 ms
14,860 KB
testcase_36 AC 81 ms
14,852 KB
testcase_37 AC 74 ms
14,572 KB
testcase_38 AC 89 ms
14,752 KB
testcase_39 AC 72 ms
15,276 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using i64 = long long;

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int n;
    std::cin >> n;
    
    std::vector<std::vector<int>> adj(n);
    for (int i = 1; i < n; i++) {
        int x, y;
        std::cin >> x >> y;
        x--, y--;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }
    
    std::vector<int> c(n);
    for (int i = 0; i < n; i++) {
        std::cin >> c[i];
    }
    
    int ans = 0;
    auto dfs = [&](auto self, int x, int p) -> void {
        for (auto y : adj[x]) {
            if (y == p) {
                continue;
            }
            self(self, y, x);
            if (c[y] == 0) {
                c[y] ^= 1;
                c[x] ^= 1;
                ans++;
            }
        }
    };
    dfs(dfs, 0, -1);
    
    if (c[0] == 0) {
        ans = -1;
    }
    std::cout << ans << "\n";
    
    return 0;
}
0