結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー HIcoderHIcoder
提出日時 2023-07-10 23:28:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 184 ms / 2,000 ms
コード長 1,331 bytes
コンパイル時間 1,536 ms
コンパイル使用メモリ 105,300 KB
実行使用メモリ 25,284 KB
最終ジャッジ日時 2023-10-10 01:26:33
合計ジャッジ時間 11,557 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,372 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,508 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,372 KB
testcase_11 AC 160 ms
15,740 KB
testcase_12 AC 162 ms
15,680 KB
testcase_13 AC 156 ms
15,620 KB
testcase_14 AC 136 ms
15,600 KB
testcase_15 AC 174 ms
25,284 KB
testcase_16 AC 109 ms
10,860 KB
testcase_17 AC 109 ms
10,696 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 164 ms
15,388 KB
testcase_20 AC 166 ms
14,728 KB
testcase_21 AC 166 ms
14,780 KB
testcase_22 AC 151 ms
15,716 KB
testcase_23 AC 164 ms
14,584 KB
testcase_24 AC 162 ms
15,124 KB
testcase_25 AC 168 ms
14,580 KB
testcase_26 AC 182 ms
14,768 KB
testcase_27 AC 163 ms
15,496 KB
testcase_28 AC 149 ms
15,840 KB
testcase_29 AC 154 ms
15,696 KB
testcase_30 AC 183 ms
14,584 KB
testcase_31 AC 162 ms
15,516 KB
testcase_32 AC 165 ms
15,108 KB
testcase_33 AC 165 ms
15,124 KB
testcase_34 AC 161 ms
14,776 KB
testcase_35 AC 176 ms
14,744 KB
testcase_36 AC 182 ms
14,804 KB
testcase_37 AC 166 ms
14,724 KB
testcase_38 AC 184 ms
14,804 KB
testcase_39 AC 162 ms
15,360 KB
権限があれば一括ダウンロードができます

ソースコード

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