#include <bits/stdc++.h>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define getbit(x, i) (((x) >> (i)) & 1)
#define bit(x) (1 << (x))
#define file ""
using namespace std;
mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count());
long long rand(long long l, long long r) {
return l + rd() % (r - l + 1);
}
const int N = 1e3 + 5;
const int mod = 1e9 + 7; // 998244353;
const int inf = INT_MAX;
const long long llinf = LLONG_MAX;
const int lg = 25; // lg + 1
template<class X, class Y> bool minimize(X &a, Y b) {
return a > b ? (a = b, true) : false;
}
template<class X, class Y> bool maximize(X &a, Y b) {
return a < b ? (a = b, true) : false;
}
template<class X, class Y> bool add(X &a, Y b) {
a += b;
while (a >= mod) a -= mod;
while (a < 0) a += mod;
return true;
}
int n, m;
vector<int> adj[N];
int d[N];
long long f[N][N][2];
int sz[N];
int vis[N];
void dfs(int u, int p) {
vis[u] = true;
f[u][0][0] = 0;
if (u != 0) sz[u] = 1;
for (int v : adj[u]) if (v != p && !vis[v]) {
dfs(v, u);
for (int i = sz[u]; i >= 0; --i)
for (int j = sz[v]; j >= 0; --j) {
minimize(f[u][i + j][0], f[u][i][0] + min(f[v][j][0], f[v][j][1]));
minimize(f[u][i + j][1], f[u][i][1] + min(f[v][j][0], f[v][j][1]));
minimize(f[u][i + j + 1][1], d[u] + f[u][i][0] + f[v][j][1]);
minimize(f[u][i + j + 1][1], f[u][i][1] + d[v] + f[v][j][0]);
minimize(f[u][i + j + 2][1], f[u][i][0] + d[u] + f[v][j][0] + d[v]);
}
sz[u] += sz[v];
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
// freopen(file".inp", "r",stdin);
// freopen(file".out", "w",stdout);
cin >> n >> m;
for (int i = 1; i <= n; ++i)
cin >> d[i];
for (int i = 1; i <= m; ++i) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
memset(f, 0x3f, sizeof f);
f[0][0][0] = 0;
for (int i = 1; i <= n; ++i) {
if (!vis[i]) {
adj[0].push_back(i);
dfs(0, -1);
}
}
int q;
cin >> q;
while (q--) {
int s;
cin >> s;
int x = n;
for (int i = n; i >= 0; --i) {
if (f[0][i][0] <= s) {
x = i;
break;
}
}
cout << x << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
16220 KB |
Output is correct |
2 |
Correct |
6 ms |
16376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
16220 KB |
Output is correct |
2 |
Correct |
6 ms |
16376 KB |
Output is correct |
3 |
Correct |
65 ms |
18528 KB |
Output is correct |
4 |
Correct |
54 ms |
19024 KB |
Output is correct |
5 |
Correct |
161 ms |
18816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
18260 KB |
Output is correct |
2 |
Correct |
29 ms |
18000 KB |
Output is correct |
3 |
Correct |
44 ms |
18548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
16220 KB |
Output is correct |
2 |
Correct |
6 ms |
16376 KB |
Output is correct |
3 |
Correct |
65 ms |
18528 KB |
Output is correct |
4 |
Correct |
54 ms |
19024 KB |
Output is correct |
5 |
Correct |
161 ms |
18816 KB |
Output is correct |
6 |
Correct |
26 ms |
18260 KB |
Output is correct |
7 |
Correct |
29 ms |
18000 KB |
Output is correct |
8 |
Correct |
44 ms |
18548 KB |
Output is correct |
9 |
Correct |
53 ms |
18304 KB |
Output is correct |
10 |
Correct |
62 ms |
18928 KB |
Output is correct |
11 |
Correct |
158 ms |
18764 KB |
Output is correct |