Submission #855910

#TimeUsernameProblemLanguageResultExecution timeMemory
855910wakandaaaDžumbus (COCI19_dzumbus)C++17
110 / 110
164 ms19216 KiB
#include <bits/stdc++.h> #define fi first #define se second #define all(x) x.begin(), x.end() #define getbit(x, i) (((x) >> (i)) & 1) #define bit(x) (1 << (x)) #define file "" using namespace std; mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count()); long long rand(long long l, long long r) { return l + rd() % (r - l + 1); } const int N = 1e3 + 5; const int mod = 1e9 + 7; // 998244353; const int inf = INT_MAX; const long long llinf = LLONG_MAX; const int lg = 25; // lg + 1 template<class X, class Y> bool minimize(X &a, Y b) { return a > b ? (a = b, true) : false; } template<class X, class Y> bool maximize(X &a, Y b) { return a < b ? (a = b, true) : false; } template<class X, class Y> bool add(X &a, Y b) { a += b; while (a >= mod) a -= mod; while (a < 0) a += mod; return true; } int n, m; vector<int> adj[N]; int d[N]; long long f[N][N][2]; int sz[N]; int vis[N]; void dfs(int u, int p) { vis[u] = true; f[u][0][0] = 0; if (u != 0) sz[u] = 1; for (int v : adj[u]) if (v != p && !vis[v]) { dfs(v, u); for (int i = sz[u]; i >= 0; --i) for (int j = sz[v]; j >= 0; --j) { minimize(f[u][i + j][0], f[u][i][0] + min(f[v][j][0], f[v][j][1])); minimize(f[u][i + j][1], f[u][i][1] + min(f[v][j][0], f[v][j][1])); minimize(f[u][i + j + 1][1], d[u] + f[u][i][0] + f[v][j][1]); minimize(f[u][i + j + 1][1], f[u][i][1] + d[v] + f[v][j][0]); minimize(f[u][i + j + 2][1], f[u][i][0] + d[u] + f[v][j][0] + d[v]); } sz[u] += sz[v]; } } int main() { ios::sync_with_stdio(false); cin.tie(0); // freopen(file".inp", "r",stdin); // freopen(file".out", "w",stdout); cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> d[i]; for (int i = 1; i <= m; ++i) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } memset(f, 0x3f, sizeof f); f[0][0][0] = 0; for (int i = 1; i <= n; ++i) { if (!vis[i]) { adj[0].push_back(i); dfs(0, -1); } } int q; cin >> q; while (q--) { int s; cin >> s; int x = n; for (int i = n; i >= 0; --i) { if (f[0][i][0] <= s) { x = i; break; } } cout << x << '\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...