제출 #1346762

#제출 시각아이디문제언어결과실행 시간메모리
1346762fatelessDžumbus (COCI19_dzumbus)C++20
110 / 110
214 ms17104 KiB
// #pragma GCC optimize("O3,Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
 
#define speedup cin.tie(0)->sync_with_stdio(0);
#define bitcount __builtin_popcount
#define all(x) begin(x), end(x)
#define pb push_back
#define vc vector
#define vt pt

#define int long long

using ll = long long;
using pii = array<int, 2>;
const int N = 1e3 + 10, inf = 1e18;
static int ord[N], par[N], sz[N], c[N], f[N], id[N], pf[N];
inline void chmin(int &a, int b) {if (a > b) a = b;}

inline void solve() {
    int n, m;
    cin >> n >> m;
    vc<vc<int>> adj (n + 1);
    vc dp (n + 1, vc<pii> (n + 1));
    for (int i = 1; i <= n; i++) cin >> c[i];
    for (int i = 1; i <= m; i++) {
        int a, b; cin >> a >> b;
        adj[a].pb(b); adj[b].pb(a);
    }

    int cur = 0;
    auto dfs = [&](auto &&dfs, int a, int p) -> void {
        sz[a] = 1;
        for (int b : adj[a]) if (b != p) {
            dfs(dfs, b, a);
            par[b] = a;
        }
        ord[cur++] = a;
    };

    for (int i = 1; i <= n; i++) {
        if (par[i] == 0) {
            dfs(dfs, i, 0);
            adj[0].pb(i);
        }
    }

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++)
            dp[i][j][0] = dp[i][j][1] = inf;

        dp[i][0][1] = inf;
    }

    for (int i = 0; i < n; i++) {
        int a = ord[i];
        for (int b : adj[a]) {
            if (b == par[a]) continue;

            sz[a] += sz[b];
            for (int s = sz[a]; s >= 0; s--) {
                for (int x = 0; x <= min(sz[b], s); x++) {
                    chmin(dp[a][s][0], dp[a][s - x][0] + dp[b][x][0]);
                    chmin(dp[a][s][0], dp[a][s - x][0] + dp[b][x][1]);
                    chmin(dp[a][s][1], dp[a][s - x][1] + dp[b][x][0]);
                    chmin(dp[a][s][1], dp[a][s - x][1] + dp[b][x][1]);

                    if (s - x - 1 >= 0) {
                        chmin(dp[a][s][1], dp[a][s - x - 1][0] + dp[b][x][1] + c[a]);
                        chmin(dp[a][s][1], dp[a][s - x - 1][1] + dp[b][x][0] + c[b]);
                        if (s - x - 2 >= 0) chmin(dp[a][s][1], dp[a][s - x - 2][0] + dp[b][x][0] + c[a] + c[b]);
                    }
                }
            }
        }
    }

    int S = 0;
    for (int i = 1; i <= n; i++) f[i] = inf;
    for (int a : adj[0]) {
        S += sz[a];
        for (int s = S; s >= 0; s--) {
            for (int x = 0; x <= min(s, sz[a]); x++) {
                int val = min(dp[a][x][0], dp[a][x][1]);
                chmin(f[s], f[s - x] + val);
            }
        }
    }

    int R = n + 1;
    iota(id, id + R, 0);
    sort(id, id + R, [&](int l, int r) {
        return (f[l] < f[r]);
    });
    pf[0] = id[0];
    for (int i = 1; i < R; i++)
        pf[i] = max(pf[i - 1], id[i]);

    int q;
    cin >> q;
    while (q--) {
        int x;
        cin >> x;
        int l = 0, r = R;
        while (r - l > 1) {
            int mid = (l + r) >> 1;
            if (f[id[mid]] <= x) l = mid;
            else r = mid;
        }

        cout << pf[l] << ' ';
    }
}

signed main() {
    speedup;
    solve();

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...