#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];
vector<pair<int, int>> ans;
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++){
while(!ans.empty() && dp[0][i][0] <= ans.back().first) ans.pop_back();
ans.push_back({dp[0][i][0], i});
}
cin >> q;
while(q--){
int s; cin >> s;
cout << (*prev(upper_bound(ans.begin(), ans.end(), make_pair(s, MAXN+5)))).second << endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
20076 KB |
Output is correct |
2 |
Correct |
21 ms |
21356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
20076 KB |
Output is correct |
2 |
Correct |
21 ms |
21356 KB |
Output is correct |
3 |
Correct |
484 ms |
24392 KB |
Output is correct |
4 |
Correct |
491 ms |
22636 KB |
Output is correct |
5 |
Correct |
488 ms |
20716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
498 ms |
13548 KB |
Output is correct |
2 |
Correct |
509 ms |
12908 KB |
Output is correct |
3 |
Correct |
469 ms |
14700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
20076 KB |
Output is correct |
2 |
Correct |
21 ms |
21356 KB |
Output is correct |
3 |
Correct |
484 ms |
24392 KB |
Output is correct |
4 |
Correct |
491 ms |
22636 KB |
Output is correct |
5 |
Correct |
488 ms |
20716 KB |
Output is correct |
6 |
Correct |
498 ms |
13548 KB |
Output is correct |
7 |
Correct |
509 ms |
12908 KB |
Output is correct |
8 |
Correct |
469 ms |
14700 KB |
Output is correct |
9 |
Correct |
478 ms |
15212 KB |
Output is correct |
10 |
Correct |
491 ms |
15340 KB |
Output is correct |
11 |
Correct |
485 ms |
15084 KB |
Output is correct |