Submission #840831

# Submission time Handle Problem Language Result Execution time Memory
840831 2023-08-31T18:20:16 Z vjudge1 Closing Time (IOI23_closing) C++17
43 / 100
532 ms 36772 KB
#include "closing.h"

#include <bits/stdc++.h>
using namespace std;

vector<pair<int, int>> adj[200000];

void dfs(int u, int p, vector<int64_t>& d) {
        for (auto [v, c] : adj[u]) {
                if (v == p) continue;
                d[v] = d[u] + c;
                dfs(v, u, d);
        }
}

int max_score(int N, int X, int Y, long long K, std::vector<int> U, std::vector<int> V, std::vector<int> W) {
        for (int i = 0; i < N; i++) adj[i].clear();
        for (int i = 0; i < N - 1; i++) {
                adj[U[i]].emplace_back(V[i], W[i]);
                adj[V[i]].emplace_back(U[i], W[i]);
        }
        vector<int64_t> dX(N), dY(N);
        dfs(X, -1, dX);
        dfs(Y, -1, dY);
        vector<int> ord(N);
        iota(ord.begin(), ord.end(), 0);
        sort(ord.begin(), ord.end(), [&](int i, int j) {
                return max(dX[i], dY[i]) < max(dX[j], dY[j]);
        });
        vector<int> path;
        for (int i = 0; i < N; i++) {
                if (dX[i] + dY[i] == dX[Y]) path.emplace_back(i);
        }
        vector<int> both(N, 0);
        vector<int> chk;
        int64_t cur = 0;

        auto solve = [&](bool con = 1) {
                if (cur > K) return -N * 2;
                int cnt = 0;
                int64_t cost = cur;
                vector<int> c(N, 0);
                vector<int> vis(N, 0);
                if (con) {
                        for (int x : path) {
                                if (both[x]) continue;
                                cost += min(dX[x], dY[x]);
                                c[x] = 1;
                                cnt++;
                        }
                        if (cost > K) return -N * 2;
                        queue<int> q;
                        for (int i : chk) {
                                cnt += 2;
                                q.emplace(i);
                                vis[i] = 1;
                        }
                        while (q.size()) {
                                int u = q.front();
                                q.pop();
                                for (auto [v, w] : adj[u]) {
                                        if (vis[v]) continue;
                                        if (c[v]) continue;
                                        if (dX[v] == dX[u] + w && dY[v] == dY[u] + w) {
                                                q.emplace(v);
                                                vis[v] = 1;
                                        }
                                }
                        }
                }
                priority_queue<pair<int64_t, int>> pq2, pq1;
                for (int i = 0; i < N; i++) {
                        if (both[i]) continue;
                        if (c[i]) continue;
                        if (vis[i]) {
                                pq2.emplace(-max(dX[i], dY[i]), i);
                        } else {
                                pq1.emplace(-min(dX[i], dY[i]), i);
                        }
                }
                vector<int> order;
                while (pq2.size()) {
                        auto [du, u] = pq2.top();
                        if (cost - du > K) break;
                        pq2.pop();
                        cost -= du;
                        order.emplace_back(u);
                        cnt += 2;
                }
                while (pq2.size()) pq1.emplace(-min(dX[pq2.top().second], dY[pq2.top().second]), pq2.top().second), pq2.pop();
                auto upd = [&]() {
                        while (pq1.size()) {
                                auto [du, u] = pq1.top();
                                if (cost - du > K) return;
                                pq1.pop();
                                cost -= du;
                                c[u] = 1;
                                cnt++;
                        }
                };
                upd();
                int ans = cnt;
                while (order.size()) {
                        cnt -= 2;
                        int u = order.back();
                        order.pop_back();
                        cost -= max(dX[u], dY[u]);
                        pq1.emplace(-min(dX[u], dY[u]), u);
                        upd();
                        ans = max(ans, cnt);
                }
                return ans;
        };

        int res = solve(0);
        for (int i = 0; i < N; i++) {
                int j = ord[i];
                if (dX[j] + dY[j]) {
                        chk.emplace_back(j);
                        both[j] = 1;
                        cur += max(dX[j], dY[j]);
                        res = max(res, solve());
                }
        }
        return res;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 117 ms 32172 KB Output is correct
2 Correct 130 ms 36772 KB Output is correct
3 Correct 64 ms 7500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4980 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4980 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 2 ms 4948 KB Output is correct
13 Correct 4 ms 4948 KB Output is correct
14 Correct 2 ms 4948 KB Output is correct
15 Correct 2 ms 4948 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 2 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 15 ms 5076 KB Output is correct
20 Correct 4 ms 5076 KB Output is correct
21 Correct 6 ms 5076 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 2 ms 5076 KB Output is correct
24 Correct 2 ms 5076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4980 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 2 ms 4948 KB Output is correct
13 Correct 4 ms 4948 KB Output is correct
14 Correct 2 ms 4948 KB Output is correct
15 Correct 2 ms 4948 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 2 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 15 ms 5076 KB Output is correct
20 Correct 4 ms 5076 KB Output is correct
21 Correct 6 ms 5076 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 2 ms 5076 KB Output is correct
24 Correct 2 ms 5076 KB Output is correct
25 Correct 8 ms 5072 KB Output is correct
26 Correct 532 ms 5476 KB Output is correct
27 Correct 7 ms 5540 KB Output is correct
28 Correct 50 ms 5532 KB Output is correct
29 Correct 197 ms 5496 KB Output is correct
30 Correct 3 ms 5460 KB Output is correct
31 Correct 10 ms 5460 KB Output is correct
32 Correct 12 ms 5556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4980 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 2 ms 4948 KB Output is correct
13 Correct 2 ms 4948 KB Output is correct
14 Correct 2 ms 4948 KB Output is correct
15 Correct 2 ms 4948 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 2 ms 4948 KB Output is correct
18 Correct 2 ms 4948 KB Output is correct
19 Correct 2 ms 4948 KB Output is correct
20 Correct 2 ms 4948 KB Output is correct
21 Correct 2 ms 4948 KB Output is correct
22 Correct 2 ms 4948 KB Output is correct
23 Incorrect 2 ms 4948 KB 1st lines differ - on the 1st token, expected: '39', found: '38'
24 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4980 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 2 ms 4948 KB Output is correct
13 Correct 2 ms 4948 KB Output is correct
14 Correct 4 ms 4948 KB Output is correct
15 Correct 2 ms 4948 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 2 ms 4948 KB Output is correct
18 Correct 2 ms 4948 KB Output is correct
19 Correct 2 ms 4948 KB Output is correct
20 Correct 2 ms 4948 KB Output is correct
21 Correct 2 ms 4948 KB Output is correct
22 Correct 2 ms 4948 KB Output is correct
23 Correct 2 ms 4948 KB Output is correct
24 Correct 2 ms 4948 KB Output is correct
25 Correct 2 ms 4948 KB Output is correct
26 Correct 2 ms 4948 KB Output is correct
27 Correct 2 ms 4948 KB Output is correct
28 Correct 2 ms 4948 KB Output is correct
29 Correct 2 ms 4948 KB Output is correct
30 Correct 2 ms 4948 KB Output is correct
31 Correct 2 ms 4948 KB Output is correct
32 Correct 2 ms 4948 KB Output is correct
33 Correct 2 ms 4948 KB Output is correct
34 Correct 2 ms 4948 KB Output is correct
35 Incorrect 2 ms 4948 KB 1st lines differ - on the 1st token, expected: '39', found: '38'
36 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4980 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 2 ms 4948 KB Output is correct
13 Correct 2 ms 4948 KB Output is correct
14 Correct 4 ms 4948 KB Output is correct
15 Correct 2 ms 4948 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 2 ms 4948 KB Output is correct
18 Correct 2 ms 4948 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 15 ms 5076 KB Output is correct
21 Correct 4 ms 5076 KB Output is correct
22 Correct 6 ms 5076 KB Output is correct
23 Correct 3 ms 5076 KB Output is correct
24 Correct 2 ms 5076 KB Output is correct
25 Correct 2 ms 5076 KB Output is correct
26 Correct 2 ms 4948 KB Output is correct
27 Correct 2 ms 4948 KB Output is correct
28 Correct 2 ms 4948 KB Output is correct
29 Correct 2 ms 4948 KB Output is correct
30 Correct 2 ms 4948 KB Output is correct
31 Correct 2 ms 4948 KB Output is correct
32 Correct 2 ms 4948 KB Output is correct
33 Correct 2 ms 4948 KB Output is correct
34 Correct 2 ms 4948 KB Output is correct
35 Correct 2 ms 4948 KB Output is correct
36 Correct 2 ms 4948 KB Output is correct
37 Correct 2 ms 4948 KB Output is correct
38 Correct 2 ms 4948 KB Output is correct
39 Correct 2 ms 4948 KB Output is correct
40 Correct 2 ms 4948 KB Output is correct
41 Correct 2 ms 4948 KB Output is correct
42 Incorrect 2 ms 4948 KB 1st lines differ - on the 1st token, expected: '39', found: '38'
43 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4980 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 2 ms 4948 KB Output is correct
13 Correct 2 ms 4948 KB Output is correct
14 Correct 4 ms 4948 KB Output is correct
15 Correct 2 ms 4948 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 2 ms 4948 KB Output is correct
18 Correct 2 ms 4948 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 15 ms 5076 KB Output is correct
21 Correct 4 ms 5076 KB Output is correct
22 Correct 6 ms 5076 KB Output is correct
23 Correct 3 ms 5076 KB Output is correct
24 Correct 2 ms 5076 KB Output is correct
25 Correct 2 ms 5076 KB Output is correct
26 Correct 8 ms 5072 KB Output is correct
27 Correct 532 ms 5476 KB Output is correct
28 Correct 7 ms 5540 KB Output is correct
29 Correct 50 ms 5532 KB Output is correct
30 Correct 197 ms 5496 KB Output is correct
31 Correct 3 ms 5460 KB Output is correct
32 Correct 10 ms 5460 KB Output is correct
33 Correct 12 ms 5556 KB Output is correct
34 Correct 2 ms 4948 KB Output is correct
35 Correct 2 ms 4948 KB Output is correct
36 Correct 2 ms 4948 KB Output is correct
37 Correct 2 ms 4948 KB Output is correct
38 Correct 2 ms 4948 KB Output is correct
39 Correct 2 ms 4948 KB Output is correct
40 Correct 2 ms 4948 KB Output is correct
41 Correct 2 ms 4948 KB Output is correct
42 Correct 2 ms 4948 KB Output is correct
43 Correct 2 ms 4948 KB Output is correct
44 Correct 2 ms 4948 KB Output is correct
45 Correct 2 ms 4948 KB Output is correct
46 Correct 2 ms 4948 KB Output is correct
47 Correct 2 ms 4948 KB Output is correct
48 Correct 2 ms 4948 KB Output is correct
49 Correct 2 ms 4948 KB Output is correct
50 Incorrect 2 ms 4948 KB 1st lines differ - on the 1st token, expected: '39', found: '38'
51 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4980 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 2 ms 4948 KB Output is correct
13 Correct 2 ms 4948 KB Output is correct
14 Correct 4 ms 4948 KB Output is correct
15 Correct 2 ms 4948 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 2 ms 4948 KB Output is correct
18 Correct 2 ms 4948 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 15 ms 5076 KB Output is correct
21 Correct 4 ms 5076 KB Output is correct
22 Correct 6 ms 5076 KB Output is correct
23 Correct 3 ms 5076 KB Output is correct
24 Correct 2 ms 5076 KB Output is correct
25 Correct 2 ms 5076 KB Output is correct
26 Correct 8 ms 5072 KB Output is correct
27 Correct 532 ms 5476 KB Output is correct
28 Correct 7 ms 5540 KB Output is correct
29 Correct 50 ms 5532 KB Output is correct
30 Correct 197 ms 5496 KB Output is correct
31 Correct 3 ms 5460 KB Output is correct
32 Correct 10 ms 5460 KB Output is correct
33 Correct 12 ms 5556 KB Output is correct
34 Correct 2 ms 4948 KB Output is correct
35 Correct 2 ms 4948 KB Output is correct
36 Correct 2 ms 4948 KB Output is correct
37 Correct 2 ms 4948 KB Output is correct
38 Correct 2 ms 4948 KB Output is correct
39 Correct 2 ms 4948 KB Output is correct
40 Correct 2 ms 4948 KB Output is correct
41 Correct 2 ms 4948 KB Output is correct
42 Correct 2 ms 4948 KB Output is correct
43 Correct 2 ms 4948 KB Output is correct
44 Correct 2 ms 4948 KB Output is correct
45 Correct 2 ms 4948 KB Output is correct
46 Correct 2 ms 4948 KB Output is correct
47 Correct 2 ms 4948 KB Output is correct
48 Correct 2 ms 4948 KB Output is correct
49 Correct 2 ms 4948 KB Output is correct
50 Incorrect 2 ms 4948 KB 1st lines differ - on the 1st token, expected: '39', found: '38'
51 Halted 0 ms 0 KB -