#include<bits/stdc++.h>
#define taskname ""
#define el '\n'
#define fi first
#define sc second
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
#define int ll
using namespace std;
using ll=long long;
using ull=unsigned long long;
using ld=long double;
#define Faster ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int maxn=1e3+2;
const int mod=1e9+7;
int n,m,dp[2][maxn][maxn],vis[maxn],sz[maxn],d[maxn],q;
vector<int> adj[maxn];
void trav(int x,int par)
{
vis[x]=1;
for(int y:adj[x])
{
if(y!=par) trav(y,x);
}
}
void dfs(int x,int par)
{
sz[x]=1;
for(int y:adj[x])
{
if(y==par) continue;
dfs(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];
}
}
signed main()
{
if (fopen(taskname".INP","r"))
{
freopen(taskname".INP","r",stdin);
freopen(taskname".OUT","w",stdout);
}
Faster
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>d[i];
for(int i=1;i<=m;i++)
{
int a,b;
cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for(int i=1;i<=n;i++)
{
if(!vis[i])
{
adj[0].push_back(i);
trav(i,0);
}
}
for(int i=0;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
dp[0][i][j]=dp[1][i][j]=mod;
}
dp[1][i][0]=mod;
}
dfs(0,0);
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";
}
// for(int i=1;i<=n;i++)
// {
//// for(int j=1;j<=n;j++)
// cout<<dp[0][0][i]<<" ";
// }
}
Compilation message
dzumbus.cpp: In function 'int main()':
dzumbus.cpp:52:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
52 | freopen(taskname".INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
dzumbus.cpp:53:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
53 | freopen(taskname".OUT","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
16220 KB |
Output is correct |
2 |
Correct |
8 ms |
16216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
16220 KB |
Output is correct |
2 |
Correct |
8 ms |
16216 KB |
Output is correct |
3 |
Correct |
151 ms |
18468 KB |
Output is correct |
4 |
Correct |
157 ms |
18976 KB |
Output is correct |
5 |
Correct |
155 ms |
18508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
7504 KB |
Output is correct |
2 |
Correct |
38 ms |
8584 KB |
Output is correct |
3 |
Correct |
41 ms |
9040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
16220 KB |
Output is correct |
2 |
Correct |
8 ms |
16216 KB |
Output is correct |
3 |
Correct |
151 ms |
18468 KB |
Output is correct |
4 |
Correct |
157 ms |
18976 KB |
Output is correct |
5 |
Correct |
155 ms |
18508 KB |
Output is correct |
6 |
Correct |
38 ms |
7504 KB |
Output is correct |
7 |
Correct |
38 ms |
8584 KB |
Output is correct |
8 |
Correct |
41 ms |
9040 KB |
Output is correct |
9 |
Correct |
150 ms |
18312 KB |
Output is correct |
10 |
Correct |
156 ms |
18772 KB |
Output is correct |
11 |
Correct |
156 ms |
18720 KB |
Output is correct |