# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1097666 | vjudge1 | The Xana coup (BOI21_xanadu) | C++17 | 50 ms | 23388 KiB |
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<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#define int long long
#define ll long long
using namespace std;
const int N=1e5+100;
int f[N][2][2];
vector<int> g[N];
int n;
int a[N];
int dp[N][2],gg[N][2];
void dfs(int x,int fa){
if(g[x].size()==1&&x!=1){
if(a[x]==0){
f[x][0][0]=0;
f[x][1][1]=1;
}else{
f[x][0][1]=0;
f[x][1][0]=1;
}
return ;
}
int siz=0;
for(int i:g[x]){
if(i==fa)continue;
++siz;
dfs(i,x);
}
int cnt=0;
for(int i=1;i<=siz;i++){
dp[i][0]=dp[i][1]=0x3f3f3f3f;
gg[i][0]=gg[i][1]=0x3f3f3f3f;
}
dp[0][1]=gg[0][1]=0x3f3f3f3f;
for(int i:g[x]){
if(i==fa)continue;
++cnt;
dp[cnt][0]=min((ll)dp[cnt-1][0]+f[i][0][0],(ll)dp[cnt-1][1]+f[i][1][0]);
dp[cnt][1]=min((ll)dp[cnt-1][1]+f[i][0][0],(ll)dp[cnt-1][0]+f[i][1][0]);
gg[cnt][0]=min((ll)gg[cnt-1][0]+f[i][0][1],(ll)gg[cnt-1][1]+f[i][1][1]);
gg[cnt][1]=min((ll)gg[cnt-1][1]+f[i][0][1],(ll)gg[cnt-1][0]+f[i][1][1]);
}
if(a[x]==0){
f[x][0][1]=dp[cnt][1];
f[x][0][0]=dp[cnt][0];
f[x][1][1]=gg[cnt][0]+1;
f[x][1][0]=gg[cnt][1]+1;
}else{
f[x][0][1]=dp[cnt][0];
f[x][0][0]=dp[cnt][1];
f[x][1][0]=gg[cnt][0]+1;
f[x][1][1]=gg[cnt][1]+1;
}
}
signed main(){
cin>>n;
for(int i=1;i<=n;i++)f[i][0][0]=f[i][1][0]=f[i][1][1]=f[i][0][1]=0x3f3f3f3f;
for(int i=1;i<n;i++){
int u,v;
scanf("%lld%lld",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
}
dfs(1,0);
int ans=min(f[1][1][0],f[1][0][0]);
if(ans>=0x3f3f3f3f){
puts("impossible");
}else{
cout<<ans<<endl;
}
}
Compilation message (stderr)
# | 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... |