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 ll long long
#define all(aaa) aaa.begin(), aaa.end()
const ll INF = 1e18;
const int N = 1005;
vector<int> g[N];
bool used[N];
ll dp[N][N][2], cost[N], dp2[N][2];
int sz[N];
void dfs(int node) {
used[node] = true;
for (int to : g[node]) {
if (!used[to]) {
dfs(to);
}
}
}
void dive(int node, int anc) {
sz[node] = 1;
for (int to : g[node]) {
if (to != anc) {
dive(to, node);
sz[node] += sz[to];
}
}
int s = 0;
dp[node][0][0] = 0;
for (int to : g[node]) {
if (to != anc) {
for (int j = s; j >= 0; j--) {
for (int k = 0; k <= sz[to]; k++) {
dp[node][j + k][0] = min(dp[node][j + k][0],
min(dp[to][k][0], dp[to][k][1]) + dp[node][j][0]);
}
}
s += sz[to];
}
}
for (int i = 0; i < N; i++) {
dp2[i][0] = dp2[i][1] = INF;
}
dp2[1][0] = cost[node];
s = 1;
for (int to : g[node]) {
if (to != anc) {
for (int j = s; j >= 0; j--) {
for (int k = 0; k <= sz[to]; k++) {
for (int h = 0; h < 2; h++) {
dp2[j + k][h] = min(dp2[j + k][h], dp[to][k][0] + dp2[j][h]);
dp2[j + k][1] = min(dp2[j + k][1], dp[to][k][1] + dp2[j][h]);
dp2[j + k + 1][1] = min(dp2[j + k + 1][1], dp[to][k][0] + cost[to] + dp2[j][h]);
}
}
}
s += sz[to];
}
}
for (int i = 1; i < N; i++) {
dp[node][i][1] = dp2[i][1];
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> cost[i];
}
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
x--, y--;
g[x].push_back(y);
g[y].push_back(x);
}
int rt = n++;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
dp[i][j][0] = dp[i][j][1] = INF;
}
}
cost[rt] = INF;
for (int i = 0; i < n - 1; i++) {
if (!used[i]) {
g[rt].push_back(i);
dfs(i);
}
}
dive(rt, -1);
int q;
cin >> q;
while (q--) {
ll x;
cin >> x;
int ans = 0;
if (dp[rt][2][0] <= x) {
ans = 2;
while (dp[rt][ans + 1][0] <= x)
ans++;
}
cout << ans << "\n";
}
return 0;
}
# | 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... |