結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー tsugutsugutsugutsugu
提出日時 2023-03-05 16:02:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 94 ms / 2,000 ms
コード長 728 bytes
コンパイル時間 1,744 ms
コンパイル使用メモリ 173,164 KB
実行使用メモリ 27,416 KB
最終ジャッジ日時 2023-10-18 04:55:41
合計ジャッジ時間 8,584 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 69 ms
15,808 KB
testcase_12 AC 69 ms
15,808 KB
testcase_13 AC 68 ms
15,808 KB
testcase_14 AC 64 ms
15,808 KB
testcase_15 AC 84 ms
27,416 KB
testcase_16 AC 51 ms
10,852 KB
testcase_17 AC 51 ms
10,852 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 77 ms
15,536 KB
testcase_20 AC 79 ms
15,008 KB
testcase_21 AC 76 ms
15,008 KB
testcase_22 AC 69 ms
16,064 KB
testcase_23 AC 77 ms
15,008 KB
testcase_24 AC 75 ms
15,272 KB
testcase_25 AC 81 ms
15,008 KB
testcase_26 AC 93 ms
15,008 KB
testcase_27 AC 77 ms
15,800 KB
testcase_28 AC 67 ms
16,064 KB
testcase_29 AC 70 ms
16,064 KB
testcase_30 AC 90 ms
15,008 KB
testcase_31 AC 75 ms
15,800 KB
testcase_32 AC 75 ms
15,272 KB
testcase_33 AC 77 ms
15,272 KB
testcase_34 AC 75 ms
15,008 KB
testcase_35 AC 89 ms
15,008 KB
testcase_36 AC 89 ms
15,008 KB
testcase_37 AC 78 ms
15,008 KB
testcase_38 AC 94 ms
15,008 KB
testcase_39 AC 75 ms
15,536 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

int n;
vector<vector<int>> G;
vector<int> c;
int ans = 0;

int dfs(int pos, int pre) {
    int cnt = 0;
    for (auto u : G[pos]) {
        if (u != pre) {
            cnt += dfs(u, pos);
        }
    }
    if ((cnt + c[pos]) % 2 == 0) {
        ans++;
        return 1;
    }
    return 0;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n;
    G.resize(n);
    c.resize(n);
    for (int i = 0; i < n - 1; i++) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    for (int i = 0; i < n; i++) {
        cin >> c[i];
    }
    cout << (dfs(0, -1) == 0 ? ans : -1) << endl;
}
0