結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー ぷらぷら
提出日時 2023-02-03 21:43:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 89 ms / 2,000 ms
コード長 874 bytes
コンパイル時間 1,965 ms
コンパイル使用メモリ 204,100 KB
実行使用メモリ 31,952 KB
最終ジャッジ日時 2023-09-15 17:24:38
合計ジャッジ時間 6,170 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 66 ms
17,436 KB
testcase_12 AC 67 ms
17,272 KB
testcase_13 AC 64 ms
17,220 KB
testcase_14 AC 59 ms
17,272 KB
testcase_15 AC 83 ms
31,952 KB
testcase_16 AC 49 ms
11,752 KB
testcase_17 AC 48 ms
11,680 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 74 ms
17,156 KB
testcase_20 AC 82 ms
16,640 KB
testcase_21 AC 82 ms
16,376 KB
testcase_22 AC 66 ms
17,172 KB
testcase_23 AC 76 ms
16,380 KB
testcase_24 AC 74 ms
16,612 KB
testcase_25 AC 82 ms
16,372 KB
testcase_26 AC 89 ms
16,504 KB
testcase_27 AC 73 ms
16,956 KB
testcase_28 AC 64 ms
17,176 KB
testcase_29 AC 68 ms
17,512 KB
testcase_30 AC 89 ms
16,376 KB
testcase_31 AC 72 ms
17,220 KB
testcase_32 AC 76 ms
16,668 KB
testcase_33 AC 76 ms
16,672 KB
testcase_34 AC 73 ms
16,320 KB
testcase_35 AC 87 ms
16,432 KB
testcase_36 AC 87 ms
16,476 KB
testcase_37 AC 74 ms
16,508 KB
testcase_38 AC 89 ms
16,312 KB
testcase_39 AC 72 ms
17,144 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int inf = 1001001001;

int dp[200200][2];

void dfs(int n,int p,vector<int>&c,vector<vector<int>>&ki) {
    dp[n][1-c[n]] = inf;
    for(int i:ki[n]) {
        if(i == p) {
            continue;
        }
        dfs(i,n,c,ki);
        int a = dp[n][0],b = dp[n][1];
        dp[n][0] = min({inf,dp[i][0]+b+1,dp[i][1]+a});
        dp[n][1] = min({inf,dp[i][0]+a+1,dp[i][1]+b});
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N;
    cin >> N;
    vector<vector<int>>ki(N);
    for(int i = 0; i < N-1; i++) {
        int a,b;
        cin >> a >> b;
        a--;
        b--;
        ki[a].push_back(b);
        ki[b].push_back(a);
    }
    vector<int>c(N);
    for(int i = 0; i < N; i++) {
        cin >> c[i];
    }
    dfs(0,-1,c,ki);
    cout << ((dp[0][1] == inf)?-1:dp[0][1]) << endl;
}
0