結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー roarisroaris
提出日時 2023-02-03 23:04:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 92 ms / 2,000 ms
コード長 978 bytes
コンパイル時間 2,882 ms
コンパイル使用メモリ 202,012 KB
実行使用メモリ 35,456 KB
最終ジャッジ日時 2023-09-15 19:18:12
合計ジャッジ時間 6,172 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
8,384 KB
testcase_01 AC 3 ms
8,392 KB
testcase_02 AC 4 ms
8,368 KB
testcase_03 AC 3 ms
8,376 KB
testcase_04 AC 4 ms
8,364 KB
testcase_05 AC 3 ms
8,380 KB
testcase_06 AC 4 ms
8,500 KB
testcase_07 AC 4 ms
8,436 KB
testcase_08 AC 3 ms
8,396 KB
testcase_09 AC 3 ms
8,380 KB
testcase_10 AC 3 ms
8,504 KB
testcase_11 AC 69 ms
17,092 KB
testcase_12 AC 69 ms
16,964 KB
testcase_13 AC 68 ms
17,044 KB
testcase_14 AC 62 ms
17,012 KB
testcase_15 AC 89 ms
35,456 KB
testcase_16 AC 52 ms
13,988 KB
testcase_17 AC 51 ms
13,976 KB
testcase_18 AC 4 ms
8,372 KB
testcase_19 AC 76 ms
17,548 KB
testcase_20 AC 81 ms
16,700 KB
testcase_21 AC 81 ms
16,672 KB
testcase_22 AC 68 ms
17,680 KB
testcase_23 AC 76 ms
16,676 KB
testcase_24 AC 76 ms
16,932 KB
testcase_25 AC 80 ms
16,900 KB
testcase_26 AC 91 ms
16,700 KB
testcase_27 AC 76 ms
17,512 KB
testcase_28 AC 67 ms
17,708 KB
testcase_29 AC 69 ms
17,808 KB
testcase_30 AC 83 ms
16,724 KB
testcase_31 AC 72 ms
17,432 KB
testcase_32 AC 76 ms
17,044 KB
testcase_33 AC 76 ms
17,028 KB
testcase_34 AC 74 ms
16,676 KB
testcase_35 AC 82 ms
16,732 KB
testcase_36 AC 80 ms
16,788 KB
testcase_37 AC 76 ms
16,788 KB
testcase_38 AC 92 ms
16,736 KB
testcase_39 AC 75 ms
17,260 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i=0; i<n; i++)
#define pb push_back
typedef long long ll;

int N;
vector<int> G[200010];
int c[200010];
int memo[2][200010];

int dfs(int f, int v, int pv=-1) {
    if (memo[f][v]!=-1) return memo[f][v];
    int dp0 = 0;
    int dp1 = 1e9;
    for (int nv : G[v]) {
        if (nv==pv) continue;
        int c0 = dfs(0, nv, v);
        int c1 = dfs(1, nv, v);
        int ndp0 = min(dp0+c1, dp1+c0+1);
        int ndp1 = min(dp0+c0+1, dp1+c1);
        dp0 = ndp0;
        dp1 = ndp1;
    }
    int res = c[v]^f ? dp1 : dp0;
    return memo[f][v] = res;
}

int main() {
    cin.tie(0); ios::sync_with_stdio(false);
    
    cin >> N;
    rep(i, N-1) {
        int u, v; cin >> u >> v;
        G[u-1].pb(v-1);
        G[v-1].pb(u-1);
    }
    rep(i, N) cin >> c[i];
    
    rep(f, 2) rep(v, N) memo[f][v] = -1;
    int ans = dfs(1, 0);
    if (ans>=1e9) cout << -1 << endl;
    else cout << ans << endl;
}
0