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;
#ifdef DEBUG
#define dbg(...) printf(__VA_ARGS__);
#define getchar_unlocked getchar
#else
#define dbg(...)
#endif
#define sf scanf
#define pf printf
#define fi first
#define se second
#define pb push_back
#define psf push_front
#define ppb pop_back
#define ppf pop_front
#define sz(x) (int)x.size()
#define mnto(x,y) x=min(x,(__typeof__(x))y)
#define mxto(x,y) x=max(x,(__typeof__(x))y)
#define INF 1023456789
#define LINF 1023456789123456789
#define all(x) x.begin(),x.end()
#define lb(x,v) (lower_bound(all(x),v)-x.begin())
#define ub(x,v) (upper_bound(all(x),v)-x.begin())
#define disc(x) sort(all(x));x.resize(unique(all(x))-x.begin());
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;
typedef tuple<int,int,int> iii;
typedef tuple<int,int,int,int> iiii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
mt19937 rng(time(0));
#define mod 1000000007
inline int add(int a,int b){
int r=a+b;
while(r>=mod)r-=mod;
while(r<0)r+=mod;
return r;
}
inline int mult(int a,int b){
return (int)(((ll)(a*b))%mod);
}
inline int rd(){
int x=0;
char ch=getchar_unlocked();
while(!(ch&16))ch=getchar();//keep reading while current character is whitespace
while(ch&16){//this will break when ‘\n’ or ‘ ‘ is encountered
x=(x<<3)+(x<<1)+(ch&15);
ch=getchar_unlocked();
}
return x;
}
#define maxn 100005
int n,a[maxn],dp[maxn][2][2];
vi AL[maxn];
int dfs(int u,int target,int toggle,int par){
if(dp[u][target][toggle]!=-1)return dp[u][target][toggle];
int tot=0,add=INF,pari=0;
for(int v:AL[u]){
if(v==par)continue;
int x=dfs(v,1-toggle,0,u),y=dfs(v,1-toggle,1,u);
tot+=min(x,y);
pari+=(x==min(x,y))?0:1;
add=min(add,abs(x-y));
}
if((a[u]+pari+toggle)%2==target)tot+=add;
pf("dfs %d %d %d: %d\n",u,target,toggle,tot+toggle);
return dp[u][target][toggle]=tot+toggle;
}
int main(){
sf("%d",&n);
for(int i=0;i<n-1;++i){
int x,y;sf("%d%d",&x,&y);
AL[x].pb(y),AL[y].pb(x);
}
for(int i=1;i<=n;++i)sf("%d",&a[i]);
memset(dp,-1,sizeof dp);
int ans=min(dfs(1,1,0,-1),dfs(1,1,1,-1));
if(ans>=INF)pf("impossible\n");
else pf("%d\n",ans);
}
Compilation message (stderr)
xanadu.cpp: In function 'int main()':
xanadu.cpp:83:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
83 | sf("%d",&n);
| ^
xanadu.cpp:85:13: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
85 | int x,y;sf("%d%d",&x,&y);
| ^
xanadu.cpp:88:25: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
88 | for(int i=1;i<=n;++i)sf("%d",&a[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... |