結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー nono00nono00
提出日時 2023-02-10 02:52:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 170 ms / 2,000 ms
コード長 1,508 bytes
コンパイル時間 2,804 ms
コンパイル使用メモリ 207,200 KB
実行使用メモリ 18,380 KB
最終ジャッジ日時 2023-09-21 07:58:12
合計ジャッジ時間 11,973 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 162 ms
18,100 KB
testcase_12 AC 162 ms
18,064 KB
testcase_13 AC 156 ms
17,964 KB
testcase_14 AC 136 ms
17,988 KB
testcase_15 AC 164 ms
16,376 KB
testcase_16 AC 107 ms
12,044 KB
testcase_17 AC 107 ms
12,224 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 164 ms
17,428 KB
testcase_20 AC 167 ms
16,708 KB
testcase_21 AC 166 ms
16,560 KB
testcase_22 AC 152 ms
18,380 KB
testcase_23 AC 163 ms
16,816 KB
testcase_24 AC 163 ms
17,104 KB
testcase_25 AC 166 ms
16,628 KB
testcase_26 AC 166 ms
16,408 KB
testcase_27 AC 163 ms
17,676 KB
testcase_28 AC 151 ms
17,908 KB
testcase_29 AC 155 ms
18,256 KB
testcase_30 AC 166 ms
16,284 KB
testcase_31 AC 163 ms
17,672 KB
testcase_32 AC 164 ms
17,216 KB
testcase_33 AC 163 ms
17,108 KB
testcase_34 AC 161 ms
17,012 KB
testcase_35 AC 169 ms
16,736 KB
testcase_36 AC 168 ms
16,552 KB
testcase_37 AC 165 ms
17,180 KB
testcase_38 AC 170 ms
16,644 KB
testcase_39 AC 162 ms
17,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, l, r) for (int i = (int)(l); i < (int)(r); i++)
#define rrep(i, r, l) for (int i = (int)(r); i > (int)(l); i--)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

// 適当に根付き木にする
// 深い頂点から白なら黒にする
//      このとき、親頂点の色も反転
// 根以外の全てを黒にした時、根も黒ならOK
//

void solve() {
    int n;
    cin >> n;
    vector<vector<int>> graph(n, vector<int>());
    rep(i, 0, n - 1) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }
    vector<int> color(n), parent(n, -1);
    rep(i, 0, n) cin >> color[i];

    int root = 0;
    queue<int> q;
    stack<int> stack;
    q.push(root);
    while(!q.empty()) {
        int u = q.front(); q.pop();
        for (int v: graph[u]) {
            if (v == parent[u]) {
                continue;
            }
            parent[v] = u;
            q.push(v);
            stack.push(v);
        }
    }

    int count = 0;
    while (!stack.empty()) {
        int u = stack.top(); stack.pop();
        if (color[u] == 0) {
            color[u] = 1;
            color[parent[u]] ^= 1;
            count++;
        }
    }

    if (color[root] == 0) {
        cout << -1 << '\n';
    } else {
        cout << count << '\n';
    }
}

int main() {
    int q = 1;
    // cin >> q;
    while (q--) {
        solve();
    }
}
0