結果
| 問題 |
No.2205 Lights Out on Christmas Tree
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-07-10 23:28:49 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 229 ms / 2,000 ms |
| コード長 | 1,406 bytes |
| コンパイル時間 | 1,117 ms |
| コンパイル使用メモリ | 103,924 KB |
| 最終ジャッジ日時 | 2025-02-15 09:38:45 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 37 |
ソースコード
/*
葉からフリップさせるしかない.
自由度に注目する
*/
#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;
}
}