Submission #673940

# Submission time Handle Problem Language Result Execution time Memory
673940 2022-12-22T12:40:21 Z c2zi6 Swapping Cities (APIO20_swap) C++14
36 / 100
369 ms 9776 KB
#include "swap.h"

#include <bits/stdc++.h>

using namespace std;

struct EDGE {
    int u, v, w;
};

struct DSUNODE;
vector<DSUNODE> nodes;
struct DSUNODE {
    int vert;
    int parent;
    int distToParent;
    int size;
    int left;
    int right;
    int when;
    int leader(int mxpath = 2e9) {
        if (parent == -1 || distToParent > mxpath) return vert;
        return nodes[nodes[parent].leader(mxpath)].vert;
    }
};

vector<EDGE> edges;

void init(int N, int M, std::vector<int> U, std::vector<int> V, std::vector<int> W) {
    edges = vector<EDGE>(M);
    nodes = vector<DSUNODE>(N);
    for (int i = 0; i < N; i++) {
        DSUNODE& a = nodes[i];
        a.parent = -1;
        a.distToParent = 0;
        a.size = 1;
        a.vert = a.left = a.right = i;
        a.when = 2e9;
    }
    for (int i = 0; i < M; i++) {
        edges[i] = {U[i], V[i], W[i]};
    }
    sort(edges.begin(), edges.end(), [](const EDGE& a, const EDGE& b){return a.w < b.w;});
    //cout << "WORKS" << endl;
    for (int i = 0; i < M; i++) {
        //cout << nodes[edges[i].u].leader() << " " << nodes[edges[i].v].leader() << endl;
        DSUNODE& u = nodes[nodes[edges[i].u].leader()];
        DSUNODE& v = nodes[nodes[edges[i].v].leader()];
        if (u.vert == v.vert) {
            if ((edges[i].u == u.left && edges[i].v == u.right) || (edges[i].u == u.right && edges[i].v == u.left)) {
                u.left = -1;
                u.right = -1;
                u.when = min(u.when, edges[i].w);
            }
            continue;
        }

        int newleft = -1, newright = -1;
        if (edges[i].u == u.left && edges[i].v == v.left) {
            newleft = u.right;
            newright = v.right;
        } else if (edges[i].u == u.left && edges[i].v == v.right) {
            newleft = u.right;
            newright = v.left;
        } else if (edges[i].u == u.right && edges[i].v == v.left) {
            newleft = u.left;
            newright = v.right;
        } else if (edges[i].u == u.right && edges[i].v == v.right) {
            newleft = u.left;
            newright = v.left;
        }

        if (u.size > v.size) {
            v.parent = u.vert;
            v.distToParent = edges[i].w;
            u.size += v.size;
            u.left = newleft;
            u.right = newright;
            if (newleft == -1) {
                u.when = min(u.when, edges[i].w);
            }
        } else {
            u.parent = v.vert;
            u.distToParent = edges[i].w;
            v.size += u.size;
            v.left = newleft;
            v.right = newright;
            if (newleft == -1) {
                v.when = min(v.when, edges[i].w);
            }
        }
    }
}

bool can(int X, int Y, int M) {
    int u = nodes[X].leader(M), v = nodes[Y].leader(M);
    return u == v && M >= nodes[u].when;
}

int getMinimumFuelCapacity(int X, int Y) {
    if (!can(X, Y, 1e9)) return -1;
    int l = 0, r = 1e9;
    while (l + 1 < r) {
        int m = (l + r) / 2;
        if (can(X, Y, m)) r = m;
        else l = m;
    }
    return r;
}

