// #define _FORTIFY_SOURCE 0
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("no-stack-protector")
// #pragma GCC optimize("unroll-loops")
// //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
// #pragma GCC optimize("inline")
// #pragma GCC optimize("-fgcse")
// #pragma GCC optimize("-fgcse-lm")
// #pragma GCC optimize("-fipa-sra")
// #pragma GCC optimize("-ftree-pre")
// #pragma GCC optimize("-ftree-vrp")
// #pragma GCC optimize("-fpeephole2")
// #pragma GCC optimize("-ffast-math")
// #pragma GCC optimize("-fsched-spec")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC optimize("-falign-jumps")
// #pragma GCC optimize("-falign-loops")
// #pragma GCC optimize("-falign-labels")
// #pragma GCC optimize("-fdevirtualize")
// #pragma GCC optimize("-fcaller-saves")
// #pragma GCC optimize("-fcrossjumping")
// #pragma GCC optimize("-fthread-jumps")
// #pragma GCC optimize("-funroll-loops")
// #pragma GCC optimize("-fwhole-program")
// #pragma GCC optimize("-freorder-blocks")
// #pragma GCC optimize("-fschedule-insns")
// #pragma GCC optimize("inline-functions")
// #pragma GCC optimize("-ftree-tail-merge")
// #pragma GCC optimize("-fschedule-insns2")
// #pragma GCC optimize("-fstrict-aliasing")
// #pragma GCC optimize("-fstrict-overflow")
// #pragma GCC optimize("-falign-functions")
// #pragma GCC optimize("-fcse-skip-blocks")
// #pragma GCC optimize("-fcse-follow-jumps")
// #pragma GCC optimize("-fsched-interblock")
// #pragma GCC optimize("-fpartial-inlining")
// #pragma GCC optimize("no-stack-protector")
// #pragma GCC optimize("-freorder-functions")
// #pragma GCC optimize("-findirect-inlining")
// #pragma GCC optimize("-fhoist-adjacent-loads")
// #pragma GCC optimize("-frerun-cse-after-loop")
// #pragma GCC optimize("inline-small-functions")
// #pragma GCC optimize("-finline-small-functions")
// #pragma GCC optimize("-ftree-switch-conversion")
// #pragma GCC optimize("-foptimize-sibling-calls")
// #pragma GCC optimize("-fexpensive-optimizations")
// #pragma GCC optimize("-funsafe-loop-optimizations")
// #pragma GCC optimize("inline-functions-called-once")
// #pragma GCC optimize("-fdelete-null-pointer-checks")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned ll
#define vi vector<ll>
#define vvi vector<vi>
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define ld long double
#define pii pair<ll, ll>
#define mt make_tuple
#define mn(a, b) a = min(a, b)
#define mx(a, b) a = max(a, b)
#define base complex<ld>
using namespace std;
const ll INF = (ll)2e9;
const ll inf = (ll)1e18;
const ld eps = (ld)1e-12;
const ll mod = (ll)1e9 + 7;
const ll p = 31;
const ll mod2 = (ll)1e9 + 7;
const ll MAXN = (ll)1000 + 3;
const ll MAXC = (ll)1e6 + 11;
const ll MAXE = (ll)100;
const ll MAXLOG = (ll)19;
const ll asci = (ll)256;
const ll block = 31623;
const ld PI = acos(-1LL);
const ld e = 2.7182818284;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// typedef tree<
// pii,
// null_type,
// less<pii>,
// rb_tree_tag,
// tree_order_statistics_node_update>
// ordered_set;
ll int_rand() {
if (RAND_MAX == (1LL << 15) - 1LL) {
return (rand() << 15) + rand();
}
return rand();
}
ll ll_rand() {
return ((ll)int_rand() << 30LL) + int_rand();
}
istream& operator >>(istream &in, pii &x) {
cin >> x.first >> x.second;
return in;
}
template <class T>
istream& operator >>(istream &in, vector<T> &arr){
for (T &cnt : arr) {
in >> cnt;
}
return in;
}
ll dp[MAXN][MAXN];
ll dp_h[MAXN][MAXN][2];
ll dp_take[MAXN][MAXN][2];
ll ans[MAXN];
ll sz[MAXN];
vi graph[MAXN];
ll cost[MAXN];
ll used[MAXN];
void dp_calc(ll v, ll p = -1) {
dp_h[v][0][0] = cost[v];
dp[v][0] = 0;
if ((p == -1 && graph[v].size() == 0) || (p != -1 && graph[v].size() <= 1)) {
sz[v] = 1;
return;
}
dp_take[v][0][0] = 0;
for (ll u : graph[v]) {
if (p == u) continue;
dp_calc(u, v);
for (ll ws = sz[v] + sz[u]; ws > -1; --ws) {
for (ll w1 = max(0LL, ws - sz[u]); w1 <= ws; ++w1) {
ll w2 = ws - w1;
mn(dp[v][w1 + w2], dp[v][w1] + dp[u][w2]);
mn(dp_take[v][w1 + w2][1], dp_take[v][w1][1] + min({dp[u][w2], dp_h[u][max(w2 - 1, 0LL)][0], dp_h[u][w2][1]}));
mn(dp_take[v][w1 + w2][1], dp_take[v][w1][0] + min(dp_h[u][max(w2 - 1, 0LL)][0], dp_h[u][w2][1]));
mn(dp_take[v][w1 + w2][0], dp_take[v][w1][0] + dp[u][w2]);
}
}
sz[v] += sz[u];
}
++sz[v];
for (ll w = 0; w <= sz[v]; ++w) {
for (ll c = 0; c < 2; ++c) {
mn(dp_h[v][w + c][c], dp_take[v][w][c] + cost[v]);
mn(dp[v][w + c], dp_h[v][w + c][c]);
}
}
}
void solve() {
for (ll i = 0; i < MAXN; ++i) {
ans[i] = inf;
for (ll k = 0; k < 2; ++k) {
for (ll j = 0; j < MAXN; ++j) {
dp_h[i][j][0] = inf;
dp_h[i][j][1] = inf;
dp_take[i][j][0] = inf;
dp_take[i][j][1] = inf;
dp[i][j] = inf;
}
}
}
ll n, m; cin >> n >> m;
for (ll i = 0; i < n; ++i) cin >> cost[i];
for (ll i = 0; i < m; ++i) {
ll v, u; cin >> v >> u;
--v, --u;
graph[v].push_back(u);
graph[u].push_back(v);
}
vi headers;
for (ll i = 0; i < n; ++i) {
if (!sz[i]) {
dp_calc(i);
headers.push_back(i);
}
}
ans[0] = 0;
ll sz_ans = 0;
for (ll i : headers) {
for (ll ws = sz_ans + sz[i]; ws > -1; --ws) {
for (ll w = max(0LL, ws - sz[i]); w <= ws; ++w) {
mn(ans[ws], dp[i][ws - w] + ans[w]);
}
}
sz_ans += sz[i];
}
ll q; cin >> q;
while (q--) {
ll d; cin >> d;
cout << upper_bound(ans, ans + MAXN, d) - ans - 1 << "\n";
}
}
signed main() {
srand(time(0LL));
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
#endif
cout.precision(30);
solve();
return 0LL;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
349 ms |
40044 KB |
Output is correct |
2 |
Correct |
447 ms |
39916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
349 ms |
40044 KB |
Output is correct |
2 |
Correct |
447 ms |
39916 KB |
Output is correct |
3 |
Correct |
608 ms |
40872 KB |
Output is correct |
4 |
Correct |
340 ms |
40812 KB |
Output is correct |
5 |
Correct |
316 ms |
40428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
40668 KB |
Output is correct |
2 |
Correct |
62 ms |
40428 KB |
Output is correct |
3 |
Correct |
70 ms |
40168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
349 ms |
40044 KB |
Output is correct |
2 |
Correct |
447 ms |
39916 KB |
Output is correct |
3 |
Correct |
608 ms |
40872 KB |
Output is correct |
4 |
Correct |
340 ms |
40812 KB |
Output is correct |
5 |
Correct |
316 ms |
40428 KB |
Output is correct |
6 |
Correct |
62 ms |
40668 KB |
Output is correct |
7 |
Correct |
62 ms |
40428 KB |
Output is correct |
8 |
Correct |
70 ms |
40168 KB |
Output is correct |
9 |
Correct |
71 ms |
41964 KB |
Output is correct |
10 |
Correct |
73 ms |
42604 KB |
Output is correct |
11 |
Correct |
69 ms |
42456 KB |
Output is correct |