This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// In the name of God
// Hope is last to die
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define pb push_back
// #define int long long
#define S second
#define F first
#define mp make_pair
#define smax(x, y) (x) = max((x), (y))
#define smin(x, y) (x) = min((x), (y))
#define all(x) (x).begin(), (x).end()
#define len(x) ((int)(x).size())
const int maxn = 1000 + 5;
const ll inf = 1e15 + 7;
ll n, m, dp[maxn][maxn][3], a[maxn], cnt[maxn], tmp[maxn][3];
bool mark[maxn];
vector<int> adj[maxn];
void fake(int v, int p){
mark[v] = 1;
for(auto u: adj[v]) if(!mark[u]) fake(u, v);
}
void dfs(int v, int p){
dp[v][1][1] = a[v];
dp[v][0][0] = 0;
cnt[v] = 1;
for(auto u: adj[v]){
if(u == p) continue;
dfs(u, v);
for(int i = 0; i <= cnt[u] + cnt[v]; i++) for(int j = 0; j < 3; j++) tmp[i][j] = inf;
for(int i = 0; i < cnt[v]; i++){
for(int j = 0; j <= cnt[u]; j++){
smin(tmp[i + j][0], dp[v][i][0] + min(dp[u][j][2], dp[u][j][0]));
}
}
for(int i = 1; i <= cnt[v]; i++){
for(int j = 0; j <= cnt[u]; j++){
smin(tmp[i + j][1], dp[v][i][1] + dp[u][j][0]);
}
}
for(int i = 1; i <= cnt[v]; i++){
for(int j = 0; j <= cnt[u]; j++){
smin(tmp[i + j][2], dp[v][i][1] + min(dp[u][j][1], dp[u][j][2]));
smin(tmp[i + j][2], dp[v][i][2] + min(dp[u][j][0], min(dp[u][j][1], dp[u][j][2])));
}
}
cnt[v] += cnt[u];
for(int i = 0; i <= cnt[v]; i++){
for(int j = 0; j < 3; j++) dp[v][i][j] = tmp[i][j];
}
}
}
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
for(int i = 0; i <= n; i++) for(int j = 0; j <= n; j++) for(int k = 0; k < 3; k++) dp[i][j][k] = inf;
for(int i = 0; i < n; i++) cin >> a[i];
for(int i = 0; i < m; i++){
int u, v; cin >> u >> v;
u--, v--;
adj[u].pb(v);
adj[v].pb(u);
}
for(int i = 0; i < n; i++){
if(!mark[i]){
fake(i, -1);
adj[n].pb(i);
}
}
dfs(n, -1);
int q; cin >> q;
while(q--){
int s; cin >> s;
int ans = 0;
for(int i = 2; i <= n; i++) if(dp[n][i][0] <= s) ans = i;
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... |