結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー Manuel1024
提出日時 2023-08-24 01:20:53
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 200 ms / 2,000 ms
コード長 748 bytes
コンパイル時間 849 ms
コンパイル使用メモリ 78,404 KB
実行使用メモリ 30,600 KB
最終ジャッジ日時 2024-12-22 13:34:06
合計ジャッジ時間 7,767 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;

int dfs(const vector<vector<int>> &G, vector<int> &c, int v, int p = -1){
    int ans = 0;
    for(const auto &it: G[v]){
        if(it == p) continue;
        ans += dfs(G, c, it, v);
    }
    if(p == -1 && c[v] == 0) return -1;
    if(c[v] == 0){
        c[v] ^= 1;
        c[p] ^= 1;
        ans++;
    }
    return ans;
}

int main(){
    int n; cin >> n;
    vector<vector<int>> G(n);
    for(int i = 0; i < n-1; i++){
        int a, b; cin >> a >> b;
        a--; b--;
        G[a].emplace_back(b);
        G[b].emplace_back(a);
    }
    vector<int> c(n);
    for(auto &it: c) cin >> it;

    cout << dfs(G, c, 0) << endl;    
    return 0;
}
0