답안 #785531

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
785531 2023-07-17T10:03:17 Z quochuy147 Džumbus (COCI19_dzumbus) C++14
50 / 110
48 ms 18940 KB
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define TIME  (1.0 * clock() / CLOCKS_PER_SEC)
#define file "name" 
template <typename T1, typename T2> bool minimize(T1 &a, T2 b){if (a > b) {a = b; return true;} return false;}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b){if (a < b) {a = b; return true;} return false;}

const int mod = 1e9 + 7;
const int N = 1e3 + 5;
const ll inf = 1e16;

int n, m, q, k;
int sz[N];
ll f[N][N][2], res[N], cost[N];
vector <int> a[N];

void inp()
{
    cin >> n >> m;
    for(int i = 1; i <= n; ++i) cin >> cost[i];
    for(int i = 1; i <= m; ++i) {
        int u, v;
        cin >> u >> v;
        a[u].pb(v);
        a[v].pb(u);
    }
}

void dfs(int u, int pre) {
    sz[u] = 1;
    for(int v : a[u]) {
        if(v == pre) continue;
        dfs(v, u);
        sz[u] += sz[v];
        for(int i = sz[u]; i >= 0; --i) {
            for(int j = max(0, i - sz[u] + sz[v]); j <= min(i, sz[v]); ++j) {
                minimize(f[u][i][0], f[u][i - j][0] + min(f[v][j][0], f[v][j][1]));
                minimize(f[u][i][1], f[u][i - j][1] + min(f[v][j][0], f[v][j][1]));
                if(j > 0) minimize(f[u][i][1], f[u][i - j][1] + f[v][j - 1][0] + cost[v]);
                if(i - j > 0) minimize(f[u][i][1], f[u][i - j - 1][0] + f[v][j][1] + cost[u]); 
                if(j > 0 && i - j > 0) minimize(f[u][i][1], f[u][i - j - 1][0] + f[v][j - 1][0] + cost[u] + cost[v]);
            }
        }
    }
}

void solve()
{
    for(int i = 1; i <= n; ++i)
        for(int j = 0; j <= n; ++j) {
            f[i][j][0] = f[i][j][1] = inf;
            if(j == 0) f[i][j][0] = 0;
        }
    dfs(1, 1);
    for(int i = 1; i <= n; ++i) res[i] = min(f[1][i][0], f[1][i][1]);  
    cin >> q;
    while(q--) {
        cin >> k;
        int cnt = upper_bound(res + 1, res + n + 1, k) - res - 1;
        if(cnt < 2) cout << 0 << '\n';
        else cout << cnt << '\n';
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    // freopen(file".inp" , "r" , stdin);
    // freopen(file".out" , "w" , stdout);
    inp();
    solve();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 16084 KB Output is correct
2 Correct 13 ms 16212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 16084 KB Output is correct
2 Correct 13 ms 16212 KB Output is correct
3 Correct 40 ms 18380 KB Output is correct
4 Correct 48 ms 18940 KB Output is correct
5 Correct 41 ms 18508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 1636 KB Output is correct
2 Incorrect 30 ms 2576 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 16084 KB Output is correct
2 Correct 13 ms 16212 KB Output is correct
3 Correct 40 ms 18380 KB Output is correct
4 Correct 48 ms 18940 KB Output is correct
5 Correct 41 ms 18508 KB Output is correct
6 Correct 27 ms 1636 KB Output is correct
7 Incorrect 30 ms 2576 KB Output isn't correct
8 Halted 0 ms 0 KB -