#include<bits/stdc++.h>
using namespace std;
#define N 1005
#define int long long
#define oo 1e18
int dp[2][N][N], n, a[N], m, vs[N], siz[N], pre[N];
vector<int> adj[N];
void dfs(int u){
vs[u] = 1;
for (auto x : adj[u]){
if (!vs[x]) dfs(x);
}
}
void getdp(int u, int p){
siz[u] = 1;
dp[0][0][u] = 0;
for (auto x : adj[u]){
if (x == p) continue;
getdp(x, u);
for (int i = siz[u]; i >= 0; i--){
for (int j = siz[x]; j >= 0; j--){
dp[1][i + j + 2][u] = min(dp[1][i + j + 2][u], dp[0][i][u] + dp[0][j][x] + a[u] + a[x]);
dp[1][i + j][u] = min(dp[1][i + j][u], dp[1][i][u] + min(dp[0][j][x], dp[1][j][x]));
dp[1][i + j + 1][u] = min(dp[1][i + j + 1][u], dp[0][i][u] + a[u] + dp[1][j][x]);
dp[0][i + j][u] = min(dp[0][i + j][u], dp[0][i][u] + min(dp[1][j][x], dp[0][j][x]));
dp[1][i + j + 1][u] = min(dp[1][i + j + 1][u], dp[1][i][u] + a[x] + dp[0][j][x]);
}
}
siz[u] += siz[x];
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= m; i++){
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for (int i = 1; i <= n; i++){
if (!vs[i]){
dfs(i);
adj[0].push_back(i);
}
}
for (int i = 0; i <= n; i++){
for (int j = 0; j <= n; j++){
dp[0][i][j] = dp[1][i][j] = oo;
}
}
getdp(0, -1);
pre[n + 1] = oo;
for (int i = n; i >= 1; i--){
pre[i] = min(pre[i + 1], dp[0][i][0]);
}
int q;
cin >> q;
while (q--){
int k;
cin >> k;
int l = 1, r = n, rt = 0;
while (l <= r){
int mid = (l + r) >> 1;
if (pre[mid] <= k){
rt = mid;
l = mid + 1;
}
else r = mid - 1;
}
cout << rt << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
16224 KB |
Output is correct |
2 |
Correct |
14 ms |
16392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
16224 KB |
Output is correct |
2 |
Correct |
14 ms |
16392 KB |
Output is correct |
3 |
Correct |
36 ms |
18476 KB |
Output is correct |
4 |
Correct |
37 ms |
19028 KB |
Output is correct |
5 |
Correct |
43 ms |
18772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
3416 KB |
Output is correct |
2 |
Correct |
21 ms |
3160 KB |
Output is correct |
3 |
Correct |
23 ms |
3672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
16224 KB |
Output is correct |
2 |
Correct |
14 ms |
16392 KB |
Output is correct |
3 |
Correct |
36 ms |
18476 KB |
Output is correct |
4 |
Correct |
37 ms |
19028 KB |
Output is correct |
5 |
Correct |
43 ms |
18772 KB |
Output is correct |
6 |
Correct |
21 ms |
3416 KB |
Output is correct |
7 |
Correct |
21 ms |
3160 KB |
Output is correct |
8 |
Correct |
23 ms |
3672 KB |
Output is correct |
9 |
Correct |
33 ms |
18256 KB |
Output is correct |
10 |
Correct |
33 ms |
19028 KB |
Output is correct |
11 |
Correct |
34 ms |
18628 KB |
Output is correct |