結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー simansiman
提出日時 2023-02-07 15:55:07
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 193 ms / 2,000 ms
コード長 911 bytes
コンパイル時間 8,001 ms
コンパイル使用メモリ 102,720 KB
実行使用メモリ 27,616 KB
最終ジャッジ日時 2023-09-18 23:09:53
合計ジャッジ時間 20,200 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,252 KB
testcase_01 AC 3 ms
8,152 KB
testcase_02 AC 4 ms
8,108 KB
testcase_03 AC 4 ms
8,116 KB
testcase_04 AC 4 ms
8,232 KB
testcase_05 AC 3 ms
8,196 KB
testcase_06 AC 5 ms
8,196 KB
testcase_07 AC 3 ms
8,392 KB
testcase_08 AC 4 ms
8,224 KB
testcase_09 AC 4 ms
8,660 KB
testcase_10 AC 4 ms
8,272 KB
testcase_11 AC 168 ms
15,632 KB
testcase_12 AC 167 ms
15,776 KB
testcase_13 AC 161 ms
15,592 KB
testcase_14 AC 140 ms
15,640 KB
testcase_15 AC 178 ms
27,616 KB
testcase_16 AC 112 ms
12,788 KB
testcase_17 AC 110 ms
12,708 KB
testcase_18 AC 3 ms
8,204 KB
testcase_19 AC 169 ms
15,788 KB
testcase_20 AC 173 ms
15,224 KB
testcase_21 AC 170 ms
15,264 KB
testcase_22 AC 158 ms
16,148 KB
testcase_23 AC 169 ms
15,232 KB
testcase_24 AC 169 ms
15,524 KB
testcase_25 AC 176 ms
15,252 KB
testcase_26 AC 186 ms
15,136 KB
testcase_27 AC 167 ms
15,980 KB
testcase_28 AC 160 ms
16,220 KB
testcase_29 AC 157 ms
16,324 KB
testcase_30 AC 193 ms
15,196 KB
testcase_31 AC 168 ms
15,964 KB
testcase_32 AC 171 ms
15,544 KB
testcase_33 AC 171 ms
15,440 KB
testcase_34 AC 167 ms
15,224 KB
testcase_35 AC 187 ms
15,328 KB
testcase_36 AC 186 ms
15,288 KB
testcase_37 AC 170 ms
15,188 KB
testcase_38 AC 189 ms
15,232 KB
testcase_39 AC 167 ms
15,792 KB
権限があれば一括ダウンロードができます

ソースコード

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