제출 #1124542

#제출 시각아이디문제언어결과실행 시간메모리
1124542doducanhDžumbus (COCI19_dzumbus)C++20
50 / 110
47 ms16964 KiB
///roadtoVOI2025 ///enjoythejourney #include<bits/stdc++.h> using namespace std; #define ll long long #define fi first #define se second #define ii pair<int,int> #define mp make_pair #define in(x) freopen(x,"r",stdin) #define out(x) freopen(x,"w",stdout) #define bit(x,i) ((x>>i)&1) #define lc (id<<1) #define rc ((id<<1)^1) const int maxn=1005; const ll inf=1e18+7; vector<int>adj[maxn]; int a[maxn]; int n,m,q; int sz[maxn]; ll dp[maxn][2][maxn]; ll ndp[2][maxn]; bool vst[maxn]; void work(int u,int v) { for(int i=sz[u];i>=0;i--){ for(int j=sz[v];j>=0;j--){ ndp[0][i+j]=min(ndp[0][i+j],dp[u][0][i]+min(dp[v][0][j],dp[v][1][j])); ndp[1][i+j]=min(ndp[1][i+j],dp[u][1][i]+min(dp[v][0][j],dp[v][1][j])); if(i+1<=sz[u]){ ndp[1][i+j+1]=min(ndp[1][i+j+1],dp[u][0][i]+a[u]+dp[v][1][j]); } if(i+1<=sz[u]&&j+1<=sz[v]){ ndp[1][i+j+2]=min(ndp[1][i+j+2],dp[u][0][i]+a[u]+a[v]+dp[v][0][j]); } if(j+1<=sz[v]){ ndp[1][i+j+1]=min(ndp[1][i+j+1],dp[u][1][i]+a[v]+dp[v][0][j]); } } } sz[u]+=sz[v]; for(int i=sz[u];i>=0;i--){ dp[u][1][i]=min(dp[u][1][i],ndp[1][i]); dp[u][0][i]=min(dp[u][0][i],ndp[0][i]); ndp[1][i]=inf; ndp[0][i]=inf; } } void dfs(int u,int par) { vst[u]=1; sz[u]=1; dp[u][0][0]=0; ndp[0][0]=0; for(int v:adj[u])if(v!=par){ dfs(v,u); work(u,v); } } ll res[maxn]; void sol() { cin>>n>>m; for(int i=1;i<=n;i++){ cin>>a[i]; } for(int i=1;i<=m;i++){ int u,v; cin>>u>>v; adj[u].push_back(v); adj[v].push_back(u); } for(int i=1;i<=n;i++){ res[i]=inf; for(int j=0;j<=n;j++){ dp[i][0][j]=inf; dp[i][1][j]=inf; } } for(int i=0;i<2;i++){ for(int j=0;j<=n;j++){ ndp[i][j]=inf; } } for(int i=1;i<=n;i++){ if(!vst[i]){ dfs(i,0); for(int j=0;j<=n;j++){ res[j]=min(res[j],dp[i][0][j]); res[j]=min(res[j],dp[i][1][j]); } } } for(int i=n-1;i>=0;i--){ res[i]=min(res[i],res[i+1]); } cin>>q; while(q--){ int s; cin>>s; int x=upper_bound(res,res+n+1,s)-res-1; if(res[x]>s){ cout<<0<<"\n"; continue; } cout<<x<<"\n"; } } signed main(void) { ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); int t=1; while(t--){ sol(); } // you should actually read the stuff at the bottom return 0; } /* stuff you should look for * int overflow, array bounds * special cases (n=1?) * do smth instead of nothing and stay organized * WRITE STUFF DOWN * DON'T GET STUCK ON ONE APPROACH */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...