#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];
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);
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 (dp[0][mid][0] <= k){
rt = mid;
l = mid + 1;
}
else r = mid - 1;
}
cout << rt << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
16220 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
16220 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
20 ms |
3432 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
16220 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |