結果

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

ソースコード

diff #

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;
 
#include<bits/stdc++.h>
using namespace std;

vector<int> G[200200];
int dp[200200][2];

int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  
  rep(i,200200) rep(j,2) dp[i][j]=-1;
  
  int n;
  cin>>n;
  rep(i,n-1){
    int a,b;
    cin>>a>>b;
    a--,b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  vector<int> C(n);
  rep(i,n) cin>>C[i];
  
  auto dfs=[&](auto dfs,int v,int p)->void{
    int c=0;
    if(C[v]==0) c++;
    int sum=0;
    for(auto nv:G[v]){
      if(nv==p) continue;
      dfs(dfs,nv,v);
      if(dp[nv][1]!=-1){
        c++;
        sum+=dp[nv][1]+1;
      }
      else sum+=dp[nv][0];
    }
    if(c%2==0) dp[v][0]=sum;
    else dp[v][1]=sum;
  };
  
  dfs(dfs,0,-1);
  cout<<dp[0][0]<<endl;
  
  return 0;
}
0