結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー HIcoder
提出日時 2023-07-10 23:28:12
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 201 ms / 2,000 ms
コード長 1,331 bytes
コンパイル時間 900 ms
コンパイル使用メモリ 103,888 KB
最終ジャッジ日時 2025-02-15 09:38:34
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<set>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<numeric>
#include<queue>
#include<cmath>
using namespace std;
typedef long long ll;
const ll INF=1LL<<60;
typedef pair<int,int> P;
typedef pair<int,P> PP;
const ll MOD=998244353;
const double PI=acos(-1);

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].push_back(b);
        G[b].push_back(a);
    }

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

    int ans=0;
    //葉からフリップを考える

    auto dfs=[&](auto f,int now,int pre)->bool{

        int cnt=0;
        for(int to:G[now]){
            if(to==pre) continue;
            if(!f(f,to,now)){
                //nowの直接の部下(to)が反転させた分.自分nowも反転
                c[now]^=1;
            }
        }

        if(now!=0){
            if(c[now]==0){
                c[now]^=1;
                ans++;

                return false;
            }else{
                return true;
            }
        }else{
            //now==0
            return c[now]==1;
        }


    };

    if(!dfs(dfs,0,-1)){
        cout<<-1<<endl;
    }else{
        cout<<ans<<endl;
    }
}
0