int mai2n() {
  int N, M;
  assert(2 == scanf("%d %d", &N, &M));

  std::vector<int> U(M), V(M), W(M);
  for (int i = 0; i < M; ++i) {
    assert(3 == scanf("%d %d %d", &U[i], &V[i], &W[i]));
  }

  int Q;
  assert(1 == scanf("%d", &Q));

  std::vector<int> X(Q), Y(Q);
  for (int i = 0; i < Q; ++i) {
    assert(2 == scanf("%d %d", &X[i], &Y[i]));
  }

  init(N, M, U, V, W);

  std::vector<int> minimum_fuel_capacities(Q);
  for (int i = 0; i < Q; ++i) {
    minimum_fuel_capacities[i] = getMinimumFuelCapacity(X[i], Y[i]);
  }

  for (int i = 0; i < Q; ++i) {
    printf("%d\n", minimum_fuel_capacities[i]);
  }

  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 352 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 33 ms 5204 KB Output is correct
10 Correct 40 ms 6228 KB Output is correct
11 Correct 39 ms 6228 KB Output is correct
12 Correct 43 ms 6572 KB Output is correct
13 Correct 41 ms 6556 KB Output is correct
14 Correct 40 ms 5332 KB Output is correct
15 Correct 111 ms 8128 KB Output is correct
16 Correct 114 ms 7964 KB Output is correct
17 Correct 114 ms 8236 KB Output is correct
18 Correct 96 ms 8264 KB Output is correct
19 Correct 207 ms 4644 KB Output is correct
20 Correct 315 ms 9272 KB Output is correct
21 Correct 329 ms 9396 KB Output is correct
22 Correct 346 ms 9664 KB Output is correct
23 Correct 139 ms 9668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 139 ms 9200 KB Output is correct
4 Correct 133 ms 9340 KB Output is correct
5 Correct 143 ms 9516 KB Output is correct
6 Correct 137 ms 9396 KB Output is correct
7 Correct 139 ms 9432 KB Output is correct
8 Correct 155 ms 9360 KB Output is correct
9 Correct 136 ms 9260 KB Output is correct
10 Correct 138 ms 9220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 352 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Incorrect 1 ms 340 KB Output isn't correct
25 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 352 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 33 ms 5204 KB Output is correct
11 Correct 40 ms 6228 KB Output is correct
12 Correct 39 ms 6228 KB Output is correct
13 Correct 43 ms 6572 KB Output is correct
14 Correct 41 ms 6556 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Incorrect 1 ms 340 KB Output isn't correct
30 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 352 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 33 ms 5204 KB Output is correct
10 Correct 40 ms 6228 KB Output is correct
11 Correct 39 ms 6228 KB Output is correct
12 Correct 43 ms 6572 KB Output is correct
13 Correct 41 ms 6556 KB Output is correct
14 Correct 40 ms 5332 KB Output is correct
15 Correct 111 ms 8128 KB Output is correct
16 Correct 114 ms 7964 KB Output is correct
17 Correct 114 ms 8236 KB Output is correct
18 Correct 96 ms 8264 KB Output is correct
19 Correct 139 ms 9200 KB Output is correct
20 Correct 133 ms 9340 KB Output is correct
21 Correct 143 ms 9516 KB Output is correct
22 Correct 137 ms 9396 KB Output is correct
23 Correct 139 ms 9432 KB Output is correct
24 Correct 155 ms 9360 KB Output is correct
25 Correct 136 ms 9260 KB Output is correct
26 Correct 138 ms 9220 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 6 ms 1108 KB Output is correct
37 Correct 41 ms 6456 KB Output is correct
38 Correct 42 ms 6472 KB Output is correct
39 Correct 38 ms 6484 KB Output is correct
40 Correct 41 ms 6456 KB Output is correct
41 Correct 38 ms 6460 KB Output is correct
42 Correct 34 ms 5844 KB Output is correct
43 Correct 42 ms 6484 KB Output is correct
44 Correct 40 ms 6472 KB Output is correct
45 Correct 36 ms 6468 KB Output is correct
46 Correct 42 ms 6556 KB Output is correct
47 Correct 25 ms 1356 KB Output is correct
48 Correct 351 ms 9140 KB Output is correct
49 Correct 332 ms 9148 KB Output is correct
50 Correct 326 ms 9028 KB Output is correct
51 Correct 306 ms 8976 KB Output is correct
52 Correct 276 ms 8692 KB Output is correct
53 Correct 241 ms 7932 KB Output is correct
54 Correct 369 ms 9776 KB Output is correct
55 Correct 364 ms 9024 KB Output is correct
56 Correct 140 ms 9016 KB Output is correct
57 Correct 260 ms 9664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 352 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 33 ms 5204 KB Output is correct
11 Correct 40 ms 6228 KB Output is correct
12 Correct 39 ms 6228 KB Output is correct
13 Correct 43 ms 6572 KB Output is correct
14 Correct 41 ms 6556 KB Output is correct
15 Correct 40 ms 5332 KB Output is correct
16 Correct 111 ms 8128 KB Output is correct
17 Correct 114 ms 7964 KB Output is correct
18 Correct 114 ms 8236 KB Output is correct
19 Correct 96 ms 8264 KB Output is correct
20 Correct 139 ms 9200 KB Output is correct
21 Correct 133 ms 9340 KB Output is correct
22 Correct 143 ms 9516 KB Output is correct
23 Correct 137 ms 9396 KB Output is correct
24 Correct 139 ms 9432 KB Output is correct
25 Correct 155 ms 9360 KB Output is correct
26 Correct 136 ms 9260 KB Output is correct
27 Correct 138 ms 9220 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 6 ms 1108 KB Output is correct
38 Correct 41 ms 6456 KB Output is correct
39 Correct 42 ms 6472 KB Output is correct
40 Correct 38 ms 6484 KB Output is correct
41 Correct 41 ms 6456 KB Output is correct
42 Correct 38 ms 6460 KB Output is correct
43 Correct 34 ms 5844 KB Output is correct
44 Correct 42 ms 6484 KB Output is correct
45 Correct 40 ms 6472 KB Output is correct
46 Correct 36 ms 6468 KB Output is correct
47 Correct 42 ms 6556 KB Output is correct
48 Correct 25 ms 1356 KB Output is correct
49 Correct 351 ms 9140 KB Output is correct
50 Correct 332 ms 9148 KB Output is correct
51 Correct 326 ms 9028 KB Output is correct
52 Correct 306 ms 8976 KB Output is correct
53 Correct 276 ms 8692 KB Output is correct
54 Correct 241 ms 7932 KB Output is correct
55 Correct 369 ms 9776 KB Output is correct
56 Correct 364 ms 9024 KB Output is correct
57 Correct 140 ms 9016 KB Output is correct
58 Correct 260 ms 9664 KB Output is correct
59 Correct 207 ms 4644 KB Output is correct
60 Correct 315 ms 9272 KB Output is correct
61 Correct 329 ms 9396 KB Output is correct
62 Correct 346 ms 9664 KB Output is correct
63 Correct 139 ms 9668 KB Output is correct
64 Correct 1 ms 340 KB Output is correct
65 Correct 1 ms 340 KB Output is correct
66 Correct 1 ms 340 KB Output is correct
67 Correct 1 ms 340 KB Output is correct
68 Correct 1 ms 340 KB Output is correct
69 Incorrect 1 ms 340 KB Output isn't correct
70 Halted 0 ms 0 KB -