結果

問題 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  
実行時間 175 ms / 2,000 ms
コード長 1,160 bytes
コンパイル時間 700 ms
コンパイル使用メモリ 81,504 KB
実行使用メモリ 17,776 KB
最終ジャッジ日時 2023-08-24 01:15:12
合計ジャッジ時間 10,809 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,504 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 156 ms
17,268 KB
testcase_12 AC 158 ms
17,344 KB
testcase_13 AC 149 ms
15,632 KB
testcase_14 AC 133 ms
17,248 KB
testcase_15 AC 159 ms
15,656 KB
testcase_16 AC 119 ms
11,528 KB
testcase_17 AC 103 ms
11,440 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 160 ms
16,932 KB
testcase_20 AC 155 ms
14,804 KB
testcase_21 AC 162 ms
15,948 KB
testcase_22 AC 148 ms
17,776 KB
testcase_23 AC 158 ms
16,004 KB
testcase_24 AC 158 ms
16,368 KB
testcase_25 AC 166 ms
15,836 KB
testcase_26 AC 164 ms
15,908 KB
testcase_27 AC 158 ms
17,044 KB
testcase_28 AC 143 ms
17,256 KB
testcase_29 AC 149 ms
17,376 KB
testcase_30 AC 164 ms
15,760 KB
testcase_31 AC 175 ms
16,976 KB
testcase_32 AC 160 ms
16,180 KB
testcase_33 AC 159 ms
16,212 KB
testcase_34 AC 156 ms
15,904 KB
testcase_35 AC 163 ms
15,604 KB
testcase_36 AC 166 ms
15,788 KB
testcase_37 AC 156 ms
16,296 KB
testcase_38 AC 167 ms
14,800 KB
testcase_39 AC 154 ms
15,332 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