답안 #1004292

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1004292 2024-06-21T07:16:37 Z TrinhKhanhDung Džumbus (COCI19_dzumbus) C++14
110 / 110
36 ms 19292 KB
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18
#define INF (ll)1e9
#define MOD (ll)(1e9 + 7)

using namespace std;

template<class T1, class T2>
    bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}

template<class T1, class T2>
    bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}

template<class T1, class T2>
    void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}

template<class T1, class T2>
    void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}

template<class T>
    void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}

const int MAX = 1e3 + 10;

int N, M, Q;
ll dzumbus[MAX], nChild[MAX];
ll f[MAX][MAX][2], pre[MAX][2], cur[MAX][2], ans[MAX];
vector<int> adj[MAX];
bool vis[MAX];

void findTPLT(int u, int p){
    vis[u] = true;
    for(int v: adj[u]) if(v != p){
        findTPLT(v, u);
    }
}

void dfs(int u, int p){
    for(int v: adj[u]) if(v != p){
        dfs(v, u);
    }
    memset(cur, 0x3f, sizeof cur);
    memset(pre, 0x3f, sizeof pre);
    cur[0][0] = 0;
    nChild[u] = 1;
    for(int v: adj[u]) if(v != p){
        for(int i=0; i<=nChild[u]; i++){
            for(int j=0; j<=1; j++){
                pre[i][j] = cur[i][j];
                cur[i][j] = oo;
            }
        }
        for(int i=nChild[u]; i>=0; i--) for(int j=nChild[v]; j>=0; j--){
            minimize(cur[i + j][0], pre[i][0] + min(f[v][j][0], f[v][j][1]));
            if(i > 0 && j > 0) minimize(cur[i + j][1], pre[i - 1][0] + dzumbus[u] + f[v][j - 1][0] + dzumbus[v]);
            if(i > 0) minimize(cur[i + j][1], pre[i - 1][0] + dzumbus[u] + f[v][j][1]);
            minimize(cur[i + j][1], pre[i][1] + min(f[v][j][1], f[v][j][0]));
            if(j > 0) minimize(cur[i + j][1], pre[i][1] + f[v][j - 1][0] + dzumbus[v]);
        }
        nChild[u] += nChild[v];
    }
    for(int i=0; i<=nChild[u]; i++){
        for(int j=0; j<=1; j++){
            f[u][i][j] = cur[i][j];
        }
    }
}

void solve(){
    cin >> N >> M;
    for(int i=1; i<=N; i++){
        cin >> dzumbus[i];
    }
    for(int i=1; i<=M; i++){
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    for(int i=1; i<=N; i++) if(!vis[i]){
        findTPLT(i, -1);
        adj[0].push_back(i);
        adj[i].push_back(0);
    }
    dzumbus[0] = oo;

    memset(f, 0x3f, sizeof f);
    dfs(0, -1);
    ans[N] = f[0][N][0];
    for(int i=N-1; i>=0; i--){
        ans[i] = f[0][i][0];
        minimize(ans[i], ans[i + 1]);
    }
    cin >> Q;
    while(Q--){
        int s;
        cin >> s;
        int l = 0, r = N;
        while(l < r){
            int m = (l + r) >> 1;
            if((l + r) & 1) m++;
            if(ans[m] <= s){
                l = m;
            }
            else{
                r = m - 1;
            }
        }
        if(r == 1) l--;
        cout << l << '\n';
    }
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
//    freopen("netw.inp", "r", stdin);
//    freopen("netw.out", "w", stdout);

    solve();

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 16472 KB Output is correct
2 Correct 9 ms 16476 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 16472 KB Output is correct
2 Correct 9 ms 16476 KB Output is correct
3 Correct 31 ms 18644 KB Output is correct
4 Correct 34 ms 19284 KB Output is correct
5 Correct 33 ms 18784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 17232 KB Output is correct
2 Correct 26 ms 16988 KB Output is correct
3 Correct 30 ms 18772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 16472 KB Output is correct
2 Correct 9 ms 16476 KB Output is correct
3 Correct 31 ms 18644 KB Output is correct
4 Correct 34 ms 19284 KB Output is correct
5 Correct 33 ms 18784 KB Output is correct
6 Correct 27 ms 17232 KB Output is correct
7 Correct 26 ms 16988 KB Output is correct
8 Correct 30 ms 18772 KB Output is correct
9 Correct 31 ms 18560 KB Output is correct
10 Correct 36 ms 19292 KB Output is correct
11 Correct 36 ms 18768 KB Output is correct