結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー Manuel1024Manuel1024
提出日時 2023-08-24 01:15:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 168 ms / 2,000 ms
コード長 1,160 bytes
コンパイル時間 835 ms
コンパイル使用メモリ 81,996 KB
実行使用メモリ 17,572 KB
最終ジャッジ日時 2024-06-01 22:38:23
合計ジャッジ時間 8,016 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 160 ms
17,184 KB
testcase_12 AC 157 ms
17,308 KB
testcase_13 AC 152 ms
15,644 KB
testcase_14 AC 134 ms
17,184 KB
testcase_15 AC 160 ms
15,696 KB
testcase_16 AC 104 ms
11,648 KB
testcase_17 AC 106 ms
11,648 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 156 ms
17,024 KB
testcase_20 AC 156 ms
14,976 KB
testcase_21 AC 168 ms
15,872 KB
testcase_22 AC 147 ms
17,536 KB
testcase_23 AC 167 ms
16,184 KB
testcase_24 AC 164 ms
16,384 KB
testcase_25 AC 157 ms
15,904 KB
testcase_26 AC 165 ms
15,616 KB
testcase_27 AC 160 ms
17,024 KB
testcase_28 AC 149 ms
17,536 KB
testcase_29 AC 149 ms
17,572 KB
testcase_30 AC 164 ms
15,616 KB
testcase_31 AC 155 ms
17,084 KB
testcase_32 AC 161 ms
16,372 KB
testcase_33 AC 160 ms
16,256 KB
testcase_34 AC 165 ms
16,256 KB
testcase_35 AC 162 ms
15,964 KB
testcase_36 AC 162 ms
15,744 KB
testcase_37 AC 166 ms
16,256 KB
testcase_38 AC 163 ms
14,880 KB
testcase_39 AC 154 ms
15,360 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;

int main(){
    int n; cin >> n;
    vector<vector<int>> G(n);
    for(int i = 0; i < n-1; i++){
        int a, b; cin >> a >> b;
        a--; b--;
        G[a].emplace_back(b);
        G[b].emplace_back(a);
    }
    vector<int> c(n);
    for(auto &it: c) cin >> it;

    int blackcnt = 0;
    for(auto &it: c) blackcnt += it;
    if(blackcnt%2 != n%2){
        cout << -1 << endl;
        return 0;
    }

    vector<int> in(n);
    queue<int> Q;
    for(int i = 0; i < n; i++){
        in[i] = G[i].size();
        if(in[i] == 1) Q.emplace(i);
    }

    int ans = 0;
    while(!Q.empty()){
        int cur = Q.front(); Q.pop();
        in[cur]--;
        int nex = -1;
        for(const auto &it: G[cur]){
            if(in[it] > 0){
                nex = it;
                break;
            }
        }
        if(nex == -1) break;
        if(c[cur] == 0){
            c[cur] = c[cur]^1;
            c[nex] = c[nex]^1;
            ans++;
        }
        in[nex]--;
        if(in[nex] == 1) Q.emplace(nex);
    }
    cout << ans << endl;
    return 0;
}
0