結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー gyouzasushigyouzasushi
提出日時 2023-02-03 21:45:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 188 ms / 2,000 ms
コード長 1,837 bytes
コンパイル時間 2,252 ms
コンパイル使用メモリ 205,528 KB
実行使用メモリ 23,780 KB
最終ジャッジ日時 2023-09-15 17:26:16
合計ジャッジ時間 8,696 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 160 ms
15,904 KB
testcase_12 AC 161 ms
15,620 KB
testcase_13 AC 154 ms
15,564 KB
testcase_14 AC 133 ms
15,760 KB
testcase_15 AC 169 ms
23,780 KB
testcase_16 AC 108 ms
10,652 KB
testcase_17 AC 107 ms
10,580 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 160 ms
15,424 KB
testcase_20 AC 163 ms
14,728 KB
testcase_21 AC 164 ms
15,096 KB
testcase_22 AC 149 ms
15,644 KB
testcase_23 AC 168 ms
14,844 KB
testcase_24 AC 161 ms
15,248 KB
testcase_25 AC 164 ms
14,736 KB
testcase_26 AC 181 ms
14,732 KB
testcase_27 AC 161 ms
15,472 KB
testcase_28 AC 149 ms
15,648 KB
testcase_29 AC 151 ms
15,848 KB
testcase_30 AC 182 ms
14,840 KB
testcase_31 AC 159 ms
15,584 KB
testcase_32 AC 161 ms
15,060 KB
testcase_33 AC 162 ms
15,060 KB
testcase_34 AC 160 ms
14,800 KB
testcase_35 AC 174 ms
14,896 KB
testcase_36 AC 178 ms
14,856 KB
testcase_37 AC 163 ms
15,092 KB
testcase_38 AC 188 ms
14,776 KB
testcase_39 AC 159 ms
15,320 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
using namespace std;
using ll = long long;
const int INF = 1e9;
const ll LINF = 1e18;
template <class T>
void get_unique(vector<T>& x) {
    x.erase(unique(x.begin(), x.end()), x.end());
}
template <class T>
bool chmax(T& a, const T& b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T& a, const T& b) {
    if (b < a) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
vector<T> make_vec(size_t a) {
    return vector<T>(a);
}
template <class T, class... Ts>
auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T>
istream& operator>>(istream& is, vector<T>& v) {
    for (int i = 0; i < int(v.size()); i++) {
        is >> v[i];
    }
    return is;
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    for (int i = 0; i < int(v.size()); i++) {
        os << v[i];
        if (i < sz(v) - 1) os << ' ';
    }
    return os;
}
int main() {
    int n;
    cin >> n;
    vector<vector<int>> g(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);
    cin >> c;

    int ans = 0;
    auto dfs = [&](auto dfs, int u, int p) -> void {
        for (int v : g[u]) {
            if (v == p) continue;
            dfs(dfs, v, u);
            if (c[v] == 0) {
                c[v] ^= 1;
                c[u] ^= 1;
                ans++;
            }
        }
    };
    dfs(dfs, 0, -1);
    if (c[0] == 0) ans = -1;
    cout << ans << '\n';
}
0