#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]==inf) continue;
drep(nej,chs[v]+1){
fdp[nej+j]=min(fdp[nej+j],fdp[j]+min(dp[v][nej][0],dp[v][nej][1]));
}
}
}
}
ll q;
cin>>q;
rep(_,q){
ll x;
cin>>x;
drep(i,n+1){
if(fdp[i]<=x){
cout<<i<<"\n";
break;
}
}
}
//
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
55380 KB |
Output is correct |
2 |
Correct |
68 ms |
55452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
55380 KB |
Output is correct |
2 |
Correct |
68 ms |
55452 KB |
Output is correct |
3 |
Correct |
116 ms |
56292 KB |
Output is correct |
4 |
Correct |
118 ms |
56508 KB |
Output is correct |
5 |
Correct |
177 ms |
55896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
1660 KB |
Output is correct |
2 |
Incorrect |
40 ms |
1364 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
55380 KB |
Output is correct |
2 |
Correct |
68 ms |
55452 KB |
Output is correct |
3 |
Correct |
116 ms |
56292 KB |
Output is correct |
4 |
Correct |
118 ms |
56508 KB |
Output is correct |
5 |
Correct |
177 ms |
55896 KB |
Output is correct |
6 |
Correct |
31 ms |
1660 KB |
Output is correct |
7 |
Incorrect |
40 ms |
1364 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |