This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define pb push_back
const int N = 1e5 + 5;
int n, dp[N][2][2], a[N];
vector<int> g[N];
void dfs(int v, int p){
bool ok = 1;
vector<int> c;
for(int u: g[v]){
if(u == p) continue;
dfs(u, v);
ok = 0;
c.pb(u);
}
int s = c.size();
for(int mask = 0; mask < (1<<s); ++mask){
int k = __builtin_popcount(mask);
k = k % 2;
int x = 0, y = 0;
for(int j = 0; j < s; ++j){
int child = c[j];
if((1<<j)&mask){
x += dp[child][1][0];
y += dp[child][1][1];
}else{
x += dp[child][0][0];
y += dp[child][0][1];
}
}
dp[v][0][a[v]^k] = min(dp[v][0][a[v]^k], x);
dp[v][1][a[v]^k^1] = min(dp[v][1][a[v]^k^1], y + 1);
}
}
void solve(){
cin >> n;
for(int i = 0; i < n - 1; ++i){
int u, v; cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
for(int i = 1; i <= n; ++i) cin >> a[i];
for(int i = 1; i <= n; ++i) dp[i][0][0] = dp[i][0][1] = dp[i][1][0] = dp[i][1][1] = N;
dfs(1, 0);
if(min(dp[1][0][0], dp[1][1][0]) >= N){
cout << "impossible";
}else{
cout << min(dp[1][0][0], dp[1][1][0]);
}
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
solve();
return 0;
}
Compilation message (stderr)
xanadu.cpp: In function 'void dfs(int, int)':
xanadu.cpp:12:10: warning: variable 'ok' set but not used [-Wunused-but-set-variable]
12 | bool ok = 1;
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |