#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ii pair<int,int>
#define all(x) x.begin(),x.end()
#define fo(i, a, b) for (int i=(a);i<=(b);++i)
#define fd(i, a, b) for (int i=(a);i>=(b);--i)
#define umax(a, b) a=max(a, b)
#define umin(a, b) a=min(a, b)
#define ve vector
#define vi ve<int>
#define vl ve<ll>
#define vvi ve<vi>
#define ar array
#define fi first
#define se second
const int N = 1e3+5;
int n, m, a[N], sz[N];
vi g[N];
ll dp[N][N][2];
void dfs(int u, int p) {
sz[u] = 1;
dp[u][0][0] = 0;
dp[u][1][1] = a[u];
for (int v : g[u]) if (v != p) {
dfs(v, u);
fd(j,sz[u],0) {
fd(i,sz[v],0) {
if (j > 1) umin(dp[u][i+j][0], dp[v][i][0] + dp[u][j][0]);
if (i > 1) umin(dp[u][i+j][0], dp[v][i][1] + dp[u][j][0]);
umin(dp[u][i+j][1], dp[v][i][1] + dp[u][j][1]);
umin(dp[u][i+j][1], dp[v][i][0] + dp[u][j][1]);
umin(dp[u][i+j+1][1], dp[v][i][1] + dp[u][j][0] + a[u]);
umin(dp[u][i+j+1][1], dp[v][i][0] + dp[u][j][1] + a[v]);
umin(dp[u][i+j+2][1], dp[v][i][0] + dp[u][j][0] + a[u] + a[v]);
}
}
sz[u] += sz[v];
}
}
void sol(int tc) {
memset(dp, 0x3f, sizeof dp);
cin >> n >> m;
fo(i,1,n) cin >> a[i];
fo(i,1,m) {
int u, v; cin >> u >> v;
g[u].pb(v), g[v].pb(u);
}
vi st;
fo(i,1,n) if (!sz[i]) {
dfs(i, -1);
st.pb(i);
}
int q; cin >> q;
while (q--) {
ll X; cin >> X;
int ans = 0;
for (int x : st) {
fo(i,1,sz[x]) {
if (dp[x][i][0] <= X
|| dp[x][i][1] <= X)
ans = i;
}
}
cout << ans << '\n';
}
}
signed main() {
cin.tie(0) -> sync_with_stdio(0);
if (fopen("A.inp", "r")) freopen("A.inp", "r", stdin);
int tc = 1; // cin >> tc;
fo(i,1,tc) sol(i);
}
컴파일 시 표준 에러 (stderr) 메시지
dzumbus.cpp: In function 'int main()':
dzumbus.cpp:76:37: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | if (fopen("A.inp", "r")) freopen("A.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# | 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... |