結果
問題 | No.2205 Lights Out on Christmas Tree |
ユーザー |
|
提出日時 | 2023-02-06 09:50:56 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 190 ms / 2,000 ms |
コード長 | 1,837 bytes |
コンパイル時間 | 3,776 ms |
コンパイル使用メモリ | 232,612 KB |
実行使用メモリ | 34,560 KB |
最終ジャッジ日時 | 2024-07-04 15:34:23 |
合計ジャッジ時間 | 11,305 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 37 |
ソースコード
#include <bits/stdc++.h>#include "atcoder/all"#define debug cout << "OK" << endl;template<typename T>inline bool chmax(T& a, const T b) { if (a < b) { a = b; return true; } return false; }template<typename T>inline bool chmin(T& a, const T b) { if (a > b) { a = b; return true; } return false; }inline int Code(char c) {if ('A' <= c and c <= 'Z')return (int)(c - 'A');if ('a' <= c and c <= 'z')return (int)(c - 'a');if ('0' <= c and c <= '9')return (int)(c - '0');assert(false);}using namespace std;using namespace atcoder;#define int long longconstexpr int MOD = 998244353;constexpr int INF = (1LL << 62);using minit = modint998244353;template<typename T>ostream& operator << (ostream& st, const vector<T> &v) {for (const T value : v) {st << value << " ";}return st;}template<typename T>istream& operator >> (istream& st, vector<T>& v) {for (T &value : v) {st >> value;}return st;}////void solve() {//#1 入力int N;cin >> N;vector<vector<int>> G(N);for (int i = 0; i < N - 1; i++) {int u, v;cin >> u >> v;u--; v--;G[u].push_back(v);G[v].push_back(u);}vector<int> color(N);cin >> color;//#2 再帰function<pair<int, int>(int, int)> dfs = [&](int now, int par) {//nowを根とする部分木を黒くするのに必要な操作回数// first...根は黒にできるか// second...最小の操作回数int res = 0, cnt = 0;for (const int next : G[now]) {if (next == par)continue;pair<int,int> p = dfs(next, now);cnt += 1 - p.first;res += p.second;}return make_pair((cnt ^ color[now]) & 1, res + cnt);};//#3 出力pair<int, int> p = dfs(0, -1);if (p.first)cout << p.second << endl;else cout << -1 << endl;}signed main() {int T = 1;// cin >> T;for (int i = 0; i < T; i++)solve();return 0;}