結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー tsugutsugu
提出日時 2023-03-05 16:02:06
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 95 ms / 2,000 ms
コード長 728 bytes
コンパイル時間 1,702 ms
コンパイル使用メモリ 172,988 KB
実行使用メモリ 27,264 KB
最終ジャッジ日時 2024-09-18 01:36:34
合計ジャッジ時間 6,708 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

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