Submission #765734

# Submission time Handle Problem Language Result Execution time Memory
765734 2023-06-25T04:16:15 Z chanhchuong123 Džumbus (COCI19_dzumbus) C++14
110 / 110
50 ms 11420 KB
#include <bits/stdc++.h>
using namespace std;
#define task ""
#define fi first
#define se second
#define MASK(i) (1LL << (i))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define BIT(mask, i) ((mask) >> (i) & 1)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (b), _a = (a); i >= _a; --i)

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 dx[] = {-1, 0, 0, +1};
const int dy[] = {0, -1, +1, 0};

const int INF = 1e9 + 7;
const int MAX = 1010;
int n, m, q;
int res[MAX];
bool vis[MAX];
int D[MAX], sz[MAX];
vector<int> adj[MAX];
int dp[MAX][MAX][2][2];
int f[MAX][2][2], g[MAX][2][2];

void dfs(int u, int p) {
    vis[u] = true; for (int v: adj[u]) if (!vis[v]) dfs(v, u);

    FOR(j, 0, n) FOR(a, 0, 1) FOR(b, 0, 1) {
        f[j][a][b] = INF;
        g[j][a][b] = INF;
    }

    sz[u] = 1;
    f[0][0][0] = 0;
    f[0][1][0] = D[u];
    for (int v: adj[u]) if (v != p) {
        FOR(j, 0, sz[u]) FOR(a, 0, 1) FOR(b, 0, 1)
        FOR(jj, 0, sz[v]) FOR(aa, 0, 1) FOR(bb, 0, 1){
            int jjj = j + jj + (a & aa) * (!b + !bb);
            minimize(g[jjj][a][b | (a & aa)], f[j][a][b] + dp[v][jj][aa][bb]);
        }
        sz[u] += sz[v];
        FOR(j, 0, sz[u]) FOR(a, 0, 1) FOR(b, 0, 1) {
            f[j][a][b] = g[j][a][b];
            g[j][a][b] = INF;
        }
    }
    FOR(j, 0, sz[u]) FOR(a, 0, 1) FOR(b, 0, 1)
        dp[u][j][a][b] = f[j][a][b];
}

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

	if (fopen(task".inp", "r")) {
		freopen(task".inp", "r", stdin);
		freopen(task".out", "w", stdout);
	}

    cin >> n >> m;
    for (int i = 1; i <= n; ++i) cin >> D[i];
    for (int i = 1; i <= m; ++i) {
        int u, v; cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    vector<pair<int, int>> store;
    for (int i = 1; i <= n; ++i) if (!vis[i]) {
        dfs(i, 0);
        adj[0].push_back(i);
    }
    D[0] = INF; dfs(0, 0);
    for (int j = n; j >= 1; --j) {
        res[j] = INF;
        for (int a = 0; a < 2; ++a) {
            for (int b = 0; b < 2; ++b)
                minimize(res[j], dp[0][j][a][b]);
        }
        if (j < m) minimize(res[j], res[j + 1]);
    }
    cin >> q; while (q--) {
        int S; cin >> S;
        cout << (upper_bound(res, res + 1 + n, S) - res - 1) << '\n';
    }
	return 0;
}

Compilation message

dzumbus.cpp: In function 'int main()':
dzumbus.cpp:64:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |   freopen(task".inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
dzumbus.cpp:65:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   65 |   freopen(task".out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 21 ms 8788 KB Output is correct
2 Correct 22 ms 9572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 8788 KB Output is correct
2 Correct 22 ms 9572 KB Output is correct
3 Correct 49 ms 10836 KB Output is correct
4 Correct 50 ms 11420 KB Output is correct
5 Correct 48 ms 10780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 1620 KB Output is correct
2 Correct 25 ms 2516 KB Output is correct
3 Correct 28 ms 3092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 8788 KB Output is correct
2 Correct 22 ms 9572 KB Output is correct
3 Correct 49 ms 10836 KB Output is correct
4 Correct 50 ms 11420 KB Output is correct
5 Correct 48 ms 10780 KB Output is correct
6 Correct 25 ms 1620 KB Output is correct
7 Correct 25 ms 2516 KB Output is correct
8 Correct 28 ms 3092 KB Output is correct
9 Correct 42 ms 6700 KB Output is correct
10 Correct 49 ms 7240 KB Output is correct
11 Correct 47 ms 6868 KB Output is correct