답안 #847055

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
847055 2023-09-09T05:11:15 Z Cookie Džumbus (COCI19_dzumbus) C++14
110 / 110
549 ms 19196 KB
#include<bits/stdc++.h>
#define ll long long
#define vt vector
#define pb push_back
#define pii pair<int, int>
#define sz(v) (int)v.size()
#define fi first
#define se second
using namespace std;
const ll base = 107, mod = 1e9 + 7, mxv = 1e4 + 5, inf = 1e15;
const int mxn = 1e3 + 5, N = 3e6 + 5;
int n, m;
ll d[mxn + 1];
ll dp[mxn + 1][mxn + 1][2];
bool vis[mxn + 1];
int sz[mxn + 1];
vt<int>adj[mxn + 1];
void dfs2(int s){
    vis[s] = 1;
    for(auto i: adj[s]){
        if(!vis[i]){
            dfs2(i);
        }
    }
}
void ckmin(ll &a, ll b){
    a = min(a, b);
}
void dfs(int s, int pre){
    dp[s][0][0] = 0;
    sz[s] = 1;
    for(auto i: adj[s]){
        if(i != pre){
            dfs(i, s);
            for(int j = sz[s] + sz[i]; j >= 0; j--){
                for(int k = 0; k <= min(sz[i], j); k++){
                    ckmin(dp[s][j][0], dp[s][j - k][0] + min(dp[i][k][1], dp[i][k][0]));
                }
                for(int k = 0; k <= min(sz[i], j); k++){
                    ckmin(dp[s][j][1], dp[s][j - k][1] + min(dp[i][k][0], dp[i][k][1]));
                    if(j - k >= 1){
                        // connect s (off) = i(on)
                        ckmin(dp[s][j][1], dp[s][j - k - 1][0] + d[s] + dp[i][k][1]);
                    }if(j - k >= 1 && k >= 1){
                        // connect s to i (both off)
                        
                        ckmin(dp[s][j][1], dp[s][j - k - 1][0] + d[s] + d[i] + dp[i][k - 1][0]);
                        
                    }if(k >= 1){
                        // connect active s to off i 
                        ckmin(dp[s][j][1], dp[s][j - k][1] + d[i] + dp[i][k - 1][0]);
                    }
                }
            }
            sz[s] += sz[i];
        }
    }
}
int main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n >> m;
    d[0] = inf;
    for(int i = 1; i <= n; i++)cin >> d[i];
    for(int i = 0; i <= n; i++){
        for(int j = 0; j <= n; j++){
            dp[i][j][0] = dp[i][j][1] = inf;
        }
    }
    for(int i = 0; i < m; i++){
        int u, v; cin >> u >> v;
        adj[u].pb(v); adj[v].pb(u);
    }
    
    for(int i = 1; i <= n; i++){
        if(!vis[i]){
            dfs2(i);
            adj[0].pb(i);
        }
    }
    dfs(0, -1);
    int q; cin >> q;
    vt<ll>comp; 
    for(int i = n; i >= 0; i--){
        if(!sz(comp))comp.pb(min(dp[0][i][0], dp[0][i][1]));
        else comp.pb(min(comp.back(), (min(dp[0][i][0], dp[0][i][1]))));
    }
   reverse(comp.begin(), comp.end());
    while(q--){
        ll s; cin >> s;
        int id = upper_bound(comp.begin(), comp.end(), s) - comp.begin() - 1;
        cout << id << "\n";
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 317 ms 16220 KB Output is correct
2 Correct 429 ms 16384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 317 ms 16220 KB Output is correct
2 Correct 429 ms 16384 KB Output is correct
3 Correct 549 ms 18472 KB Output is correct
4 Correct 345 ms 19196 KB Output is correct
5 Correct 274 ms 18752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 3160 KB Output is correct
2 Correct 28 ms 4444 KB Output is correct
3 Correct 30 ms 4688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 317 ms 16220 KB Output is correct
2 Correct 429 ms 16384 KB Output is correct
3 Correct 549 ms 18472 KB Output is correct
4 Correct 345 ms 19196 KB Output is correct
5 Correct 274 ms 18752 KB Output is correct
6 Correct 28 ms 3160 KB Output is correct
7 Correct 28 ms 4444 KB Output is correct
8 Correct 30 ms 4688 KB Output is correct
9 Correct 41 ms 18256 KB Output is correct
10 Correct 38 ms 19028 KB Output is correct
11 Correct 38 ms 18772 KB Output is correct