This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |