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;
#define int long long
int N;
const int mxn = 1e5+10;
int dp[mxn][2][2];
vector<int> tree[mxn];
int arr[mxn];
void dfs(int now,int par){
for(int i = 0;i<2;i++)for(int j = 0;j<2;j++)dp[now][i][j] = mxn;
vector<int> v0,v1;
int s0 = 0,s1 = 0;
for(auto nxt:tree[now]){
if(nxt == par)continue;
dfs(nxt,now);
s0 += dp[nxt][0][0];
s1 += dp[nxt][1][0];
v0.push_back(dp[nxt][0][1]-dp[nxt][0][0]);
v1.push_back(dp[nxt][1][1]-dp[nxt][1][0]);
}
sort(v0.begin(),v0.end());
sort(v1.begin(),v1.end());
int t = arr[now];
int ts = s0;
dp[now][t][0] = ts;
for(int i = 1;i<v0.size();i+=2){
ts += v0[i-1]+v0[i];
dp[now][t][0] = min(dp[now][t][0],ts);
}
ts = s1;
for(int i = 0;i<v1.size();i+=2){
if(i)ts += v1[i-1]+v1[i];
else ts += v1[i];
dp[now][t][1] = min(dp[now][t][1],ts+1);
}
t ^= 1;
ts = s0;
for(int i = 0;i<v0.size();i+=2){
if(i)ts += v0[i-1]+v0[i];
else ts += v0[i];
dp[now][t][0] = min(dp[now][t][0],ts);
}
ts = s1;
dp[now][t][1] = ts+1;
for(int i = 1;i<v1.size();i+=2){
ts += v1[i]+v1[i-1];
dp[now][t][1] = min(dp[now][t][1],ts+1);
}
return;
}
main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>N;
for(int i = 1;i<N;i++){
int a,b;
cin>>a>>b;
tree[a].push_back(b);
tree[b].push_back(a);
}
for(int i = 1;i<=N;i++)cin>>arr[i];
dfs(1,1);
int ans = min(dp[1][0][0],dp[1][0][1]);
if(ans>N)cout<<"impossible\n";
else cout<<ans<<'\n';
return 0;
}
Compilation message (stderr)
xanadu.cpp: In function 'void dfs(long long int, long long int)':
xanadu.cpp:30:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for(int i = 1;i<v0.size();i+=2){
| ~^~~~~~~~~~
xanadu.cpp:35:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for(int i = 0;i<v1.size();i+=2){
| ~^~~~~~~~~~
xanadu.cpp:44:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for(int i = 0;i<v0.size();i+=2){
| ~^~~~~~~~~~
xanadu.cpp:51:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for(int i = 1;i<v1.size();i+=2){
| ~^~~~~~~~~~
xanadu.cpp: At global scope:
xanadu.cpp:58:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
58 | main(){
| ^~~~
# | 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... |