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 fi first
#define se second
#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define file "name"
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 mod = 1e9 + 7;
const int N = 1e3 + 5;
const ll inf = 1e16;
int n, m, q, k;
int sz[N];
ll f[N][N][2], res[N], cost[N];
vector <int> a[N];
void inp()
{
cin >> n >> m;
for(int i = 1; i <= n; ++i) cin >> cost[i];
for(int i = 1; i <= m; ++i) {
int u, v;
cin >> u >> v;
a[u].pb(v);
a[v].pb(u);
}
}
void dfs(int u, int pre) {
sz[u] = 1;
for(int v : a[u]) {
if(v == pre) continue;
dfs(v, u);
sz[u] += sz[v];
for(int i = sz[u]; i >= 0; --i) {
for(int j = max(0, i - sz[u] + sz[v]); j <= min(i, sz[v]); ++j) {
minimize(f[u][i][0], f[u][i - j][0] + min(f[v][j][0], f[v][j][1]));
minimize(f[u][i][1], f[u][i - j][1] + min(f[v][j][0], f[v][j][1]));
if(j > 0) minimize(f[u][i][1], f[u][i - j][1] + f[v][j - 1][0] + cost[v]);
if(i - j > 0) minimize(f[u][i][1], f[u][i - j - 1][0] + f[v][j][1] + cost[u]);
if(j > 0 && i - j > 0) minimize(f[u][i][1], f[u][i - j - 1][0] + f[v][j - 1][0] + cost[u] + cost[v]);
}
}
}
}
void solve()
{
for(int i = 1; i <= n; ++i)
for(int j = 0; j <= n; ++j) {
f[i][j][0] = f[i][j][1] = inf;
if(j == 0) f[i][j][0] = 0;
}
dfs(1, 1);
for(int i = 1; i <= n; ++i) res[i] = min(f[1][i][0], f[1][i][1]);
cin >> q;
while(q--) {
cin >> k;
int cnt = upper_bound(res + 1, res + n + 1, k) - res - 1;
if(cnt < 2) cout << 0 << '\n';
else cout << cnt << '\n';
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
// freopen(file".inp" , "r" , stdin);
// freopen(file".out" , "w" , stdout);
inp();
solve();
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... |