結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー siman
提出日時 2023-02-07 15:55:07
言語 C++17(clang)
(17.0.6 + boost 1.87.0)
結果
AC  
実行時間 172 ms / 2,000 ms
コード長 911 bytes
コンパイル時間 3,553 ms
コンパイル使用メモリ 140,652 KB
実行使用メモリ 27,588 KB
最終ジャッジ日時 2024-07-05 12:11:05
合計ジャッジ時間 10,352 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <map>
#include <queue>
#include <set>
#include <cstring>
#include <vector>

using namespace std;
typedef long long ll;

int N;
int C[200010];
vector<int> E[200010];

bool dfs(int v, int parent, int &ans) {
  int switch_cnt = 0;
  int color = C[v];

  for (int u : E[v]) {
    if (u == parent) continue;

    if (dfs(u, v, ans)) {
      ++switch_cnt;
      ++ans;
    }
  }

  if ((color + switch_cnt) % 2 == 0) {
    return true;
  } else {
    return false;
  }
}

int main() {
  cin >> N;
  for (int i = 0; i < N - 1; ++i) {
    int a, b;
    cin >> a >> b;

    E[a].push_back(b);
    E[b].push_back(a);
  }

  for (int i = 1; i <= N; ++i) {
    cin >> C[i];
  }

  int ans = 0;

  if (dfs(1, -1, ans)) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }

  return 0;
}
0