Submission #1097666

#TimeUsernameProblemLanguageResultExecution timeMemory
1097666vjudge1The Xana coup (BOI21_xanadu)C++17
100 / 100
50 ms23388 KiB
#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)

xanadu.cpp: In function 'int main()':
xanadu.cpp:62:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   62 |   scanf("%lld%lld",&u,&v);
      |   ~~~~~^~~~~~~~~~~~~~~~~~
xanadu.cpp:67:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   67 |   scanf("%lld",&a[i]);
      |   ~~~~~^~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...