#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
#define fi first
#define se second
#define pii pair<int, int>
#define pb push_back
#define eb emplace_back
#define task "file"
const int nmax = 1005;
int dp[nmax][nmax][2][2], sz[nmax], d[nmax], vis[nmax], n, m;
int res[nmax];
vector<int> adj[nmax];
class DSU
{
private:
vector<int> parent, rank;
public:
DSU(int n)
{
parent.resize(n + 1);
rank.resize(n + 1, 0);
for (int i = 0; i <= n; i++)
{
parent[i] = i;
}
}
int find_set(int v)
{
if (v == parent[v])
return v;
return parent[v] = find_set(parent[v]);
}
void union_sets(int a, int b)
{
a = find_set(a);
b = find_set(b);
if (a != b)
{
if (rank[a] < rank[b])
swap(a, b);
parent[b] = a;
if (rank[a] == rank[b])
rank[a]++;
}
}
};
void dfs(int u, int p = -1)
{
assert(!vis[u]);
vis[u] = 1;
sz[u] = 1;
dp[u][0][0][0] = 0;
dp[u][1][1][0] = d[u];
for (int v : adj[u])
{
if (v == p)
continue;
dfs(v, u);
for (int i = sz[u]; i >= 0; i--)
{
for (int j = 0; j <= sz[v]; j++)
{
dp[u][i + j][0][0] = min(dp[u][i + j][0][0], dp[u][i][0][0] + dp[v][j][1][1]);
dp[u][i + j][0][0] = min(dp[u][i + j][0][0], dp[u][i][0][0] + dp[v][j][0][0]);
dp[u][i + j][1][0] = min(dp[u][i + j][1][0], dp[u][i][1][0] + dp[v][j][0][0]);
dp[u][i + j][1][1] = min(dp[u][i + j][1][1], dp[u][i][1][0] + dp[v][j][1][0]);
dp[u][i + j][1][1] = min(dp[u][i + j][1][1], dp[u][i][1][0] + dp[v][j][1][1]);
dp[u][i + j][1][1] = min(dp[u][i + j][1][1], dp[u][i][1][1] + dp[v][j][1][1]);
dp[u][i + j][1][1] = min(dp[u][i + j][1][1], dp[u][i][1][1] + dp[v][j][0][0]);
dp[u][i + j][1][1] = min(dp[u][i + j][1][1], dp[u][i][1][1] + dp[v][j][1][0]);
}
}
sz[u] += sz[v];
}
}
signed main()
{
if (fopen(task ".inp", "r"))
{
freopen(task ".inp", "r", stdin);
freopen(task ".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
DSU dsu(n);
for (int i = 1; i <= n; i++)
{
cin >> d[i];
}
for (int u, v, i = 1; i <= m; i++)
{
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
dsu.union_sets(u, v);
}
memset(dp, 0x3f3f3f3f3f, sizeof(dp));
for (int i = 1; i <= n; i++)
{
if (dsu.find_set(i) == i)
{
adj[0].pb(i);
adj[i].pb(0);
dsu.union_sets(0, i);
}
}
dfs(0);
vector<pii> res;
for (int i = 0; i <= n; i++)
{
while (!res.empty() && res.back().fi >= dp[0][i][0][0])
res.pop_back();
res.pb({dp[0][i][0][0], i});
}
int q;
cin >> q;
while (q--)
{
int x;
cin >> x;
pii k = {x + 1, -1};
int idx = upper_bound(res.begin(), res.end(), k) - res.begin() - 1;
cout << res[idx].se << '\n';
}
return 0;
}
Compilation message (stderr)
dzumbus.cpp: In function 'int main()':
dzumbus.cpp:105:16: warning: overflow in conversion from 'long int' to 'int' changes value from '271644049215' to '1061109567' [-Woverflow]
105 | memset(dp, 0x3f3f3f3f3f, sizeof(dp));
| ^~~~~~~~~~~~
dzumbus.cpp:86:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
86 | freopen(task ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
dzumbus.cpp:87:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
87 | 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... |