結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-01-23 21:26:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 191 ms / 2,000 ms
コード長 1,012 bytes
コンパイル時間 1,005 ms
コンパイル使用メモリ 82,164 KB
実行使用メモリ 30,396 KB
最終ジャッジ日時 2023-09-07 21:52:15
合計ジャッジ時間 8,829 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,384 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,384 KB
testcase_11 AC 159 ms
15,812 KB
testcase_12 AC 158 ms
15,644 KB
testcase_13 AC 154 ms
15,756 KB
testcase_14 AC 131 ms
15,540 KB
testcase_15 AC 174 ms
30,396 KB
testcase_16 AC 107 ms
10,736 KB
testcase_17 AC 105 ms
10,784 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 161 ms
15,340 KB
testcase_20 AC 166 ms
14,844 KB
testcase_21 AC 167 ms
14,952 KB
testcase_22 AC 151 ms
15,916 KB
testcase_23 AC 163 ms
14,960 KB
testcase_24 AC 162 ms
15,212 KB
testcase_25 AC 172 ms
14,880 KB
testcase_26 AC 186 ms
14,808 KB
testcase_27 AC 162 ms
15,608 KB
testcase_28 AC 150 ms
15,612 KB
testcase_29 AC 157 ms
16,000 KB
testcase_30 AC 186 ms
14,772 KB
testcase_31 AC 164 ms
15,604 KB
testcase_32 AC 165 ms
15,284 KB
testcase_33 AC 166 ms
15,144 KB
testcase_34 AC 167 ms
14,836 KB
testcase_35 AC 182 ms
14,944 KB
testcase_36 AC 184 ms
14,908 KB
testcase_37 AC 164 ms
14,884 KB
testcase_38 AC 191 ms
14,832 KB
testcase_39 AC 162 ms
15,288 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cassert>

using namespace std;

int ans = 0, num=0;
vector<int> c;
vector<bool> visit;

int dfs(vector<vector<int>> &E, int from, int p){
    visit[from] = 1;
    num++;
    int cnt=0;
    for(auto to : E[from]){
        if (p == to) continue;
        assert(!visit[to]);
        cnt += dfs(E, to, from);
    }

    if ((cnt + c[from]) % 2 == 0){
        ans++;
        return 1;
    }
    else return 0;
}

int main(){

    int N, a, b;
    cin >> N;
    assert(1<=N && N<=2e5);
    vector<vector<int>> E(N);
    c.resize(N);
    visit.resize(N, 0);

    for (int i=0; i<N-1; i++){
        cin >> a >> b; a--; b--;
        E[a].push_back(b);
        E[b].push_back(a);
        assert(a < b && 0 <= a && a <= N-1 && 0 <= b && b <= N-1);
    }

    for (int i=0; i<N; i++){
        cin >> c[i];
        assert(c[i] == 0 || c[i] == 1);
    }

    if (dfs(E, 0, -1) == 1) cout << -1 << endl;
    else cout << ans << endl;
    
    assert(num == N);

    return 0;
}

0