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>
using namespace std;
void DBG() { cerr << "]\n"; }
template<class H, class... T> void DBG(H h, T... t) {
cerr << h; if(sizeof...(t)) cerr << ", ";
DBG(t...);
}
#ifdef LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif // LOCAL
#define FOR(i,a,b) for(int i = (a) ; i<(b) ; i++)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for(int i = (b)-1 ; i>=(a) ; i--)
#define R0F(i,a) ROF(i,0,a)
#define each(e,a) for(auto &e : (a))
#define sz(v) (int)(v).size()
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define pb push_back
#define tcT template<class T
#define nl "\n"
#define fi first
#define se second
using ll = long long;
using vi = vector<int>;
using pi = pair<int,int>;
using str = string;
tcT> using V = vector<T>;
tcT> using pqg = priority_queue<T,vector<T>,greater<T>>;
void setIO(string NAME = "") {
cin.tie(0)->sync_with_stdio(0);
if(sz(NAME)) {
freopen((NAME + ".inp").c_str(),"r",stdin);
freopen((NAME + ".out").c_str(),"w",stdout);
}
}
tcT> bool ckmin(T&a, const T&b) {
return b < a ? a=b,1 : 0; }
tcT> bool ckmax(T&a, const T&b) {
return b > a ? a=b,1 : 0; }
const int MOD = 1e9 + 7;
const int MX = 1e3+5;
const int MX1 = 2e5+5;
const ll INF = 1e18;
int N, M, Q, siz[MX], ans[MX1];
ll D[MX];
vi adj[MX];
bool vis[MX];
ll dp[MX][MX][2];
V<pi> qry;
void dfsMark(int u) {
vis[u] = true;
each(v, adj[u]) if(!vis[v]) dfsMark(v);
}
void dfs(int u, int p = -1) {
dp[u][0][0] = 0;
dp[u][0][1] = INF;
each(v, adj[u]) if(v != p) {
dfs(v, u);
R0F(i, siz[u]+1)
R0F(j, siz[v]+1) {
ckmin(dp[u][i+j][0], dp[u][i][0] + min(dp[v][j][0], dp[v][j][1]));
ckmin(dp[u][i+j][1], dp[u][i][0] + dp[v][j][1]);
ckmin(dp[u][i+j][1], dp[u][i][1] + min(dp[v][j][0], dp[v][j][1]));
if(j>0)
ckmin(dp[u][i+j][1], min(dp[u][i][0], dp[u][i][1]) + dp[v][j-1][0] + D[v]);
}
siz[u] += siz[v];
}
R0F(i, siz[u]+1)
dp[u][i+1][1] = min(INF, dp[u][i][1] + D[u]);
dp[u][0][1] = INF;
siz[u]++;
}
void solve() {
cin>>N>>M;
FOR(i,1,N+1) cin>>D[i];
F0R(i,M) {
int u,v;
cin>>u>>v;
adj[u].pb(v);
adj[v].pb(u);
}
FOR(i,1,N+1) if(!vis[i]) {
dfsMark(i);
adj[N+1].pb(i);
}
memset(dp, 0x3f, sizeof dp);
D[N+1] = INF;
dfs(N+1);
R0F(j, N) ckmin(dp[N+1][j][0], dp[N+1][j+1][0]);
cin>>Q;
F0R(i,Q) {
int S; cin>>S;
qry.pb({S, i});
}
sort(all(qry));
int j = 0;
F0R(i,sz(qry)) {
while(j+1 <= N && dp[N+1][j+1][0] <= qry[i].first) j++;
ans[qry[i].second] = j;
}
F0R(i,Q) cout << ans[i] << nl;
}
int main() {
setIO();
int t=1;
//cin>>t;
while(t-->0) {
solve();
}
return 0;
}
Compilation message (stderr)
dzumbus.cpp: In function 'void setIO(std::string)':
dzumbus.cpp:39:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
39 | freopen((NAME + ".inp").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dzumbus.cpp:40:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | freopen((NAME + ".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |