#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mp(x, y) make_pair(x, y)
#define MASK(i) ((1LL) << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define fi first
#define se second
using namespace std;
const long inf = 1e9 + 7;
const long mod = 1e9 + 7;
const long Nmax = 1e3 + 7;
const long lg = 20;
const long bs = 1003;
int n, m, q;
int a[Nmax];
int query[Nmax];
vector<int> g[Nmax];
vector<int> tree[Nmax];
bool vs[Nmax];
int dis[Nmax], pre[Nmax], sz[Nmax];
void prim(int s)
{
memset(dis, 0x3f, sizeof dis);
memset(vs, false, sizeof vs);
priority_queue<pii, vector<pii>, greater<pii> > q;
q.push({0, s});
while (!q.empty())
{
int u = q.top().se;
int d = q.top().fi;
q.pop();
if (vs[u]) continue;
vs[u] = true;
if (u != s)
{
tree[u].push_back(pre[u]);
tree[pre[u]].push_back(u);
}
for (auto v : g[u])
{
if (dis[v] > a[v])
{
dis[v] = a[v];
pre[v] = u;
q.push({dis[v], v});
}
}
}
}
bool minimize(ll &a, ll b)
{
if (a > b) {a = b; return true;}
return false;
}
ll pref[Nmax][2], dp[Nmax][Nmax][2];
void combine(int u, int v)
{
for (int i = 0; i <= sz[u]; i++)
{
for (int j = 0; j <= sz[v]; j++)
{
if (i + j > sz[u]) break;
minimize(dp[u][i + j][0], pref[i][0] + min(dp[v][j][0], dp[v][j][1]));
minimize(dp[u][i + j][1], pref[i][1] + min(dp[v][j][0], dp[v][j][1]));
if (j + 1 <= sz[v]) minimize(dp[u][i + j + 1][1], pref[i][0] + dp[v][j][1] + a[u]);
if (i + 1 <= sz[u]) minimize(dp[u][i + j + 1][1], pref[i][1] + dp[v][j][0] + a[v]);
if (i + 1 <= sz[u] && j + 1 <= sz[v]) minimize(dp[u][i + j + 2][1], pref[i][0] + dp[v][j][0] + a[u] + a[v]);
}
}
for (int i = 0; i <= sz[u]; i++)
{
pref[i][0] = min(pref[i][0], dp[u][i][0]);
pref[i][1] = min(pref[i][1], dp[u][i][1]);
}
}
void dfs(int u, int p)
{
sz[u] = 1;
for (auto v : g[u])
{
if (v == p) continue;
dfs(v, u);
sz[u] += sz[v];
}
memset(pref, 0x3f, sizeof pref);
pref[0][0] = pref[0][1] = 0;
for (auto v : g[u])
{
if (v == p) continue;
combine(u, v);
}
dp[u][0][0] = 0;
dp[u][1][1] = a[u];
}
int cnp(int l, int r, ll s)
{
while (r - l > 1)
{
int mid = (l + r) >> 1;
if (dp[1][mid][0] <= s || dp[1][mid][1] <= s) l = mid;
else r = mid;
}
return r - 1;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("name.inp","r",stdin);
//freopen("name.out","w",stdout);
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= m; i++)
{
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
prim(1);
memset(dp, 0x3f, sizeof dp);
dfs(1, -1);
// for (int i = 1; i <= n; i++)
// {
// for (int j = 1; j <= n; j++)
// {
// cerr << dp[i][j][1] << " ";
// }
// cerr << "\n";
// }
// for (int i = 1; i <= n; i++)
// {
// for (int j = 1; j <= n; j++)
// {
// cerr << dp[i][j][0] << " ";
// }
// cerr << "\n";
// }
cin >> q;
while (q--)
{
ll s;
cin >> s;
int id = cnp(0, n, s);
if (id == 1) cout << 0 << "\n";
else cout << id << "\n";
}
}
# | 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... |