Submission #1032113

# Submission time Handle Problem Language Result Execution time Memory
1032113 2024-07-23T12:00:03 Z Whisper Džumbus (COCI19_dzumbus) C++17
110 / 110
433 ms 22620 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (b); i >= (a); i --)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define REPD(i, a) for (int i = (a) - 1; i >= 0; --i)

#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)


constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

/*
    Phu Trong from Nguyen Tat Thanh High School for gifted student
*/

template <class X, class Y>
    bool minimize(X &x, const Y &y){
        X eps = 1e-9;
        if (x > y + eps) {x = y; return 1;}
        return 0;
    }

template <class X, class Y>
    bool maximize(X &x, const Y &y){
        X eps = 1e-9;
        if (x + eps < y) {x = y; return 1;}
        return 0;
    }
#define MAX         1111
int numNode, numEdge, numQuery;

vector<int> G[MAX];
int label[MAX];

int dp[MAX][MAX][2], sz[MAX];
int vis[MAX];
void pre_dfs(int u){
    vis[u] = 1;
    for (int &v : G[u]) if(!vis[v]) pre_dfs(v);
}
void dfs(int u, int p = -1){
    sz[u] = 1;
    dp[u][0][0] = 0;
    for (int &v : G[u]) if(v ^ p){
        dfs(v, u);
        sz[u] += sz[v];
        FORD(i, 0, sz[u]) FORD(j, 0, min(i, sz[v])){
            minimize(dp[u][i][0], dp[u][i - j][0] + min(dp[v][j][1], dp[v][j][0]));

            minimize(dp[u][i][1], dp[u][i - j][1] + min(dp[v][j][1], dp[v][j][0]));
            if (i - j >= 1) //connect u(off) and v
                minimize(dp[u][i][1], dp[u][i - j - 1][0] + dp[v][j][1] + label[u]);
            if (i - j >= 1 && j) // connect u and v (both off)
                minimize(dp[u][i][1], dp[u][i - j - 1][0] + dp[v][j - 1][0] + label[u] + label[v]);
            if (j) // connect u and v(off)
                minimize(dp[u][i][1], dp[u][i - j][1] + dp[v][j - 1][0] + label[v]);
        }
    }
}
void process(void){
    cin >> numNode >> numEdge;
    for (int i = 1; i <= numNode; ++i) cin >> label[i];
    label[0] = INF;
    for (int i = 1; i <= numEdge; ++i){
        int u, v; cin >> u >> v;
        G[u].emplace_back(v);
        G[v].emplace_back(u);
    }

    for (int i = 1; i <= numNode; ++i){
        if(!vis[i]){
            G[0].emplace_back(i);
            G[i].emplace_back(0);
            pre_dfs(i);
        }
    }

    memset(dp, 0x3f, sizeof dp);
    dfs(0);

    vector<int> res;
    for (int i = numNode; i >= 0; --i){
        if(res.empty()) res.emplace_back(min(dp[0][i][0], dp[0][i][1]));
        else res.emplace_back(min({dp[0][i][0], dp[0][i][1], res.back()}));
    }
    reverse(res.begin(), res.end());
    cin >> numQuery;
    for (int i = 1; i <= numQuery; ++i){
        int x; cin >> x;
        auto pos = upper_bound(res.begin(), res.end(), x) - res.begin() - 1;
        cout << pos << '\n';
    }
}
signed main(){
    #define name "Whisper"
    cin.tie(nullptr) -> sync_with_stdio(false);
    //freopen(name".inp", "r", stdin);
    //freopen(name".out", "w", stdout);
    process();
    return (0 ^ 0);
}




# Verdict Execution time Memory Grader output
1 Correct 229 ms 19800 KB Output is correct
2 Correct 311 ms 19800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 229 ms 19800 KB Output is correct
2 Correct 311 ms 19800 KB Output is correct
3 Correct 433 ms 22016 KB Output is correct
4 Correct 260 ms 22612 KB Output is correct
5 Correct 205 ms 22324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 21844 KB Output is correct
2 Correct 28 ms 21588 KB Output is correct
3 Correct 30 ms 22096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 229 ms 19800 KB Output is correct
2 Correct 311 ms 19800 KB Output is correct
3 Correct 433 ms 22016 KB Output is correct
4 Correct 260 ms 22612 KB Output is correct
5 Correct 205 ms 22324 KB Output is correct
6 Correct 29 ms 21844 KB Output is correct
7 Correct 28 ms 21588 KB Output is correct
8 Correct 30 ms 22096 KB Output is correct
9 Correct 35 ms 22096 KB Output is correct
10 Correct 35 ms 22620 KB Output is correct
11 Correct 34 ms 22184 KB Output is correct