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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
#define int long long
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef double db;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
int N, M, Q, S, idx, st, ed, D[1005], pos[1005], dp[1005][1005][2];
vector<int> adj[1005];
void dfs(int n, int e = -1) {
pos[++idx] = n;
for (auto u : adj[n])
if (u != e) dfs(u, n);
}
main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N >> M;
for (int i = 1; i <= N; i++) cin >> D[i];
for (int i = 1, u, v; i <= M; i++) {
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
for (int i = 1; i <= N; i++) {
if (adj[i].size() == 1) {
if (!st) st = i;
else ed = i;
}
}
dfs(st);
dp[0][0][1] = 1e17;
for (int i = 1; i <= N; i++)
dp[0][i][0] = dp[0][i][1] = 1e17;
for (int i = 1; i <= N; i++)
for (int j = i + 1; j <= N; j++) dp[i][j][0] = dp[i][j][1] = 1e17;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= i; j++) {
for (bool b : {0, 1}) {
if (b) {
dp[i][j][b] = min(dp[i - 1][j - 1][1] + D[pos[i - 1]], dp[i - 1][j][0]);
} else {
dp[i][j][b] = dp[i - 1][j][0];
if (i > 1 && j >= 2) dp[i][j][b] = min(dp[i][j][b], dp[i - 1][j - 2][1] + D[pos[i]] + D[pos[i - 1]]);
}
}
}
}
cin >> Q;
while (Q--) {
cin >> S;
if (N == 1) {
cout << "0\n";
continue;
}
int ans = 0;
for (int i = 1; i <= N; i++)
if (min(dp[N][i][0], dp[N - 1][i - 2][1] + D[pos[N]] + D[pos[N - 1]]) <= S) {
ans = i;
}
cout << ans << '\n';
}
}
Compilation message (stderr)
dzumbus.cpp:37:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
37 | main() {
| ^
# | 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... |