結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー srjywrdnprkt
提出日時 2023-08-27 02:44:21
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 176 ms / 2,000 ms
コード長 757 bytes
コンパイル時間 1,817 ms
コンパイル使用メモリ 198,260 KB
最終ジャッジ日時 2025-02-16 14:58:32
ジャッジサーバーID
(参考情報)
judge2 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main(){

    int N, a, b, ans=0;
    cin >> N;
    vector<vector<int>> E(N);
    vector<int> c(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];

    auto dfs=[&](auto self, int from, int p)->int{
        int res=0;
        for (auto to : E[from]){
            if (to == p) continue;
            res += self(self, to, from);
        }
        if ((c[from]+res) % 2 == 0){
            ans++;
            return 1;
        }
        else return 0;
    };

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

    return 0;
}
0