#include <bits/stdc++.h>
#define int long long
using namespace std;
const int inf = 1e9;
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";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
19032 KB |
Output is correct |
2 |
Correct |
7 ms |
19036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
19032 KB |
Output is correct |
2 |
Correct |
7 ms |
19036 KB |
Output is correct |
3 |
Correct |
157 ms |
20052 KB |
Output is correct |
4 |
Correct |
180 ms |
20440 KB |
Output is correct |
5 |
Correct |
183 ms |
19648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
5448 KB |
Output is correct |
2 |
Correct |
39 ms |
5204 KB |
Output is correct |
3 |
Incorrect |
42 ms |
4948 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
19032 KB |
Output is correct |
2 |
Correct |
7 ms |
19036 KB |
Output is correct |
3 |
Correct |
157 ms |
20052 KB |
Output is correct |
4 |
Correct |
180 ms |
20440 KB |
Output is correct |
5 |
Correct |
183 ms |
19648 KB |
Output is correct |
6 |
Correct |
40 ms |
5448 KB |
Output is correct |
7 |
Correct |
39 ms |
5204 KB |
Output is correct |
8 |
Incorrect |
42 ms |
4948 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |