#include <bits/stdc++.h>
#define int long long
#define prev pre
using namespace std;
const int N = 1003;
int n;
int a[N];
bool vis[N];
vector<int> g[N];
int sz[N];
int dp[N][N][3];
int prev[N][3];
inline void relax (int& x, int y) {
x = min(x, y);
}
void dfs (int v, int p = 0) {//node, taken people, take my self
dp[v][0][0] = 0;
dp[v][0][1] = a[v];
sz[v] = 1;
vis[v] = true;
for (int u : g[v]) {
if (!vis[u]) {
dfs(u, v);
}
}
dp[v][0][0] = 0;
dp[v][0][1] = a[v];
for (int u : g[v]) {
if (u == p) {
continue;
}
for (int i = 0; i <= n; ++i) {
prev[i][0] = dp[v][i][0];
prev[i][1] = dp[v][i][1];
prev[i][2] = dp[v][i][2];
}
for (int i = 0; i <= sz[v]; ++i) {
for (int j = 0; j <= sz[u]; ++j) {
relax(dp[v][i + j][0], min({dp[u][j][1], dp[u][j][0], dp[u][j][2]}) + prev[i][0]);
relax(dp[v][i + j][1], min(prev[i][0] + a[v], prev[i][1]) + dp[u][j][0]);
relax(dp[v][i + j][2], prev[i][2] + min({dp[u][j][2], dp[u][j][0]}));
relax(dp[v][i + j + 1][2], prev[i][2] + dp[u][j][1]);
relax(dp[v][i + j + 1][2], min(prev[i][0] + a[v], prev[i][1]) + dp[u][j][2]);
relax(dp[v][i + j + 2][2], min(prev[i][0] + a[v], prev[i][1]) + dp[u][j][1]);
}
}
sz[v] += sz[u];
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int m;
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
memset(dp, 0x3f3f3f3f, sizeof dp);
vector<int> roots;
for (int i = 1; i <= n; ++i) {
if (!vis[i]) {
roots.push_back(i);
dfs(i);
}
}
dp[0][0][0] = 0;
dp[0][0][1] = 1;
for (int r : roots) {
for (int i = 0; i <= n; ++i) {
for (int j = 0; j < 3; ++j) {
prev[i][j] = dp[0][i][j];
}
}
for (int i = 0; i <= sz[0]; ++i) {
for (int j = 0; j <= sz[r]; ++j) {
relax(dp[0][i + j][0], prev[i][0] + min({dp[r][j][0], dp[r][j][1], dp[r][j][2]}));
}
}
sz[0] += sz[r];
}
int q;
cin >> q;
while (q--) {
int s;
cin >> s;
for (int i = n; i >= 0; --i) {
if (dp[0][i][0] <= s || dp[0][i][1] <= s || dp[0][i][2] <= s) {
cout << i << '\n';
break;
}
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
24020 KB |
Output is correct |
2 |
Correct |
17 ms |
24148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
24020 KB |
Output is correct |
2 |
Correct |
17 ms |
24148 KB |
Output is correct |
3 |
Correct |
72 ms |
25072 KB |
Output is correct |
4 |
Correct |
71 ms |
25008 KB |
Output is correct |
5 |
Correct |
200 ms |
24632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
24804 KB |
Output is correct |
2 |
Correct |
39 ms |
24604 KB |
Output is correct |
3 |
Correct |
56 ms |
26208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
24020 KB |
Output is correct |
2 |
Correct |
17 ms |
24148 KB |
Output is correct |
3 |
Correct |
72 ms |
25072 KB |
Output is correct |
4 |
Correct |
71 ms |
25008 KB |
Output is correct |
5 |
Correct |
200 ms |
24632 KB |
Output is correct |
6 |
Correct |
37 ms |
24804 KB |
Output is correct |
7 |
Correct |
39 ms |
24604 KB |
Output is correct |
8 |
Correct |
56 ms |
26208 KB |
Output is correct |
9 |
Correct |
68 ms |
26072 KB |
Output is correct |
10 |
Correct |
86 ms |
26748 KB |
Output is correct |
11 |
Correct |
185 ms |
26504 KB |
Output is correct |