#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define sz(a) (ll)a.size()
#define all(a) a.begin(),a.end()
#define rep(i,n) for(ll i=0;i<n;i++)
#define crep(i,x,n) for(ll i=x;i<n;i++)
#define drep(i,n) for(ll i=n-1;i>=0;i--)
#define vec(...) vector<__VA_ARGS__>
#define _3dDwnkq ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;
using pii=pair<ll,ll>;
using vi=vector<ll>;
const ll inf=1e12;
int main(){
_3dDwnkq;
ll n,m;
cin>>n>>m;
vi a(n);
rep(i,n){
cin>>a[i];
}
vec(vi) adj(n);
rep(i,m){
ll u,v;
cin>>u>>v;
u--,v--;
adj[u].pb(v);
adj[v].pb(u);
}
vi chs(n,0);
vi usd(n,0);
auto dfs=[&](auto self,ll v)->void{
chs[v]=1;
usd[v]=1;
for(auto u : adj[v]){
if(!usd[u]){
self(self,u);
chs[v]+=chs[u];
}
}
};
rep(v,n){
if(!usd[v]){
dfs(dfs,v);
}
}
usd=vi(n,0);
vec(vec(vi)) dp(n+1,vec(vi)(n+1,vi(2,1e9)));
auto magicaldfs=[&](auto self,ll v)->void{
usd[v]=1;
dp[v][0][0]=0;
ll now=1;
for(auto u : adj[v]){
if(!usd[u]){
self(self,u);
ll pnow=now;
drep(j,pnow+1){
drep(k,chs[u]+1){
ll nej=j+k;
dp[v][nej][0]=min({
dp[v][nej][0],
dp[v][j][0]+min(dp[u][k][1],dp[u][k][0])
});
if(nej+1<=n) dp[v][nej+1][1]=min({
dp[v][nej+1][1],
dp[v][j][1]+dp[u][k][0]+a[u],
dp[v][j][0]+dp[u][k][1]+a[v]
});
if(nej+2<=n) dp[v][nej+2][1]=min({
dp[v][nej+2][1],
dp[v][j][0]+dp[u][k][0]+a[v]+a[u]
});
dp[v][nej][1]=min({
dp[v][nej][1],
dp[v][j][1]+min(dp[u][k][1],dp[u][k][0])
});
}
}
now+=chs[u];
}
}
};
vi fdp(n+1,1e18);
fdp[0]=0;
ll now=0;
rep(v,n){
if(!usd[v]){
magicaldfs(magicaldfs,v);
drep(j,now+1){
if(fdp[j]==1e18) continue;
drep(nej,chs[v]+1){
fdp[nej+j]=min(fdp[nej+j],fdp[j]+min(dp[v][nej][0],dp[v][nej][1]));
}
}
now+=chs[v];
}
}
ll q;
cin>>q;
rep(_,q){
ll x;
cin>>x;
drep(i,n+1){
if(fdp[i]<=x){
cout<<i<<"\n";
break;
}
}
}
//
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
78 ms |
55464 KB |
Output is correct |
2 |
Correct |
79 ms |
55472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
78 ms |
55464 KB |
Output is correct |
2 |
Correct |
79 ms |
55472 KB |
Output is correct |
3 |
Correct |
124 ms |
56528 KB |
Output is correct |
4 |
Correct |
117 ms |
56320 KB |
Output is correct |
5 |
Correct |
181 ms |
55816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
1604 KB |
Output is correct |
2 |
Correct |
35 ms |
1356 KB |
Output is correct |
3 |
Incorrect |
45 ms |
1220 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
78 ms |
55464 KB |
Output is correct |
2 |
Correct |
79 ms |
55472 KB |
Output is correct |
3 |
Correct |
124 ms |
56528 KB |
Output is correct |
4 |
Correct |
117 ms |
56320 KB |
Output is correct |
5 |
Correct |
181 ms |
55816 KB |
Output is correct |
6 |
Correct |
33 ms |
1604 KB |
Output is correct |
7 |
Correct |
35 ms |
1356 KB |
Output is correct |
8 |
Incorrect |
45 ms |
1220 KB |
Output isn't correct |