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;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define N 100005
long long st[N],dp[2][2][N],ans;
// good/bad not pass/pass
vector<int> gr[N];
void upd(long long &x,long long &v0,long long &v1,long long &s,long long &m){
x+=min(v0,v1);
s^=v0<=v1?0:1;
m=min(m,abs(v0-v1));
}
void dfs(int s,int f){
long long b=0,g=0,sb=0,sg=0,mb=1e9,mg=1e9;
for(auto x:gr[s]){
if(x==f)continue;
dfs(x,s);
upd(b,dp[1][0][x],dp[1][1][x],sb,mb);
upd(g,dp[0][0][x],dp[0][1][x],sg,mg);
}
dp[sg^st[s]][0][s]=min(g,dp[sg^st[s]][0][s]);
dp[sg^st[s]^1][0][s]=min(g+mg,dp[sg^st[s]^1][0][s]);
dp[sb^st[s]^1][1][s]=min(b+1,dp[sb^st[s]^1][1][s]);
dp[sb^st[s]][1][s]=min(b+mb+1,dp[sb^st[s]][1][s]);
}
int main(){
int n,i,j,k,a,b;
scanf("%d",&n);
for(i=1;i<n;i++){
scanf("%d %d",&a,&b);
gr[a].push_back(b);
gr[b].push_back(a);
}
for(i=0;i<2;i++)for(j=0;j<2;j++)for(k=1;k<=n;k++)dp[i][j][k]=1e9;
for(i=1;i<=n;i++)scanf("%d",&st[i]);
dfs(1,0);
if(min(dp[0][0][1],dp[0][1][1])<1e9)printf("%d",min(dp[0][0][1],dp[0][1][1]));
else printf("impossible");
return 0;
}
Compilation message (stderr)
xanadu.cpp: In function 'int main()':
xanadu.cpp:40:30: warning: format '%d' expects argument of type 'int*', but argument 2 has type 'long long int*' [-Wformat=]
40 | for(i=1;i<=n;i++)scanf("%d",&st[i]);
| ~^ ~~~~~~
| | |
| | long long int*
| int*
| %lld
xanadu.cpp:42:50: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long long int' [-Wformat=]
42 | if(min(dp[0][0][1],dp[0][1][1])<1e9)printf("%d",min(dp[0][0][1],dp[0][1][1]));
| ~^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| | |
| int long long int
| %lld
xanadu.cpp:33:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
xanadu.cpp:35:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | scanf("%d %d",&a,&b);
| ~~~~~^~~~~~~~~~~~~~~
xanadu.cpp:40:27: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | for(i=1;i<=n;i++)scanf("%d",&st[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... |