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>
using namespace std;
#define task ""
#define fi first
#define se second
#define MASK(i) (1LL << (i))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define BIT(mask, i) ((mask) >> (i) & 1)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (b), _a = (a); i >= _a; --i)
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 dx[] = {-1, 0, 0, +1};
const int dy[] = {0, -1, +1, 0};
const int INF = 1e9 + 7;
const int MAX = 1010;
int n, m, q;
bool vis[MAX];
int D[MAX], sz[MAX];
vector<int> adj[MAX];
int dp[MAX][MAX][2][2];
void dfs(int u, int p) {
vis[u] = true;
for (int j = 0; j <= n; ++j) {
for (int a = 0; a < 2; ++a) {
for (int b = 0; b < 2; ++b)
dp[u][j][a][b] = INF;
}
}
sz[u] = 1;
dp[u][0][0][0] = 0;
dp[u][0][1][0] = D[u];
for (int v: adj[u]) if (v != p) {
dfs(v, u);
for (int j = sz[u]; j >= 0; --j) {
for (int jj = sz[v]; jj >= 0; --jj) {
minimize(dp[u][j + jj][0][0], dp[u][j][0][0] + dp[v][jj][0][0]);
minimize(dp[u][j + jj][0][0], dp[u][j][0][0] + dp[v][jj][1][0]);
minimize(dp[u][j + jj][0][0], dp[u][j][0][0] + dp[v][jj][1][1]);
minimize(dp[u][j + jj][1][0], dp[u][j][1][0] + dp[v][jj][0][0]);
minimize(dp[u][j + jj][1][1], dp[u][j][1][1] + dp[v][jj][0][0]);
minimize(dp[u][j + jj][1][1], dp[u][j][1][1] + dp[v][jj][1][1]);
minimize(dp[u][j + jj + 1][1][1], dp[u][j][1][1] + dp[v][jj][1][0]);
minimize(dp[u][j + jj + 2][1][1], dp[u][j][1][0] + dp[v][jj][1][0]);
}
}
sz[u] += sz[v];
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".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);
}
dfs(1, -1);
for (int i = 1; i <= n; ++i) {
if (!vis[i]) dfs(i, -1);
}
vector<pair<int, int>> store;
for (int j = 0; j <= n; ++j) {
for (int a = 0; a < 2; ++a) {
for (int b = 0; b < 2; ++b) {
if (dp[1][j][a][b] != INF) store.push_back({dp[1][j][a][b], j});
if (dp[1][j][a][b] != INF) store.push_back({dp[1][j][a][b], j});
}
}
}
sort(all(store));
for (int i = 1; i < store.size(); ++i) {
maximize(store[i].second, store[i - 1].second);
}
cin >> q; while (q--) {
int S; cin >> S;
cout << store[lower_bound(all(store), make_pair(S, n + 1)) - store.begin() - 1].second << '\n';
}
return 0;
}
Compilation message (stderr)
dzumbus.cpp: In function 'int main()':
dzumbus.cpp:89:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for (int i = 1; i < store.size(); ++i) {
| ~~^~~~~~~~~~~~~~
dzumbus.cpp:64:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
dzumbus.cpp:65:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |