Submission #947030

# Submission time Handle Problem Language Result Execution time Memory
947030 2024-03-15T11:34:15 Z alextodoran Security Guard (JOI23_guard) C++17
100 / 100
728 ms 81084 KB
/**
 _  _   __  _ _ _  _  _ _
 |a  ||t  ||o    d | |o  |
| __    _| | _ | __|  _ |
| __ |/_  | __  /__\ / _\|

**/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N_MAX = 200000;
const int M_MAX = 400000;

int N, M, Q;
int S[N_MAX + 2];
int edge_u[M_MAX + 2], edge_v[M_MAX + 2];

int order[M_MAX + 2];

int leader[N_MAX + 2];
int find_leader (int u) {
    return (leader[u] == 0 ? u : leader[u] = find_leader(leader[u]));
}
bool join (int u, int v) {
    u = find_leader(u);
    v = find_leader(v);
    if (u != v) {
        leader[u] = v;
        return true;
    }
    return false;
}

vector <int> adj[N_MAX + 2];

int root;
ll cost;

void init () {
    int mx = 0;
    for (int u = 1; u <= N; u++) {
        cost -= S[u];
        mx = max(mx, S[u]);
    }
    cost += mx;
    root = 1;
    for (int u = 2; u <= N; u++) {
        if (S[u] < S[root]) {
            root = u;
        }
    }
    for (int u = 1; u <= N; u++) {
        if (u != root) {
            cost += S[u] + S[root];
        }
    }
}

ll answer[N_MAX + 2];

struct Edge {
    int u, v;
    int delta;
};
bool operator < (const Edge &e1, const Edge &e2) {
    return make_tuple(e1.delta, e1.u, e1.v) < make_tuple(e2.delta, e2.u, e2.v);
}
Edge operator - (const Edge &e, const int &val) {
    return Edge{e.u, e.v, e.delta - val};
}

int min_comp[N_MAX + 2];
set <Edge> edges[N_MAX + 2];
set <Edge> best_edges;

