#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 merge(int u, int v) {
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]);
}
}
void dfs(int u, int pre) {
sz[u] = 1;
for(int v : a[u]) {
if(v == pre) continue;
dfs(v, u);
merge(u, 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()
{
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;
trace(i, i);
dfs(i, i);
if(i != 1) merge(1, i);
}
}
for(int i = 1; i <= n; ++i) res[i] = min(f[1][i][0], f[1][i][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 |
10 ms |
16084 KB |
Output is correct |
2 |
Correct |
12 ms |
16084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16084 KB |
Output is correct |
2 |
Correct |
12 ms |
16084 KB |
Output is correct |
3 |
Correct |
38 ms |
18268 KB |
Output is correct |
4 |
Correct |
48 ms |
18896 KB |
Output is correct |
5 |
Correct |
41 ms |
18544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
2928 KB |
Output is correct |
2 |
Incorrect |
28 ms |
2784 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16084 KB |
Output is correct |
2 |
Correct |
12 ms |
16084 KB |
Output is correct |
3 |
Correct |
38 ms |
18268 KB |
Output is correct |
4 |
Correct |
48 ms |
18896 KB |
Output is correct |
5 |
Correct |
41 ms |
18544 KB |
Output is correct |
6 |
Correct |
30 ms |
2928 KB |
Output is correct |
7 |
Incorrect |
28 ms |
2784 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |