結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー t98slidert98slider
提出日時 2023-02-03 22:02:01
言語 C++14
(gcc 11.2.0 + boost 1.78.0)
結果
AC  
実行時間 105 ms / 2,000 ms
コード長 1,087 bytes
コンパイル時間 1,489 ms
使用メモリ 41,476 KB
最終ジャッジ日時 2023-02-03 22:02:12
合計ジャッジ時間 6,030 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 1 ms
4,904 KB
testcase_01 AC 1 ms
4,904 KB
testcase_02 AC 1 ms
4,900 KB
testcase_03 AC 1 ms
4,904 KB
testcase_04 AC 1 ms
4,904 KB
testcase_05 AC 1 ms
4,904 KB
testcase_06 AC 2 ms
4,900 KB
testcase_07 AC 2 ms
4,900 KB
testcase_08 AC 2 ms
4,908 KB
testcase_09 AC 1 ms
6,948 KB
testcase_10 AC 2 ms
6,948 KB
testcase_11 AC 82 ms
27,304 KB
testcase_12 AC 76 ms
27,256 KB
testcase_13 AC 56 ms
15,968 KB
testcase_14 AC 74 ms
26,588 KB
testcase_15 AC 92 ms
41,476 KB
testcase_16 AC 58 ms
18,084 KB
testcase_17 AC 57 ms
18,004 KB
testcase_18 AC 1 ms
4,900 KB
testcase_19 AC 85 ms
26,740 KB
testcase_20 AC 63 ms
15,168 KB
testcase_21 AC 102 ms
26,488 KB
testcase_22 AC 76 ms
27,212 KB
testcase_23 AC 86 ms
26,052 KB
testcase_24 AC 85 ms
25,824 KB
testcase_25 AC 98 ms
26,232 KB
testcase_26 AC 104 ms
26,292 KB
testcase_27 AC 83 ms
26,716 KB
testcase_28 AC 74 ms
27,504 KB
testcase_29 AC 81 ms
27,416 KB
testcase_30 AC 105 ms
25,740 KB
testcase_31 AC 83 ms
26,760 KB
testcase_32 AC 92 ms
26,824 KB
testcase_33 AC 90 ms
26,148 KB
testcase_34 AC 83 ms
25,740 KB
testcase_35 AC 102 ms
25,744 KB
testcase_36 AC 101 ms
26,372 KB
testcase_37 AC 84 ms
26,104 KB
testcase_38 AC 66 ms
15,348 KB
testcase_39 AC 64 ms
15,436 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, u, v;
    cin >> n;
    vector<vector<int>> g(n);
    vector<int> c(n), wv;
    for(int i = 1; i < n; i++){
        cin >> u >> v;
        g[--u].push_back(--v);
        g[v].push_back(u);
    }
    for(int i = 0; i < n; i++){
        cin >> c[i];
        if(c[i] == 0)wv.push_back(i);
    }
    if(wv.size() & 1){
        cout << -1 << '\n';
        return 0;
    }
    vector<vector<int>> dp(n, vector<int>(2, 1 << 29));
    function<void(int,int)> dfs = [&](int v, int p){
        vector<int>& temp = dp[v];
        temp[c[v] ^ 1] = 0;
        for(auto &&u:g[v]){
            if(u == p)continue;
            dfs(u, v);
            vector<int> nxt(2, 1 << 30);
            for(int i = 0; i < 2; i++){
                for(int j = 0; j < 2; j++){
                    nxt[i ^ j] = min(nxt[i ^ j], temp[i] + dp[u][j] + j);
                }
            }
            swap(temp, nxt);
        }
    };
    dfs(0, -1);
    cout << dp[0][0] << '\n';
}
0