結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー srjywrdnprkt
提出日時 2023-01-23 19:58:50
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 206 ms / 2,000 ms
コード長 705 bytes
コンパイル時間 790 ms
コンパイル使用メモリ 74,612 KB
最終ジャッジ日時 2025-02-10 06:29:51
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

using namespace std;

int ans = 0;
vector<int> c;

int dfs(vector<vector<int>> &E, int from, int p){

    int cnt=0;
    for(auto to : E[from]){
        if (p == to) continue;
        cnt += dfs(E, to, from);
    }

    if ((cnt + c[from]) % 2 == 0){
        ans++;
        return 1;
    }
    else return 0;
}

int main(){

    int N, a, b;
    cin >> N;
    vector<vector<int>> E(N);
    c.resize(N);

    for (int i=0; i<N-1; i++){
        cin >> a >> b; a--; b--;
        E[a].push_back(b);
        E[b].push_back(a);
    }

    for (int i=0; i<N; i++) cin >> c[i];

    if (dfs(E, 0, -1) == 1) cout << -1 << endl;
    else cout << ans << endl;

    return 0;
}
0