#include <bits/stdc++.h>
#pragma GCC optimize ("O2")
#pragma GCC optimize ("unroll-loops")
//#pragma GCC optimize("no-stack-protector,fast-math")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) {cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;}
#define all(x) x.begin(), x.end()
#define pb push_back
#define upd(x, y) x=min(x, y)
const ld eps=1e-7;
const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod = 1000000007;
const int MAXN = 3010, LOG=20;
int n, m, k, u, v, x, y, t, a, b;
int A[MAXN];
int dp[MAXN][MAXN][3];
int dpp[MAXN][3];
int ans[MAXN], anss[MAXN];
int sz[MAXN];
vector<int> G[MAXN];
int f(int i, int j){
if (i==1) return 1+(j>0);
return i;
}
void dfs(int node, int par){
sz[node]=1;
dp[node][0][0]=0;
dp[node][0][1]=A[node];
for (int v:G[node]) if (v!=par){
dfs(v, node);
for (int i=0; i<=sz[node]; i++)
for (int ii:{0, 1, 2}) dpp[i][ii]=dp[node][i][ii], dp[node][i][ii]=inf;
for (int i=0; i<=sz[node]; i++)
for (int ii:{0, 1, 2})
for (int j=0; j<=sz[v]; j++)
for (int jj:{0, 1, 2})
upd(dp[node][i+j+(ii==1 && jj>0)+(ii>0 && jj==1)][f(ii, jj)], dpp[i][ii] + dp[v][j][jj]);
sz[node]+=sz[v];
}
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
memset(dp, 63, sizeof(dp));
memset(ans, 63, sizeof(ans));
cin>>n>>m;
for (int i=1; i<=n; i++) cin>>A[i];
while (m--){
cin>>u>>v;
G[u].pb(v);
G[v].pb(u);
}
int sum=0;
ans[0]=0;
for (int v=1; v<=n; v++) if (!sz[v]){
dfs(v, v);
for (int i=0; i<=sum; i++) anss[i]=ans[i], ans[i]=inf;
for (int j=0; j<=sz[v]; j++){
int mn=min({dp[v][j][0], dp[v][j][1], dp[v][j][2]});
for (int i=0; i<=sum; i++) upd(ans[i+j], anss[i] + mn);
}
sum+=sz[v];
}
for (int i=n; i; i--) upd(ans[i-1], ans[i]);
cin>>m;
while (m--){
cin>>x;
int res=upper_bound(ans, ans+n+1, x)-ans-1;
cout<<res<<'\n';
}
return 0;
}
/*
3 2
1 2 3
1 2
1 3
3
2
3
5
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
106872 KB |
Output is correct |
2 |
Correct |
78 ms |
107000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
106872 KB |
Output is correct |
2 |
Correct |
78 ms |
107000 KB |
Output is correct |
3 |
Correct |
121 ms |
109176 KB |
Output is correct |
4 |
Correct |
126 ms |
109816 KB |
Output is correct |
5 |
Correct |
128 ms |
109432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
104 ms |
108920 KB |
Output is correct |
2 |
Correct |
108 ms |
108632 KB |
Output is correct |
3 |
Correct |
113 ms |
109304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
106872 KB |
Output is correct |
2 |
Correct |
78 ms |
107000 KB |
Output is correct |
3 |
Correct |
121 ms |
109176 KB |
Output is correct |
4 |
Correct |
126 ms |
109816 KB |
Output is correct |
5 |
Correct |
128 ms |
109432 KB |
Output is correct |
6 |
Correct |
104 ms |
108920 KB |
Output is correct |
7 |
Correct |
108 ms |
108632 KB |
Output is correct |
8 |
Correct |
113 ms |
109304 KB |
Output is correct |
9 |
Correct |
108 ms |
108920 KB |
Output is correct |
10 |
Correct |
118 ms |
109688 KB |
Output is correct |
11 |
Correct |
116 ms |
109304 KB |
Output is correct |