제출 #442028

#제출 시각아이디문제언어결과실행 시간메모리
442028CSQ31Džumbus (COCI19_dzumbus)C++17
0 / 110
41 ms24268 KiB
#include<bits/stdc++.h> using namespace std; #define pb push_back #define fi first #define se second #define sz(a) (int)(a.size()) #define all(a) a.begin(),a.end() #define lb lower_bound #define ub upper_bound #define owo ios_base::sync_with_stdio(0);cin.tie(0); #define INF (ll)(1e18) #define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr) #define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\ debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC)) typedef long long int ll; typedef long double ld; typedef pair<ll,ll> PII; typedef pair<int,int> pii; typedef vector<vector<int>> vii; typedef vector<vector<ll>> VII; ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);} const int MAXN = 2000; vii adj(MAXN); vector<ll>d(MAXN); vector<int>vis(MAXN),comp(MAXN),sub(MAXN); ll dp[2][MAXN][MAXN]; //v not taken,v taken //there is also a singleton state thonk but this state is constant no need extra more stuff void dfs(int v,int u){ sub[v] = 1; dp[0][v][0] = 0; for(int x:adj[v]){ if(x != u){ dfs(x,v); sub[v]+=sub[x]; } } int cur = 1; for(int x:adj[v]){ if(x == u)continue; for(int i=cur;i>=0;i--){ for(int j=0;j<=sub[x];j++){ if(j+i > sub[x]+cur)break; dp[0][v][i+j] = min(dp[0][v][i+j],dp[0][v][i]+min(dp[0][x][j],dp[1][x][j])); dp[1][v][i+j] = min(dp[1][v][i+j],dp[1][v][i]+min(dp[0][x][j],dp[1][x][j])); } //account for singletons if(i>1)dp[1][v][i+1] = min(dp[1][v][i+1],dp[1][v][i] + d[x]); } dp[1][v][2] = min(dp[1][v][2],d[v]+d[x]); cur+=sub[x]; } } int main() { owo int n,m; cin>>n>>m; for(int i=1;i<=n;i++)cin>>d[i]; for(int i=0;i<m;i++){ int v,u; cin>>v>>u; adj[v].pb(u); adj[u].pb(v); } for(int i=0;i<=n;i++){ for(int j=0;j<=n;j++){ dp[0][i][j] = dp[1][i][j] = INF; } } dfs(1,0); //for(int i=0;i<=n;i++)cout<<dp[0][1][i]<<" "<<dp[1][1][i]<<'\n'; vector<PII>a; for(int i=0;i<=n;i++)a.pb({min(dp[0][1][i],dp[1][1][i]),i}); sort(all(a)); vector<int>mx(n+1,0); for(int i=0;i<=n;i++){ mx[i] = a[i].se; if(i)mx[i] = max(mx[i],mx[i-1]); } int q; cin>>q; while(q--){ ll s; cin>>s; int p = ub(all(a),make_pair(s*1LL,INF)) - a.begin(); cout<<mx[p-1]<<'\n'; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...