結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー umimelumimel
提出日時 2023-02-03 23:12:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 114 ms / 2,000 ms
コード長 1,535 bytes
コンパイル時間 1,925 ms
コンパイル使用メモリ 176,664 KB
実行使用メモリ 45,224 KB
最終ジャッジ日時 2023-09-15 19:27:08
合計ジャッジ時間 6,362 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 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,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 88 ms
28,016 KB
testcase_12 AC 89 ms
27,916 KB
testcase_13 AC 87 ms
27,944 KB
testcase_14 AC 83 ms
27,992 KB
testcase_15 AC 106 ms
45,224 KB
testcase_16 AC 64 ms
19,360 KB
testcase_17 AC 64 ms
19,356 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 96 ms
28,316 KB
testcase_20 AC 102 ms
27,660 KB
testcase_21 AC 102 ms
27,664 KB
testcase_22 AC 89 ms
28,348 KB
testcase_23 AC 97 ms
27,668 KB
testcase_24 AC 95 ms
27,680 KB
testcase_25 AC 105 ms
27,168 KB
testcase_26 AC 114 ms
26,668 KB
testcase_27 AC 97 ms
28,648 KB
testcase_28 AC 86 ms
28,428 KB
testcase_29 AC 90 ms
28,796 KB
testcase_30 AC 113 ms
26,600 KB
testcase_31 AC 94 ms
28,292 KB
testcase_32 AC 100 ms
27,312 KB
testcase_33 AC 100 ms
27,316 KB
testcase_34 AC 93 ms
27,704 KB
testcase_35 AC 107 ms
26,976 KB
testcase_36 AC 109 ms
26,872 KB
testcase_37 AC 95 ms
27,668 KB
testcase_38 AC 111 ms
26,660 KB
testcase_39 AC 92 ms
27,952 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
#define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i)
#define rep(i, n) drep(i, 0, n - 1)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define fi first
#define se second

const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ll INF = 1LL << 60;
const ll MAX_N = 2e5;

vector<vector<ll>> T;
vector<ll> c;
vector<vector<ll>> dp;
void dfs(ll u, ll p){
    ll x0=0, x1=0;
    for(ll v : T[u]){
        if(v == p) continue;
        dfs(v, u);
        if(dp[v][0]==INF) x1++;
        if(dp[v][1]==INF) x0++;
    }

    if(x0%2==0){
        dp[u][c[u]] = 0;
        dp[u][1-c[u]] = INF;
        for(ll v : T[u]){
            if(v == p) continue;
            if(dp[v][0]!=INF) dp[u][c[u]] += dp[v][0]+1;
            else dp[u][c[u]] += dp[v][1];
        }
    }else{
        dp[u][c[u]] = INF;
        dp[u][1-c[u]] = 0;
        for(ll v : T[u]){
            if(v == p) continue;
            if(dp[v][0]!=INF) dp[u][1-c[u]] += dp[v][0]+1;
            else dp[u][1-c[u]] += dp[v][1];
        }
    }

    return;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    
    ll n;
    cin >> n;

    T.resize(n);
    rep(i, n-1){
        ll a, b;
        cin >> a >> b;
        a--; b--;
        T[a].pb(b);
        T[b].pb(a);
    }

    c.resize(n);
    rep(i, n) cin >> c[i];

    dp.resize(n, vector<ll>(2, 0));
    dfs(0, -1);

    if(dp[0][1]==INF) cout << -1 << endl;
    else cout << dp[0][1] << endl;
}
0