Submission #623950

# Submission time Handle Problem Language Result Execution time Memory
623950 2022-08-07T04:09:58 Z Soul234 Džumbus (COCI19_dzumbus) C++14
110 / 110
67 ms 21472 KB
#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

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
1 Correct 9 ms 16212 KB Output is correct
2 Correct 9 ms 16212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 16212 KB Output is correct
2 Correct 9 ms 16212 KB Output is correct
3 Correct 47 ms 20896 KB Output is correct
4 Correct 67 ms 21472 KB Output is correct
5 Correct 49 ms 21036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 20600 KB Output is correct
2 Correct 40 ms 20464 KB Output is correct
3 Correct 53 ms 20924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 16212 KB Output is correct
2 Correct 9 ms 16212 KB Output is correct
3 Correct 47 ms 20896 KB Output is correct
4 Correct 67 ms 21472 KB Output is correct
5 Correct 49 ms 21036 KB Output is correct
6 Correct 41 ms 20600 KB Output is correct
7 Correct 40 ms 20464 KB Output is correct
8 Correct 53 ms 20924 KB Output is correct
9 Correct 43 ms 20676 KB Output is correct
10 Correct 48 ms 21348 KB Output is correct
11 Correct 48 ms 21100 KB Output is correct