This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 100100;
ll dp[N][2][2],col[N];
vector<int> g[N];
ll f(int v,int p,int c,int flp){
if(dp[v][c][flp] != -1) return dp[v][c][flp];
vector<pair<ll,ll>> a;
for(int u: g[v]) if(u != p){
a.emplace_back(f(u,v,col[u]^flp,0),f(u,v,col[u]^flp^1,1)+1);
}
vector<ll> ndp(2,1e9);
ndp[0] = 0;
for(int i = 0; i < a.size(); i++){
vector<ll> mdp(2,1e9);
mdp[0] = min(ndp[0] + a[i].first,ndp[1] + a[i].second);
mdp[1] = min(ndp[1] + a[i].first,ndp[0] + a[i].second);
ndp = mdp;
}
return dp[v][c][flp] = ndp[c^1];
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for(int i = 0; i < n - 1; i++){
int u,v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for(int i = 1; i <= n; i++){
cin >> col[i];
col[i] ^= 1;
}
memset(dp,-1,sizeof(dp));
ll res = min(f(1,1,col[1],0),f(1,1,col[1]^1,1)+1);
if(res > n) cout << "impossible";
else cout << res << "\n";
}
Compilation message (stderr)
xanadu.cpp: In function 'll f(int, int, int, int)':
xanadu.cpp:17:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | for(int i = 0; i < a.size(); i++){
| ~~^~~~~~~~~~
# | 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... |