Submission #396062

#TimeUsernameProblemLanguageResultExecution timeMemory
396062ronnithDžumbus (COCI19_dzumbus)C++14
0 / 110
40 ms7500 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; inline void remin(ll& x, ll y) { x = (y < x ? y : x); } const int N = (int)1e3 + 1, Q = (int)2e5; int n, m, q, d[N]; bool vis[N]; vector<ll> dp0[N]; vector<ll> dp1[N][2]; vector<int> adj[N]; void dfs(int x, int pr) { vis[x] = true; for(auto& e : adj[x]) { if(e != pr) { dfs(e, x); } } } void dfs2(int x, int pr) { dp0[x] = {0, LLONG_MAX}; dp1[x][0] = {d[x], LLONG_MAX}; dp1[x][1] = {LLONG_MAX, LLONG_MAX}; for(auto& e : adj[x]) { if(e != pr) { dfs2(e, x); int a = (int)dp0[x].size(), b = (int)dp0[e].size(); vector<ll> res(a + b - 1, LLONG_MAX); for(int i = 0;i < a;i ++) { for(int j = 0;j < b;j ++) { if(dp1[x][0][i] != LLONG_MAX) { if(dp1[e][0][j] != LLONG_MAX) remin(res[i + j + 2], dp1[x][0][i] + dp1[e][0][j]); if(dp1[e][1][j] != LLONG_MAX) remin(res[i + j + 1], dp1[x][0][i] + dp1[e][1][j]); } if(dp1[x][1][i] != LLONG_MAX) { if(dp1[e][0][j] != LLONG_MAX) remin(res[i + j + 1], dp1[x][1][i] + dp1[e][0][j]); if(dp1[e][1][j] != LLONG_MAX) remin(res[i + j], dp1[x][1][i] + dp1[e][1][j]); if(dp0[e][j] != LLONG_MAX) remin(res[i + j], dp1[x][1][i] + dp0[e][j]); } } } dp1[x][1] = res; res = vector<ll>(a + b - 1, LLONG_MAX); for(int i = 0;i < a;i ++) { for(int j = 0;j < b;j ++) { if(dp1[x][0][i] != LLONG_MAX) { if(dp0[e][j] != LLONG_MAX) remin(res[i + j], dp1[x][0][i] + dp0[e][j]); } } } dp1[x][0] = res; for(int i = 0;i < a;i ++) { for(int j = 0;j < b;j ++) { if(dp0[x][i] != LLONG_MAX) { if(dp1[e][1][j] != LLONG_MAX) remin(res[i + j], dp0[x][i] + dp1[e][1][j]); if(dp1[e][0][j] != LLONG_MAX) remin(res[i + j], dp0[x][i] + dp1[e][0][j]); if(dp0[e][j] != LLONG_MAX) remin(res[i + j], dp0[x][i] + dp0[e][j]); } } } dp0[x] = res; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m; d[0] = 0; for(int i = 1;i <= n;i ++) { cin >> d[i]; } for(int i = 0;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 ++) { if(!vis[i]) { dfs(i, -1); adj[0].push_back(i); adj[i].push_back(0); } } dfs2(0, -1); cin >> q; while(q --) { int s; cin >> s; int lx = 0, rx = n; while(lx < rx) { int g = lx + (rx - lx + 1) / 2; if(dp0[0][g] <= s) { lx = g; } else { rx = g - 1; } } cout << lx << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...