답안 #557983

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
557983 2022-05-06T12:27:14 Z stefantaga Džumbus (COCI19_dzumbus) C++14
110 / 110
67 ms 21468 KB
#include <bits/stdc++.h>
using namespace std;

#define TRACE(x) cerr << #x << " = " << x << endl
#define _ << " _ " <<

typedef long long ll;
typedef long double ld;

const int MAXN = 1010, MAXQ = 2e5 + 10;
const ll INF = 1e15;

int n, m, q;
ll c[MAXN];
vector<int> e[MAXN];
ll dp_in[MAXN][MAXN], dp_out[MAXN][MAXN];
int subtree_size[MAXN];

pair<int, int> query[MAXQ];
int ans[MAXQ];


ll prev_dp_in[MAXN], prev_dp_out[MAXN];

void calc(int i, int parent = -1) {
    subtree_size[i] = 1;
    if (i != n) {
        for (int j : e[i]) {
            if (j != parent) {
                calc(j, i);
                subtree_size[i] += subtree_size[j];
            }
        }
    }

    for (int k = 0; k <= n; k++)
        dp_in[i][k] = dp_out[i][k] = INF;
    dp_out[i][0] = 0;

    int total = 0;
    prev_dp_in[0] = INF;
    prev_dp_out[0] = 0;

    for (int j : e[i]) {
        if (j == parent) continue;

        for (int k = 0; k <= total + subtree_size[j]; k++)
            dp_in[i][k] = dp_out[i][k] = INF;

        for (int a = 0; a <= total; a++) {
            for (int b = 0; b <= subtree_size[j]; b++) {
                dp_out[i][a + b] = min(dp_out[i][a + b],
                        prev_dp_out[a] + min(dp_in[j][b], dp_out[j][b]));

                dp_in[i][a + b] = min(dp_in[i][a + b],
                        min(prev_dp_out[a] + dp_in[j][b],
                            prev_dp_in[a] + min(dp_in[j][b], dp_out[j][b])));
                if (b > 0)
                    dp_in[i][a + b] = min(dp_in[i][a + b],
                            min(prev_dp_out[a], prev_dp_in[a]) + dp_out[j][b - 1] + c[j]);
            }
        }

        total += subtree_size[j];
        for (int k = 0; k <= total; k++) {
            prev_dp_in[k] = dp_in[i][k];
            prev_dp_out[k] = dp_out[i][k];
        }
    }

    for (int k = total; k >= 0; k--)
        dp_in[i][k + 1] = min(INF, dp_in[i][k] + c[i]);
    dp_in[i][0] = INF;
}


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        cin >> c[i];
        dp_in[i][0] = -1;
    }
    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        e[a].push_back(b);
        e[b].push_back(a);
    }

    for (int i = 0; i < n; i++) {
        if (dp_in[i][0] == -1) {
            calc(i);
            e[n].push_back(i);
        }
    }

    c[n] = INF;
    calc(n);
    for (int i = n - 1; i >= 0; i--)
        dp_out[n][i] = min(dp_out[n][i], dp_out[n][i + 1]);

    cin >> q;
    for (int i = 0; i < q; i++) {
        cin >> query[i].first;
        query[i].second = i;
    }
    sort(query, query + q);
    int answer = 0;
    for (int i = 0; i < q; i++) {
        while (answer < n && dp_out[n][answer + 1] <= query[i].first)
            answer++;
        ans[query[i].second] = answer;
    }
    for (int i = 0; i < q; i++)
        cout << ans[i] << "\n";

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 16372 KB Output is correct
2 Correct 12 ms 16340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 16372 KB Output is correct
2 Correct 12 ms 16340 KB Output is correct
3 Correct 51 ms 20868 KB Output is correct
4 Correct 67 ms 21468 KB Output is correct
5 Correct 50 ms 20972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 54 ms 5740 KB Output is correct
2 Correct 37 ms 5552 KB Output is correct
3 Correct 40 ms 5932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 16372 KB Output is correct
2 Correct 12 ms 16340 KB Output is correct
3 Correct 51 ms 20868 KB Output is correct
4 Correct 67 ms 21468 KB Output is correct
5 Correct 50 ms 20972 KB Output is correct
6 Correct 54 ms 5740 KB Output is correct
7 Correct 37 ms 5552 KB Output is correct
8 Correct 40 ms 5932 KB Output is correct
9 Correct 50 ms 20536 KB Output is correct
10 Correct 54 ms 21272 KB Output is correct
11 Correct 60 ms 21068 KB Output is correct