답안 #1004270

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1004270 2024-06-21T07:04:15 Z TrinhKhanhDung Džumbus (COCI19_dzumbus) C++14
0 / 110
47 ms 18516 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];
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);
    cur[0][0] = 0;
    nChild[u] = 1;
    for(int v: adj[u]) if(v != p){
        for(int i=0; i<=nChild[u] + nChild[v]; 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);
    f[0][1][0] = 0;
    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(f[0][m][0] <= 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 12 ms 16728 KB Output is correct
2 Incorrect 10 ms 16476 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 16728 KB Output is correct
2 Incorrect 10 ms 16476 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 18516 KB Output is correct
2 Incorrect 26 ms 18132 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 16728 KB Output is correct
2 Incorrect 10 ms 16476 KB Output isn't correct
3 Halted 0 ms 0 KB -