Submission #998876

# Submission time Handle Problem Language Result Execution time Memory
998876 2024-06-14T18:50:55 Z Wael Closing Time (IOI23_closing) C++17
43 / 100
718 ms 69952 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;
        }

        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, cntOfTwo = 0;
    for (int c = 0; c <= n; ++c) {
        if (c) {
            ans = max(ans, work(1, k) + 2 * cntOfTwo);
        } 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;
        ++cntOfTwo;
        pathCost -= (onPath[u] ? one[u] : 0);
        k -= two[u];
    }

    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 155 ms 61000 KB Output is correct
2 Correct 173 ms 69952 KB Output is correct
3 Correct 76 ms 5716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 348 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 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 348 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 436 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 436 KB Output is correct
19 Correct 8 ms 604 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 10 ms 604 KB Output is correct
22 Correct 1 ms 600 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 348 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 436 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 436 KB Output is correct
19 Correct 8 ms 604 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 10 ms 604 KB Output is correct
22 Correct 1 ms 600 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 5 ms 348 KB Output is correct
26 Correct 401 ms 1460 KB Output is correct
27 Correct 2 ms 1368 KB Output is correct
28 Correct 457 ms 1588 KB Output is correct
29 Correct 718 ms 1512 KB Output is correct
30 Correct 2 ms 1372 KB Output is correct
31 Correct 2 ms 1540 KB Output is correct
32 Correct 2 ms 1372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Incorrect 0 ms 348 KB 1st lines differ - on the 1st token, expected: '6', found: '5'
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 1 ms 344 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 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 436 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Incorrect 0 ms 348 KB 1st lines differ - on the 1st token, expected: '6', found: '5'
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 1 ms 344 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 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 436 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 436 KB Output is correct
20 Correct 8 ms 604 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 10 ms 604 KB Output is correct
23 Correct 1 ms 600 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Incorrect 0 ms 348 KB 1st lines differ - on the 1st token, expected: '6', found: '5'
27 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 1 ms 344 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 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 436 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 436 KB Output is correct
20 Correct 8 ms 604 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 10 ms 604 KB Output is correct
23 Correct 1 ms 600 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 5 ms 348 KB Output is correct
27 Correct 401 ms 1460 KB Output is correct
28 Correct 2 ms 1368 KB Output is correct
29 Correct 457 ms 1588 KB Output is correct
30 Correct 718 ms 1512 KB Output is correct
31 Correct 2 ms 1372 KB Output is correct
32 Correct 2 ms 1540 KB Output is correct
33 Correct 2 ms 1372 KB Output is correct
34 Incorrect 0 ms 348 KB 1st lines differ - on the 1st token, expected: '6', found: '5'
35 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 1 ms 344 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 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 436 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 436 KB Output is correct
20 Correct 8 ms 604 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 10 ms 604 KB Output is correct
23 Correct 1 ms 600 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 5 ms 348 KB Output is correct
27 Correct 401 ms 1460 KB Output is correct
28 Correct 2 ms 1368 KB Output is correct
29 Correct 457 ms 1588 KB Output is correct
30 Correct 718 ms 1512 KB Output is correct
31 Correct 2 ms 1372 KB Output is correct
32 Correct 2 ms 1540 KB Output is correct
33 Correct 2 ms 1372 KB Output is correct
34 Incorrect 0 ms 348 KB 1st lines differ - on the 1st token, expected: '6', found: '5'
35 Halted 0 ms 0 KB -