Submission #785587

#TimeUsernameProblemLanguageResultExecution timeMemory
785587quochuy147Džumbus (COCI19_dzumbus)C++14
50 / 110
43 ms19012 KiB
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define ll long long #define mp make_pair #define pb push_back #define pii pair<int, int> #define pll pair<ll, ll> #define MASK(i) (1LL << (i)) #define BIT(x, i) (((x) >> (i)) & 1) #define TIME (1.0 * clock() / CLOCKS_PER_SEC) #define file "name" template <typename T1, typename T2> bool minimize(T1 &a, T2 b){if (a > b) {a = b; return true;} return false;} template <typename T1, typename T2> bool maximize(T1 &a, T2 b){if (a < b) {a = b; return true;} return false;} const int mod = 1e9 + 7; const int N = 1e3 + 5; const ll inf = 1e16; int n, m, q, k, check[N]; int sz[N]; ll f[N][N][2], res[N], cost[N]; vector <int> a[N]; void inp() { cin >> n >> m; for(int i = 1; i <= n; ++i) cin >> cost[i]; for(int i = 1; i <= m; ++i) { int u, v; cin >> u >> v; a[u].pb(v); a[v].pb(u); } } void dfs(int u, int pre) { sz[u] = 1; for(int v : a[u]) { if(v == pre) continue; dfs(v, u); sz[u] += sz[v]; for(int i = sz[u]; i >= 0; --i) for(int j = max(0, i - sz[u] + sz[v]); j <= min(i, sz[v]); ++j) { minimize(f[u][i][0], f[u][i - j][0] + min(f[v][j][0], f[v][j][1])); minimize(f[u][i][1], f[u][i - j][1] + min(f[v][j][0], f[v][j][1])); if(j > 0) minimize(f[u][i][1], f[u][i - j][1] + f[v][j - 1][0] + cost[v]); if(i - j > 0) minimize(f[u][i][1], f[u][i - j - 1][0] + f[v][j][1] + cost[u]); if(j > 0 && i - j > 0) minimize(f[u][i][1], f[u][i - j - 1][0] + f[v][j - 1][0] + cost[u] + cost[v]); } } } int trace(int u, int pre) { int cnt = 1; for(int v : a[u]) { if(check[v]) continue; check[v] = 1; cnt += trace(v, u); } return cnt; } void solve() { memset(res, 0x3f, sizeof res); for(int i = 1; i <= n; ++i) for(int j = 0; j <= n; ++j) { f[i][j][0] = f[i][j][1] = inf; if(j == 0) f[i][j][0] = 0; } for(int i = 1; i <= n; ++i) { if(!check[i]) { check[i] = 1; int cntNode = trace(i, i); dfs(i, i); for(int j = 1; j <= cntNode; ++j) minimize(res[j], min(f[i][j][0], f[i][j][1])); } } cin >> q; while(q--) { cin >> k; int cnt = upper_bound(res + 1, res + n + 1, k) - res - 1; if(cnt < 2) cout << 0 << '\n'; else cout << cnt << '\n'; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); // freopen(file".inp" , "r" , stdin); // freopen(file".out" , "w" , stdout); inp(); solve(); 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...