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>
#define int long long
using namespace std;
const int inf = 1e16;
const int N=1e3+100;
vector<int> adj[N+1];
int d[N+1],n,m;
bool avail[N+1];
int dp[2][N+1][N+1], sz[N+1];
void dfs(int u, int par)
{
avail[u]=1;
for(int v : adj[u])
{
if(v!=par) dfs(v, u);
}
}
void solve(int x,int par)
{
sz[x]=1;
for(int y:adj[x])
{
if(y==par) continue;
solve(y,x);
for(int i=sz[x];i>=0;i--)
{
for(int j=sz[y];j>=0;j--)
{
dp[0][x][i+j]=min({dp[0][x][i+j],dp[0][x][i]+dp[1][y][j],dp[0][x][i]+dp[0][y][j]});
if(i>0) dp[1][x][i+j]=min(dp[1][x][i+j],dp[0][x][i-1]+dp[1][y][j]+d[x]);
if(i>0&&j>0) dp[1][x][i+j]=min(dp[1][x][i+j],dp[0][x][i-1]+dp[0][y][j-1]+d[x]+d[y]);
if(j>0) dp[1][x][i+j]=min(dp[1][x][i+j],dp[1][x][i]+dp[0][y][j-1]+d[y]);
dp[1][x][i+j]=min({dp[1][x][i+j],dp[1][x][i]+dp[1][y][j],dp[1][x][i]+dp[0][y][j]});
}
}
sz[x]+=sz[y];
}
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
//freopen("cocktail.inp", "r", stdin);
//freopen("cocktail.out", "w", stdout);
int n,m;
cin >> n >> m;
for(int i=1;i<=n;++i) cin >> d[i];
for(int i=1;i<=m;++i)
{
int u,v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for(int i=1;i<=n;++i)
{
if(!avail[i])
{dfs(i, 0);
adj[0].push_back(i);}
}
// cout << "K";
for(int i=0;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
dp[0][i][j]=dp[1][i][j]=inf;
}
dp[1][i][0]=inf;
}
// cout << "K";
solve(0,0);
int q;
cin >> q;
while(q--)
{
int D;
cin>>D;
int ans=0;
for(int i=1;i<=n;i++)
{
if(dp[0][0][i]<=D) ans=i;
}
cout<<ans<<"\n";
}
}
# | 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... |