This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define Y8o "dzumbus"
#define maxn (int) 1005
#define ll long long
#define fi(i, a, b) for(int i = a; i <= b; i ++)
#define fid(i, a, b) for(int i = a; i >= b; i --)
#define pii pair<int, int>
#define gb(i, j) ((i >> j) & 1)
#define int long long
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r) {
return uniform_int_distribution<ll> (l, r) (rng);
}
const ll INF = 1e18;
int n, m, Q;
int a[maxn], sz[maxn], dd[maxn];
vector<int> o[maxn];
ll dp[maxn][maxn][2], tmp[maxn][2], ans[maxn];
void dfs(int u, int p = 0) {
dd[u] = sz[u] = 1;
dp[u][0][0] = 0;
for(int v : o[u]) if(v != p) {
dfs(v, u);
memset(tmp, 0x3f, sizeof tmp);
fi(i, 0, sz[u]) {
fi(j, 0, sz[v]) {
tmp[i + j][0] = min( tmp[i + j][0],
dp[u][i][0] + min(dp[v][j][0], dp[v][j][1]) );
tmp[i + j][1] = min( tmp[i + j][1],
dp[u][i][1] + min(dp[v][j][0], dp[v][j][1]) );
if(i + j + 1 <= sz[u] + sz[v])
tmp[i + j + 1][1] = min( tmp[i + j + 1][1],
dp[u][i][0] + dp[v][j][1] + a[u] );
if(i + j + 1 <= sz[u] + sz[v])
tmp[i + j + 1][1] = min( tmp[i + j + 1][1],
dp[u][i][1] + dp[v][j][0] + a[v] );
if(i + j + 2 <= sz[u] + sz[v])
tmp[i + j + 2][1] = min( tmp[i + j + 2][1],
dp[u][i][0] + dp[v][j][0] + a[u] + a[v] );
}
}
memcpy(dp[u], tmp, sizeof tmp);
sz[u] += sz[v];
}
}
void solve() {
cin >> n >> m;
fi(i, 1, n) cin >> a[i];
fi(i, 1, m) {
int u, v; cin >> u >> v;
o[u].push_back(v), o[v].push_back(u);
}
memset(dp, 0x3f, sizeof dp);
memset(ans, 0x3f, sizeof ans);
ans[0] = 0;
fi(i, 1, n) if(!dd[i]) {
dfs(i);
fid(j, n, 1) {
fi(t, 1, min(j, sz[i])) {
ans[j] = min(ans[j], ans[j - t] + min(dp[i][t][0], dp[i][t][1]));
}
}
}
cin >> Q;
fi(i, 1, Q) {
ll lim; cin >> lim;
fid(j, n, 0) if(ans[j] <= lim) {
cout << j << '\n';
break;
}
}
}
main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL), cout.tie(NULL);
if(fopen(Y8o".inp", "r")) {
freopen(Y8o".inp", "r", stdin);
// freopen(Y8o".out", "w", stdout);
}
int nTest = 1;
// cin >> nTest;
while(nTest --) {
solve();
}
return 0;
}
Compilation message (stderr)
dzumbus.cpp:89:2: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
89 | main() {
| ^~~~
dzumbus.cpp: In function 'int main()':
dzumbus.cpp:94:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
94 | freopen(Y8o".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |