Submission #204187

#TimeUsernameProblemLanguageResultExecution timeMemory
204187alishahali1382Džumbus (COCI19_dzumbus)C++14
110 / 110
128 ms109816 KiB
#include <bits/stdc++.h> #pragma GCC optimize ("O2") #pragma GCC optimize ("unroll-loops") //#pragma GCC optimize("no-stack-protector,fast-math") using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<pii, int> piii; typedef pair<ll, ll> pll; #define debug(x) cerr<<#x<<'='<<(x)<<endl; #define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl; #define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl; #define debugv(v) {cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;} #define all(x) x.begin(), x.end() #define pb push_back #define upd(x, y) x=min(x, y) const ld eps=1e-7; const int inf=1000000010; const ll INF=10000000000000010LL; const int mod = 1000000007; const int MAXN = 3010, LOG=20; int n, m, k, u, v, x, y, t, a, b; int A[MAXN]; int dp[MAXN][MAXN][3]; int dpp[MAXN][3]; int ans[MAXN], anss[MAXN]; int sz[MAXN]; vector<int> G[MAXN]; int f(int i, int j){ if (i==1) return 1+(j>0); return i; } void dfs(int node, int par){ sz[node]=1; dp[node][0][0]=0; dp[node][0][1]=A[node]; for (int v:G[node]) if (v!=par){ dfs(v, node); for (int i=0; i<=sz[node]; i++) for (int ii:{0, 1, 2}) dpp[i][ii]=dp[node][i][ii], dp[node][i][ii]=inf; for (int i=0; i<=sz[node]; i++) for (int ii:{0, 1, 2}) for (int j=0; j<=sz[v]; j++) for (int jj:{0, 1, 2}) upd(dp[node][i+j+(ii==1 && jj>0)+(ii>0 && jj==1)][f(ii, jj)], dpp[i][ii] + dp[v][j][jj]); sz[node]+=sz[v]; } } int main(){ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); memset(dp, 63, sizeof(dp)); memset(ans, 63, sizeof(ans)); cin>>n>>m; for (int i=1; i<=n; i++) cin>>A[i]; while (m--){ cin>>u>>v; G[u].pb(v); G[v].pb(u); } int sum=0; ans[0]=0; for (int v=1; v<=n; v++) if (!sz[v]){ dfs(v, v); for (int i=0; i<=sum; i++) anss[i]=ans[i], ans[i]=inf; for (int j=0; j<=sz[v]; j++){ int mn=min({dp[v][j][0], dp[v][j][1], dp[v][j][2]}); for (int i=0; i<=sum; i++) upd(ans[i+j], anss[i] + mn); } sum+=sz[v]; } for (int i=n; i; i--) upd(ans[i-1], ans[i]); cin>>m; while (m--){ cin>>x; int res=upper_bound(ans, ans+n+1, x)-ans-1; cout<<res<<'\n'; } return 0; } /* 3 2 1 2 3 1 2 1 3 3 2 3 5 */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...