Submission #233787

# Submission time Handle Problem Language Result Execution time Memory
233787 2020-05-21T17:23:58 Z jungsnow Džumbus (COCI19_dzumbus) C++14
110 / 110
808 ms 19320 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 N = 1010, MAXQ = 2e5 + 10;
const ll INF = 1e15;
const int dummy = 0;

int n, m, q;
ll dp[N][N][2], D[N];
int numChild[N] ;
bool vis[N];
vector<int> adj[N];

void con(int par, int i) {
    vis[i] = 1;
    for (int v : adj[i]) if (v != par) {
        if (!vis[v]) {
            vis[v] = 1;
            con(i, v);
        }
    }
}

void dfs(int par, int u) {
    numChild[u] = 1;
    dp[u][0][0] = 0;
    for (int v : adj[u]) if (v != par) {
        dfs(u, v);
        numChild[u] += numChild[v];
        for (int i = numChild[u]; i >= 2; i--) {
            for (int j = min(i, numChild[v]); j >= 0; j--) {
                ///use current node;
                    dp[u][i][1] = min(dp[u][i][1], dp[u][i - j][1] + min(dp[v][j][0], dp[v][j][1]));

                if (i - j - 1 >= 0 && j - 1 >= 0) {
                    dp[u][i][1] = min(dp[u][i][1], dp[u][i - j - 1][0] + D[u] + dp[v][j - 1][0] + D[v]);
                }
                if (i - j - 1 >= 0) {
                    dp[u][i][1] = min(dp[u][i][1], dp[u][i - j - 1][0] + D[u] + dp[v][j][1]);
                }
                if (j - 1 >= 0) {
                    dp[u][i][1] = min(dp[u][i][1], dp[u][i - j][1] + dp[v][j - 1][0] + D[v]);
                }


                ///not use current node
                    dp[u][i][0] = min(dp[u][i][0], dp[u][i - j][0] + min(dp[v][j][0], dp[v][j][1]));
            }
        }
    }
}

int main() {
    //freopen("dzumbus.inp", "r", stdin);
    //freopen("dzumbus.out", "w", stdout);
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> D[i];
    D[0] = INF;
    for (int u, v, i = 1; i <= m; i++) {
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    for (int i = 1; i <= n; i++) {
        if (!vis[i]) {
            vis[i] = 1;
            con(dummy, i);
            adj[dummy].push_back(i);
        }
    }
    for (int i = 0; i <= n; i++) {
        for (int j = 0; j <= n; j++) {
            dp[i][j][0] = dp[i][j][1] = INF;
        }
    }
    dfs(-1, 0);
    cin >> q;
    while (q--) {
        ll S; cin >> S;
        for (int i = n ; i >= 0; i--) {
            if (dp[0][i][0] <= S) {
                //cout << dp[0][i][0] << ' ' ;
                cout << i << '\n';
                break;
            }
        }
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 444 ms 16384 KB Output is correct
2 Correct 616 ms 16420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 444 ms 16384 KB Output is correct
2 Correct 616 ms 16420 KB Output is correct
3 Correct 808 ms 18528 KB Output is correct
4 Correct 464 ms 19320 KB Output is correct
5 Correct 509 ms 18808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 3064 KB Output is correct
2 Correct 51 ms 2808 KB Output is correct
3 Correct 67 ms 3320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 444 ms 16384 KB Output is correct
2 Correct 616 ms 16420 KB Output is correct
3 Correct 808 ms 18528 KB Output is correct
4 Correct 464 ms 19320 KB Output is correct
5 Correct 509 ms 18808 KB Output is correct
6 Correct 45 ms 3064 KB Output is correct
7 Correct 51 ms 2808 KB Output is correct
8 Correct 67 ms 3320 KB Output is correct
9 Correct 88 ms 18296 KB Output is correct
10 Correct 99 ms 19064 KB Output is correct
11 Correct 188 ms 18684 KB Output is correct