#include <bits/stdc++.h>
using namespace std;
#define MAXN 1005
#define INF 1061109567
int ckmin(int &a, int b){return (b<a)?a=b:a;}
int ckmax(int &a, int b){return (b>a)?a=b:b;}
int n, m, q;
int arr[MAXN];
vector<int> adj[MAXN];
int dp[MAXN][MAXN][3]; //0: no root, 1: yes root but not engaged, 2: yes root and engaged
bool visited[MAXN];
int ans[MAXN];
void findcomp(int v, int p){
visited[v] = 1;
for(auto u : adj[v]){
if(u == p) continue;
findcomp(u, v);
}
}
int dfs(int v, int p){
dp[v][0][0] = 0;
dp[v][0][1] = arr[v];
int cursiz = 1;
for(auto u : adj[v]){
if(u == p) continue;
int siz = dfs(u, v);
int tmp[MAXN][3]; memset(tmp, 63, sizeof tmp);
for(int i = 0; i<=cursiz; i++){
for(int j = 0; j<=siz; j++){
ckmin(tmp[i+j][0], dp[v][i][0]+min({dp[u][j][0], dp[u][j][1], dp[u][j][2]}));
ckmin(tmp[i+j][1], dp[v][i][1]+dp[u][j][0]);
ckmin(tmp[i+j][2], dp[v][i][2]+min(dp[u][j][0], dp[u][j][2]));
ckmin(tmp[i+j+1][2], dp[v][i][2]+dp[u][j][1]);
ckmin(tmp[i+j+1][2], dp[v][i][1]+dp[u][j][2]);
ckmin(tmp[i+j+2][2], dp[v][i][1]+dp[u][j][1]);
}
}
for(int i = 0; i<MAXN; i++) dp[v][i][0] = tmp[i][0], dp[v][i][1] = tmp[i][1], dp[v][i][2] = tmp[i][2];
cursiz += siz;
}
return cursiz;
}
signed main(){
// freopen("a.in", "r", stdin);
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for(int i = 1; i<=n; i++) cin >> arr[i];
arr[0] = INF;
for(int i = 0; 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(!visited[i]){
findcomp(i, 0);
adj[0].push_back(i);
adj[i].push_back(0);
}
}
memset(dp, 0x3f, sizeof dp);
dfs(0, -1);
for(int i = 0; i<MAXN; i++) ans[i] = dp[0][i][0];
cin >> q;
while(q--){
int s; cin >> s;
if(ans[2] > s) cout << 0 << endl;
else cout << (upper_bound(ans+2, ans+MAXN, s)-ans)-1 << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
20076 KB |
Output is correct |
2 |
Incorrect |
22 ms |
21356 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
20076 KB |
Output is correct |
2 |
Incorrect |
22 ms |
21356 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
475 ms |
13548 KB |
Output is correct |
2 |
Incorrect |
473 ms |
14188 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
20076 KB |
Output is correct |
2 |
Incorrect |
22 ms |
21356 KB |
Output isn't correct |