signed main () {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> N >> M >> Q;
    for (int u = 1; u <= N; u++) {
        cin >> S[u];
    }
    for (int i = 1; i <= M; i++) {
        cin >> edge_u[i] >> edge_v[i];
    }
    iota(order + 1, order + M + 1, 1);
    sort(order + 1, order + M + 1, [&] (const int &i, const int &j) {
        return S[edge_u[i]] + S[edge_v[i]] < S[edge_u[j]] + S[edge_v[j]];
    });
    for (int _ = 1; _ <= M; _++) {
        int i = order[_];
        if (join(edge_u[i], edge_v[i]) == true) {
            adj[edge_u[i]].push_back(edge_v[i]);
            adj[edge_v[i]].push_back(edge_u[i]);
        }
    }
    init();
    for (int i = N - 1; i <= Q; i++) {
        answer[i] = cost;
    }
    fill(leader + 1, leader + N + 1, 0);
    for (int u = 1; u <= N; u++) {
        min_comp[u] = S[u];
        for (int v : adj[u]) {
            edges[u].insert(Edge{u, v, S[u] + S[v]});
        }
        best_edges.insert(*edges[u].begin() - S[u]);
    }
    for (int k = N - 2; k >= 0; k--) {
        Edge e = *best_edges.begin();
        int u = e.u, v = e.v, delta = e.delta;
        int U = find_leader(u), V = find_leader(v);
        best_edges.erase(*edges[U].begin() - min_comp[U]);
        best_edges.erase(*edges[V].begin() - min_comp[V]);
        edges[U].erase(Edge{u, v, S[u] + S[v]});
        edges[V].erase(Edge{v, u, S[v] + S[u]});
        cost += delta;
        cost -= S[root];
        answer[k] = cost;
        if ((int) edges[U].size() > (int) edges[V].size()) {
            swap(U, V);
        }
        for (Edge e : edges[U]) {
            edges[V].insert(e);
        }
        edges[U].clear();
        min_comp[V] = min(min_comp[U], min_comp[V]);
        leader[U] = V;
        best_edges.insert(*edges[V].begin() - min_comp[V]);
    }
    for (int i = 0; i <= Q; i++) {
        cout << answer[i] << "\n";
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 21080 KB Output is correct
2 Correct 189 ms 66644 KB Output is correct
3 Correct 184 ms 69120 KB Output is correct
4 Correct 217 ms 68944 KB Output is correct
5 Correct 240 ms 69104 KB Output is correct
6 Correct 230 ms 69108 KB Output is correct
7 Correct 233 ms 69112 KB Output is correct
8 Correct 4 ms 21084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 21080 KB Output is correct
2 Correct 189 ms 66644 KB Output is correct
3 Correct 184 ms 69120 KB Output is correct
4 Correct 217 ms 68944 KB Output is correct
5 Correct 240 ms 69104 KB Output is correct
6 Correct 230 ms 69108 KB Output is correct
7 Correct 233 ms 69112 KB Output is correct
8 Correct 4 ms 21084 KB Output is correct
9 Correct 4 ms 21080 KB Output is correct
10 Correct 524 ms 70596 KB Output is correct
11 Correct 204 ms 70888 KB Output is correct
12 Correct 215 ms 70460 KB Output is correct
13 Correct 189 ms 70644 KB Output is correct
14 Correct 514 ms 70652 KB Output is correct
15 Correct 529 ms 70864 KB Output is correct
16 Correct 484 ms 70592 KB Output is correct
17 Correct 593 ms 70628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 21080 KB Output is correct
2 Correct 189 ms 66644 KB Output is correct
3 Correct 184 ms 69120 KB Output is correct
4 Correct 217 ms 68944 KB Output is correct
5 Correct 240 ms 69104 KB Output is correct
6 Correct 230 ms 69108 KB Output is correct
7 Correct 233 ms 69112 KB Output is correct
8 Correct 4 ms 21084 KB Output is correct
9 Correct 4 ms 21080 KB Output is correct
10 Correct 524 ms 70596 KB Output is correct
11 Correct 204 ms 70888 KB Output is correct
12 Correct 215 ms 70460 KB Output is correct
13 Correct 189 ms 70644 KB Output is correct
14 Correct 514 ms 70652 KB Output is correct
15 Correct 529 ms 70864 KB Output is correct
16 Correct 484 ms 70592 KB Output is correct
17 Correct 593 ms 70628 KB Output is correct
18 Correct 4 ms 21080 KB Output is correct
19 Correct 606 ms 70760 KB Output is correct
20 Correct 624 ms 70652 KB Output is correct
21 Correct 596 ms 70740 KB Output is correct
22 Correct 594 ms 70868 KB Output is correct
23 Correct 345 ms 71652 KB Output is correct
24 Correct 353 ms 71456 KB Output is correct
25 Correct 392 ms 71564 KB Output is correct
26 Correct 305 ms 71304 KB Output is correct
27 Correct 373 ms 71180 KB Output is correct
28 Correct 487 ms 70740 KB Output is correct
29 Correct 439 ms 70996 KB Output is correct
30 Correct 354 ms 71500 KB Output is correct
31 Correct 429 ms 71364 KB Output is correct
32 Correct 500 ms 71032 KB Output is correct
33 Correct 424 ms 69836 KB Output is correct
34 Correct 586 ms 70592 KB Output is correct
35 Correct 643 ms 71764 KB Output is correct
36 Correct 440 ms 77752 KB Output is correct
37 Correct 410 ms 78020 KB Output is correct
38 Correct 386 ms 70648 KB Output is correct
39 Correct 343 ms 71672 KB Output is correct
40 Correct 368 ms 71120 KB Output is correct
41 Correct 360 ms 69772 KB Output is correct
42 Correct 376 ms 69884 KB Output is correct
43 Correct 478 ms 70996 KB Output is correct
44 Correct 561 ms 70656 KB Output is correct
45 Correct 489 ms 70992 KB Output is correct
46 Correct 504 ms 70992 KB Output is correct
47 Correct 501 ms 70832 KB Output is correct
48 Correct 568 ms 71124 KB Output is correct
49 Correct 425 ms 71044 KB Output is correct
50 Correct 501 ms 71040 KB Output is correct
51 Correct 442 ms 71088 KB Output is correct
52 Correct 433 ms 69112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 21080 KB Output is correct
2 Correct 189 ms 66644 KB Output is correct
3 Correct 184 ms 69120 KB Output is correct
4 Correct 217 ms 68944 KB Output is correct
5 Correct 240 ms 69104 KB Output is correct
6 Correct 230 ms 69108 KB Output is correct
7 Correct 233 ms 69112 KB Output is correct
8 Correct 4 ms 21084 KB Output is correct
9 Correct 4 ms 21080 KB Output is correct
10 Correct 524 ms 70596 KB Output is correct
11 Correct 204 ms 70888 KB Output is correct
12 Correct 215 ms 70460 KB Output is correct
13 Correct 189 ms 70644 KB Output is correct
14 Correct 514 ms 70652 KB Output is correct
15 Correct 529 ms 70864 KB Output is correct
16 Correct 484 ms 70592 KB Output is correct
17 Correct 593 ms 70628 KB Output is correct
18 Correct 4 ms 21080 KB Output is correct
19 Correct 606 ms 70760 KB Output is correct
20 Correct 624 ms 70652 KB Output is correct
21 Correct 596 ms 70740 KB Output is correct
22 Correct 594 ms 70868 KB Output is correct
23 Correct 345 ms 71652 KB Output is correct
24 Correct 353 ms 71456 KB Output is correct
25 Correct 392 ms 71564 KB Output is correct
26 Correct 305 ms 71304 KB Output is correct
27 Correct 373 ms 71180 KB Output is correct
28 Correct 487 ms 70740 KB Output is correct
29 Correct 439 ms 70996 KB Output is correct
30 Correct 354 ms 71500 KB Output is correct
31 Correct 429 ms 71364 KB Output is correct
32 Correct 500 ms 71032 KB Output is correct
33 Correct 424 ms 69836 KB Output is correct
34 Correct 586 ms 70592 KB Output is correct
35 Correct 643 ms 71764 KB Output is correct
36 Correct 440 ms 77752 KB Output is correct
37 Correct 410 ms 78020 KB Output is correct
38 Correct 386 ms 70648 KB Output is correct
39 Correct 343 ms 71672 KB Output is correct
40 Correct 368 ms 71120 KB Output is correct
41 Correct 360 ms 69772 KB Output is correct
42 Correct 376 ms 69884 KB Output is correct
43 Correct 478 ms 70996 KB Output is correct
44 Correct 561 ms 70656 KB Output is correct
45 Correct 489 ms 70992 KB Output is correct
46 Correct 504 ms 70992 KB Output is correct
47 Correct 501 ms 70832 KB Output is correct
48 Correct 568 ms 71124 KB Output is correct
49 Correct 425 ms 71044 KB Output is correct
50 Correct 501 ms 71040 KB Output is correct
51 Correct 442 ms 71088 KB Output is correct
52 Correct 433 ms 69112 KB Output is correct
53 Correct 4 ms 21084 KB Output is correct
54 Correct 574 ms 71060 KB Output is correct
55 Correct 610 ms 70884 KB Output is correct
56 Correct 622 ms 71956 KB Output is correct
57 Correct 728 ms 74172 KB Output is correct
58 Correct 398 ms 74660 KB Output is correct
59 Correct 421 ms 74792 KB Output is correct
60 Correct 349 ms 72896 KB Output is correct
61 Correct 422 ms 74552 KB Output is correct
62 Correct 443 ms 74856 KB Output is correct
63 Correct 563 ms 70992 KB Output is correct
64 Correct 500 ms 72776 KB Output is correct
65 Correct 479 ms 74432 KB Output is correct
66 Correct 424 ms 74440 KB Output is correct
67 Correct 526 ms 74192 KB Output is correct
68 Correct 394 ms 69712 KB Output is correct
69 Correct 592 ms 72840 KB Output is correct
70 Correct 647 ms 72180 KB Output is correct
71 Correct 498 ms 77904 KB Output is correct
72 Correct 414 ms 78164 KB Output is correct
73 Correct 364 ms 70820 KB Output is correct
74 Correct 355 ms 71512 KB Output is correct
75 Correct 343 ms 71048 KB Output is correct
76 Correct 376 ms 69968 KB Output is correct
77 Correct 381 ms 69964 KB Output is correct
78 Correct 487 ms 71252 KB Output is correct
79 Correct 655 ms 74152 KB Output is correct
80 Correct 476 ms 71628 KB Output is correct
81 Correct 523 ms 73792 KB Output is correct
82 Correct 645 ms 74136 KB Output is correct
83 Correct 654 ms 74064 KB Output is correct
84 Correct 517 ms 74192 KB Output is correct
85 Correct 489 ms 70868 KB Output is correct
86 Correct 447 ms 70984 KB Output is correct
87 Correct 435 ms 69452 KB Output is correct
88 Correct 628 ms 74172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 21084 KB Output is correct
2 Correct 5 ms 21084 KB Output is correct
3 Correct 4 ms 21080 KB Output is correct
4 Correct 4 ms 21080 KB Output is correct
5 Correct 4 ms 21092 KB Output is correct
6 Correct 4 ms 21084 KB Output is correct
7 Correct 4 ms 21084 KB Output is correct
8 Correct 16 ms 23128 KB Output is correct
9 Correct 17 ms 23284 KB Output is correct
10 Correct 16 ms 23132 KB Output is correct
11 Correct 16 ms 23132 KB Output is correct
12 Correct 16 ms 23132 KB Output is correct
13 Correct 16 ms 23276 KB Output is correct
14 Correct 16 ms 23132 KB Output is correct
15 Correct 16 ms 23128 KB Output is correct
16 Correct 16 ms 23132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 21084 KB Output is correct
2 Correct 5 ms 21084 KB Output is correct
3 Correct 4 ms 21080 KB Output is correct
4 Correct 4 ms 21080 KB Output is correct
5 Correct 4 ms 21092 KB Output is correct
6 Correct 4 ms 21084 KB Output is correct
7 Correct 4 ms 21084 KB Output is correct
8 Correct 16 ms 23128 KB Output is correct
9 Correct 17 ms 23284 KB Output is correct
10 Correct 16 ms 23132 KB Output is correct
11 Correct 16 ms 23132 KB Output is correct
12 Correct 16 ms 23132 KB Output is correct
13 Correct 16 ms 23276 KB Output is correct
14 Correct 16 ms 23132 KB Output is correct
15 Correct 16 ms 23128 KB Output is correct
16 Correct 16 ms 23132 KB Output is correct
17 Correct 24 ms 24096 KB Output is correct
18 Correct 26 ms 24144 KB Output is correct
19 Correct 36 ms 24656 KB Output is correct
20 Correct 58 ms 25168 KB Output is correct
21 Correct 66 ms 25764 KB Output is correct
22 Correct 58 ms 25296 KB Output is correct
23 Correct 111 ms 27220 KB Output is correct
24 Correct 72 ms 25772 KB Output is correct
25 Correct 19 ms 24012 KB Output is correct
26 Correct 41 ms 24756 KB Output is correct
27 Correct 24 ms 24152 KB Output is correct
28 Correct 19 ms 24152 KB Output is correct
29 Correct 19 ms 23900 KB Output is correct
30 Correct 19 ms 23896 KB Output is correct
31 Correct 17 ms 23128 KB Output is correct
32 Correct 17 ms 22876 KB Output is correct
33 Correct 20 ms 23900 KB Output is correct
34 Correct 28 ms 24028 KB Output is correct
35 Correct 41 ms 24840 KB Output is correct
36 Correct 109 ms 27272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 21080 KB Output is correct
2 Correct 189 ms 66644 KB Output is correct
3 Correct 184 ms 69120 KB Output is correct
4 Correct 217 ms 68944 KB Output is correct
5 Correct 240 ms 69104 KB Output is correct
6 Correct 230 ms 69108 KB Output is correct
7 Correct 233 ms 69112 KB Output is correct
8 Correct 4 ms 21084 KB Output is correct
9 Correct 4 ms 21080 KB Output is correct
10 Correct 524 ms 70596 KB Output is correct
11 Correct 204 ms 70888 KB Output is correct
12 Correct 215 ms 70460 KB Output is correct
13 Correct 189 ms 70644 KB Output is correct
14 Correct 514 ms 70652 KB Output is correct
15 Correct 529 ms 70864 KB Output is correct
16 Correct 484 ms 70592 KB Output is correct
17 Correct 593 ms 70628 KB Output is correct
18 Correct 4 ms 21080 KB Output is correct
19 Correct 606 ms 70760 KB Output is correct
20 Correct 624 ms 70652 KB Output is correct
21 Correct 596 ms 70740 KB Output is correct
22 Correct 594 ms 70868 KB Output is correct
23 Correct 345 ms 71652 KB Output is correct
24 Correct 353 ms 71456 KB Output is correct
25 Correct 392 ms 71564 KB Output is correct
26 Correct 305 ms 71304 KB Output is correct
27 Correct 373 ms 71180 KB Output is correct
28 Correct 487 ms 70740 KB Output is correct
29 Correct 439 ms 70996 KB Output is correct
30 Correct 354 ms 71500 KB Output is correct
31 Correct 429 ms 71364 KB Output is correct
32 Correct 500 ms 71032 KB Output is correct
33 Correct 424 ms 69836 KB Output is correct
34 Correct 586 ms 70592 KB Output is correct
35 Correct 643 ms 71764 KB Output is correct
36 Correct 440 ms 77752 KB Output is correct
37 Correct 410 ms 78020 KB Output is correct
38 Correct 386 ms 70648 KB Output is correct
39 Correct 343 ms 71672 KB Output is correct
40 Correct 368 ms 71120 KB Output is correct
41 Correct 360 ms 69772 KB Output is correct
42 Correct 376 ms 69884 KB Output is correct
43 Correct 478 ms 70996 KB Output is correct
44 Correct 561 ms 70656 KB Output is correct
45 Correct 489 ms 70992 KB Output is correct
46 Correct 504 ms 70992 KB Output is correct
47 Correct 501 ms 70832 KB Output is correct
48 Correct 568 ms 71124 KB Output is correct
49 Correct 425 ms 71044 KB Output is correct
50 Correct 501 ms 71040 KB Output is correct
51 Correct 442 ms 71088 KB Output is correct
52 Correct 433 ms 69112 KB Output is correct
53 Correct 4 ms 21084 KB Output is correct
54 Correct 574 ms 71060 KB Output is correct
55 Correct 610 ms 70884 KB Output is correct
56 Correct 622 ms 71956 KB Output is correct
57 Correct 728 ms 74172 KB Output is correct
58 Correct 398 ms 74660 KB Output is correct
59 Correct 421 ms 74792 KB Output is correct
60 Correct 349 ms 72896 KB Output is correct
61 Correct 422 ms 74552 KB Output is correct
62 Correct 443 ms 74856 KB Output is correct
63 Correct 563 ms 70992 KB Output is correct
64 Correct 500 ms 72776 KB Output is correct
65 Correct 479 ms 74432 KB Output is correct
66 Correct 424 ms 74440 KB Output is correct
67 Correct 526 ms 74192 KB Output is correct
68 Correct 394 ms 69712 KB Output is correct
69 Correct 592 ms 72840 KB Output is correct
70 Correct 647 ms 72180 KB Output is correct
71 Correct 498 ms 77904 KB Output is correct
72 Correct 414 ms 78164 KB Output is correct
73 Correct 364 ms 70820 KB Output is correct
74 Correct 355 ms 71512 KB Output is correct
75 Correct 343 ms 71048 KB Output is correct
76 Correct 376 ms 69968 KB Output is correct
77 Correct 381 ms 69964 KB Output is correct
78 Correct 487 ms 71252 KB Output is correct
79 Correct 655 ms 74152 KB Output is correct
80 Correct 476 ms 71628 KB Output is correct
81 Correct 523 ms 73792 KB Output is correct
82 Correct 645 ms 74136 KB Output is correct
83 Correct 654 ms 74064 KB Output is correct
84 Correct 517 ms 74192 KB Output is correct
85 Correct 489 ms 70868 KB Output is correct
86 Correct 447 ms 70984 KB Output is correct
87 Correct 435 ms 69452 KB Output is correct
88 Correct 628 ms 74172 KB Output is correct
89 Correct 4 ms 21084 KB Output is correct
90 Correct 5 ms 21084 KB Output is correct
91 Correct 4 ms 21080 KB Output is correct
92 Correct 4 ms 21080 KB Output is correct
93 Correct 4 ms 21092 KB Output is correct
94 Correct 4 ms 21084 KB Output is correct
95 Correct 4 ms 21084 KB Output is correct
96 Correct 16 ms 23128 KB Output is correct
97 Correct 17 ms 23284 KB Output is correct
98 Correct 16 ms 23132 KB Output is correct
99 Correct 16 ms 23132 KB Output is correct
100 Correct 16 ms 23132 KB Output is correct
101 Correct 16 ms 23276 KB Output is correct
102 Correct 16 ms 23132 KB Output is correct
103 Correct 16 ms 23128 KB Output is correct
104 Correct 16 ms 23132 KB Output is correct
105 Correct 24 ms 24096 KB Output is correct
106 Correct 26 ms 24144 KB Output is correct
107 Correct 36 ms 24656 KB Output is correct
108 Correct 58 ms 25168 KB Output is correct
109 Correct 66 ms 25764 KB Output is correct
110 Correct 58 ms 25296 KB Output is correct
111 Correct 111 ms 27220 KB Output is correct
112 Correct 72 ms 25772 KB Output is correct
113 Correct 19 ms 24012 KB Output is correct
114 Correct 41 ms 24756 KB Output is correct
115 Correct 24 ms 24152 KB Output is correct
116 Correct 19 ms 24152 KB Output is correct
117 Correct 19 ms 23900 KB Output is correct
118 Correct 19 ms 23896 KB Output is correct
119 Correct 17 ms 23128 KB Output is correct
120 Correct 17 ms 22876 KB Output is correct
121 Correct 20 ms 23900 KB Output is correct
122 Correct 28 ms 24028 KB Output is correct
123 Correct 41 ms 24840 KB Output is correct
124 Correct 109 ms 27272 KB Output is correct
125 Correct 560 ms 73620 KB Output is correct
126 Correct 644 ms 73556 KB Output is correct
127 Correct 624 ms 74696 KB Output is correct
128 Correct 682 ms 77028 KB Output is correct
129 Correct 398 ms 76740 KB Output is correct
130 Correct 450 ms 77648 KB Output is correct
131 Correct 418 ms 77520 KB Output is correct
132 Correct 412 ms 77720 KB Output is correct
133 Correct 451 ms 77508 KB Output is correct
134 Correct 519 ms 73976 KB Output is correct
135 Correct 496 ms 74968 KB Output is correct
136 Correct 445 ms 77252 KB Output is correct
137 Correct 444 ms 77680 KB Output is correct
138 Correct 581 ms 77192 KB Output is correct
139 Correct 427 ms 72020 KB Output is correct
140 Correct 603 ms 75860 KB Output is correct
141 Correct 621 ms 75088 KB Output is correct
142 Correct 452 ms 80928 KB Output is correct
143 Correct 452 ms 81084 KB Output is correct
144 Correct 395 ms 73432 KB Output is correct
145 Correct 372 ms 74264 KB Output is correct
146 Correct 368 ms 73652 KB Output is correct
147 Correct 369 ms 71964 KB Output is correct
148 Correct 392 ms 72024 KB Output is correct
149 Correct 461 ms 73808 KB Output is correct
150 Correct 652 ms 76984 KB Output is correct
151 Correct 545 ms 74836 KB Output is correct
152 Correct 576 ms 77204 KB Output is correct
153 Correct 588 ms 77272 KB Output is correct
154 Correct 611 ms 77392 KB Output is correct
155 Correct 573 ms 76884 KB Output is correct
156 Correct 499 ms 73632 KB Output is correct
157 Correct 426 ms 74164 KB Output is correct
158 Correct 431 ms 70644 KB Output is correct
159 Correct 637 ms 76984 KB Output is correct