Submission #705372

#TimeUsernameProblemLanguageResultExecution timeMemory
705372NeroZeinDžumbus (COCI19_dzumbus)C++17
50 / 110
191 ms25052 KiB
#include <bits/stdc++.h> #define int long long #define prev pre using namespace std; const int N = 1003; int n; int a[N]; bool vis[N]; vector<int> g[N]; int sz[N]; int dp[N][N][3]; int prev[N][3]; inline void relax (int& x, int y) { x = min(x, y); } void dfs (int v, int p = 0) {//node, taken people, take my self dp[v][0][0] = 0; dp[v][0][1] = a[v]; sz[v] = 1; vis[v] = true; for (int u : g[v]) { if (!vis[u]) { dfs(u, v); } } for (int i = 0; i <= n; ++i) { dp[v][i][0] = dp[v][i][1] = dp[v][i][2] = 1e9; } dp[v][0][0] = 0; dp[v][0][1] = a[v]; for (int u : g[v]) { if (u == p) { continue; } for (int i = 0; i <= n; ++i) { prev[i][0] = dp[v][i][0]; prev[i][1] = dp[v][i][1]; prev[i][2] = dp[v][i][2]; } for (int i = 0; i <= sz[v]; ++i) { for (int j = 0; j <= sz[u]; ++j) { relax(dp[v][i + j][0], min({dp[u][j][1], dp[u][j][0], dp[u][j][2]}) + prev[i][0]); relax(dp[v][i + j][1], min(prev[i][0] + a[v], prev[i][1]) + dp[u][j][0]); relax(dp[v][i + j][2], prev[i][2] + min({dp[u][j][2], dp[u][j][0]})); relax(dp[v][i + j + 1][2], prev[i][2] + dp[u][j][1]); relax(dp[v][i + j + 1][2], min(prev[i][0] + a[v], prev[i][1]) + dp[u][j][2]); relax(dp[v][i + j + 2][2], min(prev[i][0] + a[v], prev[i][1]) + dp[u][j][1]); } } sz[v] += sz[u]; } } signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); int m; cin >> n >> m; for (int i = 1; i <= n; ++i) { cin >> a[i]; } for (int i = 0; i < m; ++i) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } for (int i = 1; i <= n; ++i) { if (!vis[i]) { dfs(i); } } int q; cin >> q; while (q--) { int s; cin >> s; for (int i = n; i >= 0; --i) { if (dp[1][i][0] <= s || dp[1][i][1] <= s || dp[1][i][2] <= s) { cout << i << '\n'; break; } } } 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...