結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー i_am_noob
提出日時 2023-02-15 18:36:43
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 101 ms / 2,000 ms
コード長 691 bytes
コンパイル時間 2,169 ms
コンパイル使用メモリ 194,020 KB
最終ジャッジ日時 2025-02-10 15:43:45
ジャッジサーバーID
(参考情報)
judge3 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using pii=pair<int,int>;

#define all(a) a.begin(),a.end()
#define pb push_back
#define sz(a) ((int)a.size())

const int maxn=200005;
int n,a[maxn],res;
vector<int> adj[maxn];

void dfs(int u, int par){
    for(auto v: adj[u]) if(v!=par) dfs(v,u),a[u]^=a[v];
    res+=a[u];
}

signed main(){
    ios_base::sync_with_stdio(0),cin.tie(0);
    cin >> n;
    for(int i=0; i<n-1; ++i){
        int u,v; cin >> u >> v; u--,v--;
        adj[u].pb(v),adj[v].pb(u);
    }
    for(int i=0; i<n; ++i) cin >> a[i],a[i]^=1;
    if(count(a,a+n,1)&1){
        cout << "-1\n";
        return 0;
    }
    dfs(0,-1);
    cout << res << "\n";
}
0