結果

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cassert>

using namespace std;

int ans = 0, num=0;
vector<int> c;
vector<bool> visit;

int dfs(vector<vector<int>> &E, int from, int p){
    visit[from] = 1;
    num++;
    int cnt=0;
    for(auto to : E[from]){
        if (p == to) continue;
        assert(!visit[to]);
        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;
    assert(1<=N && N<=2e5);
    vector<vector<int>> E(N);
    c.resize(N);
    visit.resize(N, 0);

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

    for (int i=0; i<N; i++){
        cin >> c[i];
        assert(c[i] == 0 || c[i] == 1);
    }

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

    return 0;
}

0