結果
問題 | No.2205 Lights Out on Christmas Tree |
ユーザー |
|
提出日時 | 2023-02-03 23:12:38 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 115 ms / 2,000 ms |
コード長 | 1,535 bytes |
コンパイル時間 | 1,962 ms |
コンパイル使用メモリ | 177,664 KB |
実行使用メモリ | 45,312 KB |
最終ジャッジ日時 | 2024-07-02 21:17:31 |
合計ジャッジ時間 | 6,390 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 37 |
ソースコード
#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 secondconst 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;}