結果
| 問題 | No.2205 Lights Out on Christmas Tree |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-02-09 21:39:44 |
| 言語 | C++17 (gcc 15.2.0 + boost 1.90.0) |
| 結果 |
AC
|
| 実行時間 | 49 ms / 2,000 ms |
| コード長 | 932 bytes |
| 記録 | |
| コンパイル時間 | 2,182 ms |
| コンパイル使用メモリ | 216,280 KB |
| 実行使用メモリ | 24,064 KB |
| 最終ジャッジ日時 | 2026-06-29 12:42:30 |
| 合計ジャッジ時間 | 5,915 ms |
|
ジャッジサーバーID (参考情報) |
judge1_0 / judge3_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 37 |
ソースコード
#include <bits/stdc++.h>
using i64 = long long;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
std::vector<std::vector<int>> adj(n);
for (int i = 1; i < n; i++) {
int x, y;
std::cin >> x >> y;
x--, y--;
adj[x].push_back(y);
adj[y].push_back(x);
}
std::vector<int> c(n);
for (int i = 0; i < n; i++) {
std::cin >> c[i];
}
int ans = 0;
auto dfs = [&](auto self, int x, int p) -> void {
for (auto y : adj[x]) {
if (y == p) {
continue;
}
self(self, y, x);
if (c[y] == 0) {
c[y] ^= 1;
c[x] ^= 1;
ans++;
}
}
};
dfs(dfs, 0, -1);
if (c[0] == 0) {
ans = -1;
}
std::cout << ans << "\n";
return 0;
}