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 F first
#define S second
#define int long long
#define pb push_back
using namespace std;
const int N=1e6,M=2e3,mod=1e9+7;
int dp[M][M][2][2],a[N];
vector<int>adj[N];
void dfs(int node,int par){
for(auto x:adj[node]){
if(x==par)
continue;
for(int j=0;j<=1000;j++){
dp[x][j][0][0]=min(dp[x][j][0][0],dp[node][j][0][0]);
dp[x][j][1][0]=min(dp[x][j][1][0],dp[node][j][0][0]+a[x]);
dp[x][j][1][0]=min(dp[x][j][1][0],dp[node][j][0][1]+a[x]);
dp[x][j][0][0]=min(dp[x][j][0][0],dp[node][j][0][1]);
dp[x][j][0][1]=min(dp[x][j][0][1],dp[node][j][1][1]);
if(j>0)
dp[x][j][1][1]=min(dp[x][j][1][1],dp[node][j-1][1][1]+a[x]);
dp[x][j][0][1]=min(dp[x][j][0][1],dp[node][j][1][0]);
if(j>1)
dp[x][j][1][1]=min(dp[x][j][1][1],dp[node][j-2][1][0]+a[x]);
}
dfs(x,node);
}
}
main(){
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=0;i<m;i++){
int x,y;
cin>>x>>y;
adj[x].pb(y);
adj[y].pb(x);
}
int node=1,par=0;
for(int i=0;i<=1000;i++)
for(int j=0;j<=1000;j++)
for(int k=0;k<2;k++)
for(int kk=0;kk<2;kk++)
dp[i][j][k][kk]=1e18;
for(int i=1;i<=n;i++)
if(adj[i].size()==1)
node=i;
dp[node][0][0][0]=0;
dp[node][0][1][0]=a[node];
dfs(node,0);
int node2;
for(int i=1;i<=n;i++)
if(adj[i].size()==1&&node!=i)
node2=i;
int q;
cin>>q;
while(q--){
int val;
cin>>val;
int ans=-1e18;
for(int i=0;i<=1000;i++){
for(int j=0;j<2;j++)
for(int k=0;k<2;k++)
if(dp[node2][i][j][k]<=val)
ans=max(ans,i);
}
cout<<ans<<endl;
}
return 0;
}
Compilation message (stderr)
dzumbus.cpp:41:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
41 | main(){
| ^~~~
dzumbus.cpp: In function 'int main()':
dzumbus.cpp:60:16: warning: unused variable 'par' [-Wunused-variable]
60 | int node=1,par=0;
| ^~~
dzumbus.cpp:77:9: warning: 'node2' may be used uninitialized in this function [-Wmaybe-uninitialized]
77 | int node2;
| ^~~~~
# | 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... |