結果

問題 No.2205 Lights Out on Christmas Tree
ユーザー umimelumimel
提出日時 2023-02-03 23:11:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,595 bytes
コンパイル時間 2,035 ms
コンパイル使用メモリ 175,368 KB
実行使用メモリ 45,268 KB
最終ジャッジ日時 2023-09-15 19:25:30
合計ジャッジ時間 14,661 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
権限があれば一括ダウンロードができます

ソースコード

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);

    rep(i, n) cout << dp[i][0] << " " << dp[i][1] << endl;

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