Submission #998891

# Submission time Handle Problem Language Result Execution time Memory
998891 2024-06-14T19:20:58 Z Wael Closing Time (IOI23_closing) C++17
26 / 100
1000 ms 69696 KB
#include "closing.h"
//#include "grader.cpp"
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;

int max_score(int n, int x, int y, long long k, vector<int> U, vector<int> V, vector<int> W) {
    vector<vector<pair<int, int>>> adj(n);
    for (int i = 0; i < n - 1; ++i) {
        adj[U[i]].push_back({V[i], W[i]});
        adj[V[i]].push_back({U[i], W[i]});
    }

    vector<vector<i64>> dis(n, vector<i64>(2, 0));
    vector<vector<int>> next(n, vector<int>(2, -1));
    function<void(int, int, int)> dfs = [&](int u, int p, int t) {
        for (auto [v, w] : adj[u]) {
            if (v == p) continue;
            dis[v][t] = dis[u][t] + w;
            next[v][!t] = u;
            dfs(v, u, t);
        }
    };
    dfs(x, -1, 0);
    dfs(y, -1, 1);

    i64 pathCost = 0;
    vector<int> path, onPath(n, 0);
    int z = x;
    while (true) {
        path.push_back(z);
        onPath[z] = 1;
        pathCost += min(dis[z][0], dis[z][1]);
        if (z == y) {
            break;
        }
        z = next[z][0];
    }
    
    vector<pair<i64, int>> vec;
    vector<i64> one(n), two(n);
    for (int u = 0; u < n; ++u) {
        one[u] = min(dis[u][0], dis[u][1]);
        two[u] = max(dis[u][0], dis[u][1]);
        vec.push_back({two[u], u});
    }
    sort(vec.begin(), vec.end(), greater<pair<i64, int>>());

    vector<int> used(n, 0);
    auto work = [&](bool mark, i64 k) {
        int ret = 0;
        vector<int> vis = used;
        if (mark) {
            for (auto u : path) {
                if (vis[u] == 0) {
                    vis[u] = 1;
                    k -= one[u];
                    ++ret;
                }
            }
        } else {
            vis[x] = vis[y] = 1;
            ret = 2;
        }
        if (k < 0) {
            return (int)-1e9;
        }

        priority_queue<pair<i64, int>, vector<pair<i64, int>>, greater<pair<i64, int>>> pq;
        for (int u = 0; u < n; ++u) {
            bool found = 0;
            for (auto [v, w] : adj[u]) {
                found |= vis[v];
            }
            if (found) {
                pq.push({one[u], u});
            }
        }

        while (pq.size()) {
            auto [cost, u] = pq.top();
            pq.pop();
            if (cost > k) {
                break;
            }
            if (vis[u]) {
                continue;
            }

            vis[u] = 1;        
            k -= cost;
            ++ret;
            for (auto [v, w] : adj[u]) {
                pq.push({one[v], v});
            }
        }

        return ret;
    };

    auto canMark = [&](int u, i64 k, i64 pathCost) {
        pathCost -= (onPath[u] ? one[u] : 0);
        k -= two[u];
        return k >= pathCost;
    };

    int ans = 0;
    if (n <= 20) {
        for (int mask = 0; mask < (1 << n); ++mask) {
            fill(used.begin(), used.end(), 0);

            i64 curK = k;
            for (int j = 0; j < n; ++j) {
                if (mask & (1 << j)) {
                    used[j] = 1;
                    curK -= two[j];
                }
            }
            int cnt = __builtin_popcount(mask);
            ans = max(ans, work(cnt > 0, curK) + 2 * cnt);
        }
    } else {
        for (int c = 0; c <= n; ++c) {
            if (c) {
                ans = max(ans, work(1, k) + 2 * c);
            } else {
                ans = max(ans, work(0, k));
            }
            while (vec.size() && canMark(vec.back().second, k, pathCost) == 0) {
                vec.pop_back();
            }
            if (vec.size() == 0) {
                break;
            }
            auto [cost, u] = vec.back();
            vec.pop_back();
            used[u] = 1;
            pathCost -= (onPath[u] ? one[u] : 0);
            k -= two[u];
        }
    }

    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 173 ms 59924 KB Output is correct
2 Correct 191 ms 69696 KB Output is correct
3 Correct 77 ms 5720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 555 ms 412 KB Output is correct
3 Correct 199 ms 348 KB Output is correct
4 Correct 322 ms 344 KB Output is correct
5 Correct 74 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 555 ms 412 KB Output is correct
3 Correct 199 ms 348 KB Output is correct
4 Correct 322 ms 344 KB Output is correct
5 Correct 74 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Execution timed out 1098 ms 348 KB Time limit exceeded
19 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 555 ms 412 KB Output is correct
3 Correct 199 ms 348 KB Output is correct
4 Correct 322 ms 344 KB Output is correct
5 Correct 74 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Execution timed out 1098 ms 348 KB Time limit exceeded
19 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 555 ms 412 KB Output is correct
4 Correct 199 ms 348 KB Output is correct
5 Correct 322 ms 344 KB Output is correct
6 Correct 74 ms 344 KB Output is correct
7 Correct 0 ms 600 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 23 ms 420 KB Output is correct
11 Correct 232 ms 348 KB Output is correct
12 Correct 3 ms 348 KB Output is correct
13 Correct 523 ms 348 KB Output is correct
14 Correct 365 ms 348 KB Output is correct
15 Correct 25 ms 344 KB Output is correct
16 Correct 186 ms 348 KB Output is correct
17 Correct 23 ms 348 KB Output is correct
18 Correct 56 ms 348 KB Output is correct
19 Correct 125 ms 348 KB Output is correct
20 Correct 166 ms 348 KB Output is correct
21 Correct 72 ms 348 KB Output is correct
22 Correct 792 ms 412 KB Output is correct
23 Correct 788 ms 344 KB Output is correct
24 Correct 22 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 555 ms 412 KB Output is correct
4 Correct 199 ms 348 KB Output is correct
5 Correct 322 ms 344 KB Output is correct
6 Correct 74 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 600 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 23 ms 420 KB Output is correct
23 Correct 232 ms 348 KB Output is correct
24 Correct 3 ms 348 KB Output is correct
25 Correct 523 ms 348 KB Output is correct
26 Correct 365 ms 348 KB Output is correct
27 Correct 25 ms 344 KB Output is correct
28 Correct 186 ms 348 KB Output is correct
29 Correct 23 ms 348 KB Output is correct
30 Correct 56 ms 348 KB Output is correct
31 Correct 125 ms 348 KB Output is correct
32 Correct 166 ms 348 KB Output is correct
33 Correct 72 ms 348 KB Output is correct
34 Correct 792 ms 412 KB Output is correct
35 Correct 788 ms 344 KB Output is correct
36 Correct 22 ms 348 KB Output is correct
37 Correct 186 ms 416 KB Output is correct
38 Incorrect 0 ms 348 KB 1st lines differ - on the 1st token, expected: '41', found: '39'
39 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 555 ms 412 KB Output is correct
4 Correct 199 ms 348 KB Output is correct
5 Correct 322 ms 344 KB Output is correct
6 Correct 74 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Execution timed out 1098 ms 348 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 555 ms 412 KB Output is correct
4 Correct 199 ms 348 KB Output is correct
5 Correct 322 ms 344 KB Output is correct
6 Correct 74 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Execution timed out 1098 ms 348 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 555 ms 412 KB Output is correct
4 Correct 199 ms 348 KB Output is correct
5 Correct 322 ms 344 KB Output is correct
6 Correct 74 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Execution timed out 1098 ms 348 KB Time limit exceeded
20 Halted 0 ms 0 KB -