답안 #253685

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
253685 2020-07-28T14:05:10 Z egekabas Džumbus (COCI19_dzumbus) C++14
50 / 110
1000 ms 18552 KB
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef pair<ll, ll> pii;
typedef pair<ld, ld> pld;
const ll inf = 1e18;
ll n, m;
ll val[1009];
ll dp[1009][1009][2];
ll vis[1009];
ll sz[1009];
vector<ll> g[1009];
void calcsz(ll v, ll prt){
    sz[v] = 1;
    for(auto u : g[v])
        if(u != prt){
            calcsz(u, v);
            sz[v] += sz[u];
        }
}
void dfs(ll v, ll prt){
    vis[v] = 1;
    for(ll i = 1; i <= n; ++i)
        dp[v][i][0] = dp[v][i][1] = inf;
    dp[v][0][1] = inf;
    for(auto u : g[v])
        if(u != prt)
            dfs(u, v);
    for(ll j = sz[prt]; j >= 0; --j)
        for(ll i = 0; i+j<=sz[prt] && i <= sz[v]; ++i){
            dp[prt][j+i][0] = min(dp[prt][j+i][0], dp[prt][j][0]+min(dp[v][i][0], dp[v][i][1]));
            dp[prt][j+i][1] = min(dp[prt][j+i][1], dp[prt][j][1]+min(dp[v][i][0], dp[v][i][1]));
            dp[prt][j+i+2][1] = min(dp[prt][j+i+2][1], dp[prt][j][0]+dp[v][i][0] + val[v] + val[prt]);
            dp[prt][j+i+1][1] = min(dp[prt][j+i+1][1], dp[prt][j][0]+dp[v][i][1] + val[prt]);
            dp[prt][i+j+1][1] = min(dp[prt][i+j+1][1], dp[prt][j][1]+dp[v][i][0]+val[v]);
        }
}
ll ans[1009];
int main(){
    
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);

    cin >> n >> m;
    for(ll i = 1; i <= n; ++i)
        cin >> val[i];
    for(ll i = 0; i < m; ++i){
        ll x, y;
        cin >> x >> y;
        g[x].pb(y);
        g[y].pb(x);
    }

    for(ll i = 1; i <= n; ++i) ans[i] = 1e18;
    for(ll v = 1; v <= n; ++v){
        if(vis[v]) continue;
        calcsz(v, 0);
        dfs(v, 0);
        for(ll j = n; j >= 0; --j)
            for(ll i = 0; i+j <= n && i <= sz[v]; ++i)
                ans[i+j] = min(ans[i+j], ans[j]+min(dp[v][i][0], dp[v][i][1]));
    }
    for(ll i = n-1; i >= 0; --i){
        ans[i] = min(ans[i], ans[i+1]);
    }
    ll q;
    cin >> q;
    while(q--){
        ll x;
        cin >> x;
        ll l = 0, r = n;
        while(l < r){
            ll mid = (l+r+1)/2;
            if(ans[mid] > x)
                r = mid-1;
            else
                l = mid;
        }
        cout << l << '\n';
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 340 ms 16248 KB Output is correct
2 Correct 483 ms 16376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 340 ms 16248 KB Output is correct
2 Correct 483 ms 16376 KB Output is correct
3 Execution timed out 1048 ms 18552 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 497 ms 3064 KB Output is correct
2 Correct 488 ms 2808 KB Output is correct
3 Correct 529 ms 3576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 340 ms 16248 KB Output is correct
2 Correct 483 ms 16376 KB Output is correct
3 Execution timed out 1048 ms 18552 KB Time limit exceeded
4 Halted 0 ms 0 KB -