Submission #396084

#TimeUsernameProblemLanguageResultExecution timeMemory
396084ronnithDžumbus (COCI19_dzumbus)C++14
110 / 110
74 ms13652 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], ans[Q]; 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; res = vector<ll>(a + b - 1, LLONG_MAX); 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; vector<pair<int, int>> queries; for(int i = 0;i < q;i ++) { int s; cin >> s; queries.emplace_back(s, i); } sort(queries.begin(), queries.end()); vector<pair<ll, int>> values; for(int i = 0;i <= n;i ++) { values.emplace_back(dp0[0][i], i); } sort(values.begin(), values.end()); int ptr = 0; int mx = 0; for(auto& e : queries) { while(ptr <= n && values[ptr].first <= e.first) { mx = max(mx, values[ptr].second); ptr ++; } ans[e.second] = mx; } for(int i = 0;i < q;i ++) { cout << ans[i] << '\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...