#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define file "name"
template <typename T1, typename T2> bool minimize(T1 &a, T2 b){if (a > b) {a = b; return true;} return false;}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b){if (a < b) {a = b; return true;} return false;}
const int mod = 1e9 + 7;
const int N = 1e3 + 5;
const ll inf = 1e16;
int n, m, q, k, check[N];
int sz[N];
ll f[N][N][2], res[N], cost[N];
vector <int> a[N];
void inp()
{
cin >> n >> m;
for(int i = 1; i <= n; ++i) cin >> cost[i];
for(int i = 1; i <= m; ++i) {
int u, v;
cin >> u >> v;
a[u].pb(v);
a[v].pb(u);
}
}
void dfs(int u, int pre) {
sz[u] = 1;
for(int v : a[u]) {
if(v == pre) continue;
dfs(v, u);
sz[u] += sz[v];
for(int i = sz[u]; i >= 0; --i)
for(int j = max(0, i - sz[u] + sz[v]); j <= min(i, sz[v]); ++j) {
minimize(f[u][i][0], f[u][i - j][0] + min(f[v][j][0], f[v][j][1]));
minimize(f[u][i][1], f[u][i - j][1] + min(f[v][j][0], f[v][j][1]));
if(j > 0) minimize(f[u][i][1], f[u][i - j][1] + f[v][j - 1][0] + cost[v]);
if(i - j > 0) minimize(f[u][i][1], f[u][i - j - 1][0] + f[v][j][1] + cost[u]);
if(j > 0 && i - j > 0) minimize(f[u][i][1], f[u][i - j - 1][0] + f[v][j - 1][0] + cost[u] + cost[v]);
}
}
}
int trace(int u, int pre)
{
int cnt = 1;
for(int v : a[u]) {
if(check[v]) continue;
check[v] = 1;
cnt += trace(v, u);
}
return cnt;
}
void solve()
{
memset(res, 0x3f, sizeof res);
for(int i = 1; i <= n; ++i)
for(int j = 0; j <= n; ++j) {
f[i][j][0] = f[i][j][1] = inf;
if(j == 0) f[i][j][0] = 0;
}
for(int i = 1; i <= n; ++i) {
if(!check[i]) {
check[i] = 1;
int cntNode = trace(i, i);
dfs(i, i);
for(int j = 1; j <= cntNode; ++j) minimize(res[j], min(f[i][j][0], f[i][j][1]));
}
}
cin >> q;
while(q--) {
cin >> k;
int cnt = upper_bound(res + 1, res + n + 1, k) - res - 1;
if(cnt < 2) cout << 0 << '\n';
else cout << cnt << '\n';
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
// freopen(file".inp" , "r" , stdin);
// freopen(file".out" , "w" , stdout);
inp();
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
16212 KB |
Output is correct |
2 |
Correct |
11 ms |
16232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
16212 KB |
Output is correct |
2 |
Correct |
11 ms |
16232 KB |
Output is correct |
3 |
Correct |
39 ms |
18348 KB |
Output is correct |
4 |
Correct |
43 ms |
19012 KB |
Output is correct |
5 |
Correct |
42 ms |
18604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
1620 KB |
Output is correct |
2 |
Incorrect |
28 ms |
2716 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
16212 KB |
Output is correct |
2 |
Correct |
11 ms |
16232 KB |
Output is correct |
3 |
Correct |
39 ms |
18348 KB |
Output is correct |
4 |
Correct |
43 ms |
19012 KB |
Output is correct |
5 |
Correct |
42 ms |
18604 KB |
Output is correct |
6 |
Correct |
28 ms |
1620 KB |
Output is correct |
7 |
Incorrect |
28 ms |
2716 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |