Submission #429658

# Submission time Handle Problem Language Result Execution time Memory
429658 2021-06-16T08:29:50 Z snasibov05 Swapping Cities (APIO20_swap) C++14
100 / 100
435 ms 32056 KB
#include "swap.h"
#include <vector>
#include <algorithm>

using namespace std;

#define pii pair<int, int>
#define f first
#define s second
#define pb push_back
#define oo 1000000001

struct edge{
    int u, v;
    int w;

    bool operator< (edge e) const{
        return w < e.w;
    }
};


vector<int> pr;
vector<edge> ed;
vector<int> deg;
vector<vector<int>> cmp;
vector<vector<pii>> arr;
vector<int> when;

void Union(edge e){
    deg[e.u]++; deg[e.v]++;

    int x = pr[e.u]; int y = pr[e.v];

    if (x == y){
        when[x] = min(when[x], e.w);
        return;
    }

    if (cmp[x].size() < cmp[y].size()) swap(x, y);

    for (auto node : cmp[y]){
        cmp[x].pb(node);
        pr[node] = x;
        arr[node].pb({x, e.w});
    }

    cmp[y].clear();
    if (when[y] != oo) when[x] = min(when[x], e.w);
    if (deg[e.u] >= 3 || deg[e.v] >= 3) when[x] = min(when[x], e.w);
}

void init(int n, int m, vector<int> u, vector<int> v, vector<int> w) {
    pr.resize(n);
    ed.resize(m);
    deg.resize(n);
    cmp.resize(n);
    arr.resize(n);
    when.resize(n);

    for (int i = 0; i < m; ++i) {
        ed[i].u = u[i];
        ed[i].v = v[i];
        ed[i].w = w[i];
    }

    for (int i = 0; i < n; ++i) {
        pr[i] = i;
        cmp[i].pb(i);
        deg[i] = 0;
        arr[i].pb({i, 0});
        when[i] = oo;
    }


    sort(ed.begin(), ed.end());

    for (int i = 0; i < m; ++i) {
        Union(ed[i]);
    }

}

int getMinimumFuelCapacity(int x, int y) {

    int ans = 0;
    if (when[pr[x]] == oo) return -1;
    int cmpx = x, cmpy = y;
    int a = 0, b = 0;

    while (cmpx != cmpy || when[cmpx] == oo){
        if (a == arr[x].size() || (b != arr[y].size() && arr[y][b].s < arr[x][a].s)){
            cmpy = arr[y][b].f;
            ans = arr[y][b++].s;
        } else {
            cmpx = arr[x][a].f;
            ans = arr[x][a++].s;
        }
    }

    ans = max(ans, when[cmpx]);

    return ans;
}

Compilation message

swap.cpp: In function 'int getMinimumFuelCapacity(int, int)':
swap.cpp:92:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   92 |         if (a == arr[x].size() || (b != arr[y].size() && arr[y][b].s < arr[x][a].s)){
      |             ~~^~~~~~~~~~~~~~~~
swap.cpp:92:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   92 |         if (a == arr[x].size() || (b != arr[y].size() && arr[y][b].s < arr[x][a].s)){
      |                                    ~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 178 ms 20888 KB Output is correct
10 Correct 209 ms 25172 KB Output is correct
11 Correct 206 ms 24688 KB Output is correct
12 Correct 225 ms 26212 KB Output is correct
13 Correct 149 ms 18868 KB Output is correct
14 Correct 164 ms 20932 KB Output is correct
15 Correct 264 ms 27564 KB Output is correct
16 Correct 258 ms 26708 KB Output is correct
17 Correct 316 ms 28436 KB Output is correct
18 Correct 175 ms 20560 KB Output is correct
19 Correct 93 ms 7652 KB Output is correct
20 Correct 336 ms 28648 KB Output is correct
21 Correct 323 ms 28040 KB Output is correct
22 Correct 356 ms 29616 KB Output is correct
23 Correct 209 ms 22008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 188 ms 18588 KB Output is correct
4 Correct 217 ms 19248 KB Output is correct
5 Correct 188 ms 18968 KB Output is correct
6 Correct 186 ms 19056 KB Output is correct
7 Correct 192 ms 19188 KB Output is correct
8 Correct 194 ms 18564 KB Output is correct
9 Correct 194 ms 18964 KB Output is correct
10 Correct 227 ms 18408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 2 ms 460 KB Output is correct
12 Correct 2 ms 460 KB Output is correct
13 Correct 2 ms 460 KB Output is correct
14 Correct 2 ms 412 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 1 ms 460 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 1 ms 460 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 2 ms 460 KB Output is correct
21 Correct 2 ms 512 KB Output is correct
22 Correct 5 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 2 ms 460 KB Output is correct
25 Correct 2 ms 460 KB Output is correct
26 Correct 2 ms 460 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 178 ms 20888 KB Output is correct
11 Correct 209 ms 25172 KB Output is correct
12 Correct 206 ms 24688 KB Output is correct
13 Correct 225 ms 26212 KB Output is correct
14 Correct 149 ms 18868 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 2 ms 460 KB Output is correct
17 Correct 2 ms 460 KB Output is correct
18 Correct 2 ms 460 KB Output is correct
19 Correct 2 ms 412 KB Output is correct
20 Correct 1 ms 460 KB Output is correct
21 Correct 1 ms 460 KB Output is correct
22 Correct 1 ms 460 KB Output is correct
23 Correct 1 ms 460 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 2 ms 460 KB Output is correct
26 Correct 2 ms 512 KB Output is correct
27 Correct 5 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 2 ms 460 KB Output is correct
30 Correct 2 ms 460 KB Output is correct
31 Correct 2 ms 460 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 460 KB Output is correct
34 Correct 26 ms 3376 KB Output is correct
35 Correct 262 ms 26284 KB Output is correct
36 Correct 255 ms 25864 KB Output is correct
37 Correct 257 ms 25520 KB Output is correct
38 Correct 237 ms 24516 KB Output is correct
39 Correct 241 ms 24068 KB Output is correct
40 Correct 182 ms 21736 KB Output is correct
41 Correct 321 ms 26656 KB Output is correct
42 Correct 237 ms 26680 KB Output is correct
43 Correct 128 ms 18008 KB Output is correct
44 Correct 212 ms 23980 KB Output is correct
45 Correct 148 ms 17972 KB Output is correct
46 Correct 267 ms 25868 KB Output is correct
47 Correct 188 ms 22412 KB Output is correct
48 Correct 154 ms 19632 KB Output is correct
49 Correct 70 ms 5988 KB Output is correct
50 Correct 52 ms 5664 KB Output is correct
51 Correct 123 ms 14796 KB Output is correct
52 Correct 251 ms 27372 KB Output is correct
53 Correct 192 ms 23356 KB Output is correct
54 Correct 279 ms 30152 KB Output is correct
55 Correct 118 ms 18224 KB Output is correct
56 Correct 173 ms 22204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 178 ms 20888 KB Output is correct
10 Correct 209 ms 25172 KB Output is correct
11 Correct 206 ms 24688 KB Output is correct
12 Correct 225 ms 26212 KB Output is correct
13 Correct 149 ms 18868 KB Output is correct
14 Correct 164 ms 20932 KB Output is correct
15 Correct 264 ms 27564 KB Output is correct
16 Correct 258 ms 26708 KB Output is correct
17 Correct 316 ms 28436 KB Output is correct
18 Correct 175 ms 20560 KB Output is correct
19 Correct 188 ms 18588 KB Output is correct
20 Correct 217 ms 19248 KB Output is correct
21 Correct 188 ms 18968 KB Output is correct
22 Correct 186 ms 19056 KB Output is correct
23 Correct 192 ms 19188 KB Output is correct
24 Correct 194 ms 18564 KB Output is correct
25 Correct 194 ms 18964 KB Output is correct
26 Correct 227 ms 18408 KB Output is correct
27 Correct 1 ms 460 KB Output is correct
28 Correct 2 ms 460 KB Output is correct
29 Correct 2 ms 460 KB Output is correct
30 Correct 2 ms 460 KB Output is correct
31 Correct 2 ms 412 KB Output is correct
32 Correct 1 ms 460 KB Output is correct
33 Correct 1 ms 460 KB Output is correct
34 Correct 1 ms 460 KB Output is correct
35 Correct 1 ms 460 KB Output is correct
36 Correct 26 ms 3376 KB Output is correct
37 Correct 262 ms 26284 KB Output is correct
38 Correct 255 ms 25864 KB Output is correct
39 Correct 257 ms 25520 KB Output is correct
40 Correct 237 ms 24516 KB Output is correct
41 Correct 241 ms 24068 KB Output is correct
42 Correct 182 ms 21736 KB Output is correct
43 Correct 321 ms 26656 KB Output is correct
44 Correct 237 ms 26680 KB Output is correct
45 Correct 128 ms 18008 KB Output is correct
46 Correct 212 ms 23980 KB Output is correct
47 Correct 27 ms 3456 KB Output is correct
48 Correct 434 ms 28904 KB Output is correct
49 Correct 368 ms 28192 KB Output is correct
50 Correct 403 ms 28104 KB Output is correct
51 Correct 357 ms 27492 KB Output is correct
52 Correct 347 ms 25636 KB Output is correct
53 Correct 266 ms 19216 KB Output is correct
54 Correct 435 ms 29228 KB Output is correct
55 Correct 401 ms 29480 KB Output is correct
56 Correct 236 ms 21292 KB Output is correct
57 Correct 325 ms 26888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 178 ms 20888 KB Output is correct
11 Correct 209 ms 25172 KB Output is correct
12 Correct 206 ms 24688 KB Output is correct
13 Correct 225 ms 26212 KB Output is correct
14 Correct 149 ms 18868 KB Output is correct
15 Correct 164 ms 20932 KB Output is correct
16 Correct 264 ms 27564 KB Output is correct
17 Correct 258 ms 26708 KB Output is correct
18 Correct 316 ms 28436 KB Output is correct
19 Correct 175 ms 20560 KB Output is correct
20 Correct 188 ms 18588 KB Output is correct
21 Correct 217 ms 19248 KB Output is correct
22 Correct 188 ms 18968 KB Output is correct
23 Correct 186 ms 19056 KB Output is correct
24 Correct 192 ms 19188 KB Output is correct
25 Correct 194 ms 18564 KB Output is correct
26 Correct 194 ms 18964 KB Output is correct
27 Correct 227 ms 18408 KB Output is correct
28 Correct 1 ms 460 KB Output is correct
29 Correct 2 ms 460 KB Output is correct
30 Correct 2 ms 460 KB Output is correct
31 Correct 2 ms 460 KB Output is correct
32 Correct 2 ms 412 KB Output is correct
33 Correct 1 ms 460 KB Output is correct
34 Correct 1 ms 460 KB Output is correct
35 Correct 1 ms 460 KB Output is correct
36 Correct 1 ms 460 KB Output is correct
37 Correct 26 ms 3376 KB Output is correct
38 Correct 262 ms 26284 KB Output is correct
39 Correct 255 ms 25864 KB Output is correct
40 Correct 257 ms 25520 KB Output is correct
41 Correct 237 ms 24516 KB Output is correct
42 Correct 241 ms 24068 KB Output is correct
43 Correct 182 ms 21736 KB Output is correct
44 Correct 321 ms 26656 KB Output is correct
45 Correct 237 ms 26680 KB Output is correct
46 Correct 128 ms 18008 KB Output is correct
47 Correct 212 ms 23980 KB Output is correct
48 Correct 27 ms 3456 KB Output is correct
49 Correct 434 ms 28904 KB Output is correct
50 Correct 368 ms 28192 KB Output is correct
51 Correct 403 ms 28104 KB Output is correct
52 Correct 357 ms 27492 KB Output is correct
53 Correct 347 ms 25636 KB Output is correct
54 Correct 266 ms 19216 KB Output is correct
55 Correct 435 ms 29228 KB Output is correct
56 Correct 401 ms 29480 KB Output is correct
57 Correct 236 ms 21292 KB Output is correct
58 Correct 325 ms 26888 KB Output is correct
59 Correct 93 ms 7652 KB Output is correct
60 Correct 336 ms 28648 KB Output is correct
61 Correct 323 ms 28040 KB Output is correct
62 Correct 356 ms 29616 KB Output is correct
63 Correct 209 ms 22008 KB Output is correct
64 Correct 1 ms 332 KB Output is correct
65 Correct 2 ms 460 KB Output is correct
66 Correct 2 ms 512 KB Output is correct
67 Correct 5 ms 332 KB Output is correct
68 Correct 1 ms 332 KB Output is correct
69 Correct 2 ms 460 KB Output is correct
70 Correct 2 ms 460 KB Output is correct
71 Correct 2 ms 460 KB Output is correct
72 Correct 1 ms 332 KB Output is correct
73 Correct 1 ms 460 KB Output is correct
74 Correct 148 ms 17972 KB Output is correct
75 Correct 267 ms 25868 KB Output is correct
76 Correct 188 ms 22412 KB Output is correct
77 Correct 154 ms 19632 KB Output is correct
78 Correct 70 ms 5988 KB Output is correct
79 Correct 52 ms 5664 KB Output is correct
80 Correct 123 ms 14796 KB Output is correct
81 Correct 251 ms 27372 KB Output is correct
82 Correct 192 ms 23356 KB Output is correct
83 Correct 279 ms 30152 KB Output is correct
84 Correct 118 ms 18224 KB Output is correct
85 Correct 173 ms 22204 KB Output is correct
86 Correct 61 ms 7432 KB Output is correct
87 Correct 343 ms 27904 KB Output is correct
88 Correct 344 ms 27940 KB Output is correct
89 Correct 224 ms 21060 KB Output is correct
90 Correct 137 ms 8472 KB Output is correct
91 Correct 144 ms 9788 KB Output is correct
92 Correct 213 ms 17688 KB Output is correct
93 Correct 340 ms 28908 KB Output is correct
94 Correct 369 ms 26132 KB Output is correct
95 Correct 391 ms 32056 KB Output is correct
96 Correct 232 ms 21164 KB Output is correct
97 Correct 267 ms 24080 KB Output is correct