結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー t98slidert98slider
提出日時 2023-02-03 22:02:01
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 114 ms / 2,000 ms
コード長 1,087 bytes
コンパイル時間 1,561 ms
コンパイル使用メモリ 172,952 KB
実行使用メモリ 41,448 KB
最終ジャッジ日時 2023-09-15 17:51:45
合計ジャッジ時間 6,094 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 84 ms
27,212 KB
testcase_12 AC 85 ms
27,032 KB
testcase_13 AC 63 ms
15,840 KB
testcase_14 AC 78 ms
26,792 KB
testcase_15 AC 104 ms
41,448 KB
testcase_16 AC 63 ms
18,052 KB
testcase_17 AC 62 ms
18,204 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 93 ms
26,696 KB
testcase_20 AC 70 ms
15,176 KB
testcase_21 AC 107 ms
26,640 KB
testcase_22 AC 84 ms
27,076 KB
testcase_23 AC 95 ms
26,120 KB
testcase_24 AC 94 ms
26,140 KB
testcase_25 AC 106 ms
26,244 KB
testcase_26 AC 113 ms
26,364 KB
testcase_27 AC 94 ms
26,836 KB
testcase_28 AC 85 ms
27,308 KB
testcase_29 AC 86 ms
27,372 KB
testcase_30 AC 114 ms
25,880 KB
testcase_31 AC 92 ms
26,824 KB
testcase_32 AC 102 ms
26,824 KB
testcase_33 AC 101 ms
26,152 KB
testcase_34 AC 93 ms
25,848 KB
testcase_35 AC 110 ms
25,816 KB
testcase_36 AC 108 ms
26,444 KB
testcase_37 AC 94 ms
26,036 KB
testcase_38 AC 73 ms
15,436 KB
testcase_39 AC 71 ms
15,440 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