結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,356 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 162 ms
15,628 KB
testcase_12 AC 161 ms
15,604 KB
testcase_13 AC 158 ms
15,576 KB
testcase_14 AC 135 ms
15,544 KB
testcase_15 AC 173 ms
25,156 KB
testcase_16 AC 109 ms
10,768 KB
testcase_17 AC 107 ms
10,708 KB
testcase_18 AC 1 ms
4,352 KB
testcase_19 AC 164 ms
15,260 KB
testcase_20 AC 166 ms
14,780 KB
testcase_21 AC 166 ms
14,704 KB
testcase_22 AC 152 ms
15,848 KB
testcase_23 AC 165 ms
14,704 KB
testcase_24 AC 162 ms
15,052 KB
testcase_25 AC 168 ms
14,728 KB
testcase_26 AC 184 ms
14,788 KB
testcase_27 AC 164 ms
15,416 KB
testcase_28 AC 151 ms
15,756 KB
testcase_29 AC 155 ms
15,844 KB
testcase_30 AC 184 ms
14,764 KB
testcase_31 AC 164 ms
15,376 KB
testcase_32 AC 164 ms
15,124 KB
testcase_33 AC 165 ms
15,116 KB
testcase_34 AC 162 ms
14,768 KB
testcase_35 AC 176 ms
14,992 KB
testcase_36 AC 180 ms
14,860 KB
testcase_37 AC 165 ms
14,788 KB
testcase_38 AC 184 ms
14,836 KB
testcase_39 AC 160 ms
15,352 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