Submission #655572

# Submission time Handle Problem Language Result Execution time Memory
655572 2022-11-04T18:19:35 Z Alexandruabcde Swapping Cities (APIO20_swap) C++14
100 / 100
221 ms 91596 KB
#include "swap.h"

#include <bits/stdc++.h>

using namespace std;

typedef pair <int, pair <int, int> > PIII;

constexpr int NMAX = 1e5 + 2;
constexpr int MMAX = 2e5 + 2;

int WhatNode[NMAX+MMAX];
int Repr[NMAX+MMAX];
int Sz[NMAX+MMAX];
int Grad[NMAX];

int Reprezentant (int node) {
    if (node == Repr[node]) return node;

    return Repr[node] = Reprezentant(Repr[node]);
}

int Dad[NMAX+MMAX];
int Value[NMAX+MMAX];
bool Good[NMAX+MMAX];

vector <int> G[NMAX + MMAX];

void Unify (int x, int y, int new_nod, int lg) {
    int repr_x = Reprezentant(x);
    int repr_y = Reprezentant(y);

    ++ Grad[x];
    ++ Grad[y];

    if (repr_x == repr_y) {
        Dad[WhatNode[repr_x]] = new_nod;
        WhatNode[repr_x] = new_nod;
        Good[new_nod] = true;
        Value[new_nod] = lg;

        return;
    }

    if (Sz[repr_x] > Sz[repr_y]) swap(repr_x, repr_y);

    Repr[repr_x] = repr_y;
    Sz[repr_y] += Sz[repr_x];
    Dad[WhatNode[repr_y]] = new_nod;
    Dad[WhatNode[repr_x]] = new_nod;
    Value[new_nod] = lg;

    Good[new_nod] = false;
    Good[new_nod] = (Good[WhatNode[repr_x]]|Good[WhatNode[repr_y]]);
    if (Grad[x] >= 3 || Grad[y] >= 3) Good[new_nod] = true;
    WhatNode[repr_y] = new_nod;
}

int Best[NMAX + MMAX];
vector <int> Order;
int Level[NMAX + MMAX];

void Dfs (int Node) {
    Order.push_back(Node);
    if (Good[Node]) Best[Node] = Node;

    for (auto it : G[Node]) {
        Best[it] = Best[Node];
        Level[it] = Level[Node] + 1;

        Dfs(it);
        Order.push_back(Node);
    }
}

int Lg[2 * (NMAX+MMAX)];
int pos[NMAX+MMAX];
int RMQ[20][2 * (NMAX+MMAX)];

void Precompute (int Root) {
    Best[Root] = -1;
    Level[Root] = 0;

    Dfs(Root);

    Lg[1] = 0;
    for (int i = 2; i <= Order.size(); ++ i )
        Lg[i] = Lg[i/2] + 1;

    for (int i = 0; i <= Root; ++ i )
        pos[i] = -1;

    for (int i = 0; i < Order.size(); ++ i )
        if (pos[Order[i]] == -1) pos[Order[i]] = i;

    for (int i = 0; i < Order.size(); ++ i )
        RMQ[0][i] = Order[i];

    for (int lg = 1; lg <= Lg[Order.size()]; ++ lg ) {
        for (int i = 0; i + (1<<lg) < Order.size(); ++ i ) {
            if (Level[RMQ[lg-1][i]] < Level[RMQ[lg-1][i + (1<<(lg-1))]])
                RMQ[lg][i] = RMQ[lg-1][i];
            else RMQ[lg][i] = RMQ[lg-1][i + (1<<(lg-1))];
        }
    }
}

int LCA (int x, int y) {
    int st = min(pos[x], pos[y]);
    int dr = max(pos[x], pos[y]);

    int k = Lg[dr - st + 1];

    if (Level[RMQ[k][st]] < Level[RMQ[k][dr - (1<<k) + 1]])
        return RMQ[k][st];
    else return RMQ[k][dr - (1<<k) + 1];
}

void init(int N, int M,
          std::vector<int> U, std::vector<int> V, std::vector<int> W) {

    vector <PIII> Edges;
    for (int i = 0; i < M; ++ i ) {
        Edges.push_back({W[i], {U[i], V[i]}});
    }

    sort(Edges.begin(), Edges.end());
    for (int i = 0; i < N; ++ i ) {
        WhatNode[i] = i;
        Repr[i] = i;
        Sz[i] = 1;
    }

    for (int i = 0; i < Edges.size(); ++ i )
        Unify(Edges[i].second.first, Edges[i].second.second, N + i, Edges[i].first);

    for (int i = 0; i < N + M - 1; ++ i )
        G[Dad[i]].push_back(i);

    Precompute(N + M - 1);
}

int getMinimumFuelCapacity(int X, int Y) {
    int Node = Best[LCA(X, Y)];

    if (Node == -1) return -1;

    return Value[Node];
}

Compilation message

swap.cpp: In function 'void Precompute(int)':
swap.cpp:87:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |     for (int i = 2; i <= Order.size(); ++ i )
      |                     ~~^~~~~~~~~~~~~~~
swap.cpp:93:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   93 |     for (int i = 0; i < Order.size(); ++ i )
      |                     ~~^~~~~~~~~~~~~~
swap.cpp:96:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   96 |     for (int i = 0; i < Order.size(); ++ i )
      |                     ~~^~~~~~~~~~~~~~
swap.cpp:100:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  100 |         for (int i = 0; i + (1<<lg) < Order.size(); ++ i ) {
      |                         ~~~~~~~~~~~~^~~~~~~~~~~~~~
swap.cpp: In function 'void init(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
swap.cpp:134:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  134 |     for (int i = 0; i < Edges.size(); ++ i )
      |                     ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7636 KB Output is correct
5 Correct 4 ms 7764 KB Output is correct
6 Correct 4 ms 7764 KB Output is correct
7 Correct 5 ms 7836 KB Output is correct
8 Correct 4 ms 7764 KB Output is correct
9 Correct 79 ms 41540 KB Output is correct
10 Correct 96 ms 49312 KB Output is correct
11 Correct 101 ms 48684 KB Output is correct
12 Correct 101 ms 51088 KB Output is correct
13 Correct 91 ms 54284 KB Output is correct
14 Correct 81 ms 41628 KB Output is correct
15 Correct 148 ms 51244 KB Output is correct
16 Correct 147 ms 50108 KB Output is correct
17 Correct 153 ms 52696 KB Output is correct
18 Correct 145 ms 55784 KB Output is correct
19 Correct 64 ms 17008 KB Output is correct
20 Correct 149 ms 51440 KB Output is correct
21 Correct 149 ms 49600 KB Output is correct
22 Correct 158 ms 52724 KB Output is correct
23 Correct 162 ms 55888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 139 ms 56164 KB Output is correct
4 Correct 143 ms 58732 KB Output is correct
5 Correct 147 ms 56892 KB Output is correct
6 Correct 138 ms 58556 KB Output is correct
7 Correct 143 ms 57860 KB Output is correct
8 Correct 144 ms 55680 KB Output is correct
9 Correct 138 ms 57528 KB Output is correct
10 Correct 140 ms 55228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7636 KB Output is correct
5 Correct 4 ms 7764 KB Output is correct
6 Correct 4 ms 7764 KB Output is correct
7 Correct 5 ms 7836 KB Output is correct
8 Correct 4 ms 7764 KB Output is correct
9 Correct 4 ms 7380 KB Output is correct
10 Correct 4 ms 7764 KB Output is correct
11 Correct 4 ms 7764 KB Output is correct
12 Correct 4 ms 7764 KB Output is correct
13 Correct 4 ms 7764 KB Output is correct
14 Correct 4 ms 7764 KB Output is correct
15 Correct 4 ms 7808 KB Output is correct
16 Correct 5 ms 7764 KB Output is correct
17 Correct 5 ms 7764 KB Output is correct
18 Correct 5 ms 7764 KB Output is correct
19 Correct 4 ms 7764 KB Output is correct
20 Correct 5 ms 7764 KB Output is correct
21 Correct 5 ms 7764 KB Output is correct
22 Correct 5 ms 7996 KB Output is correct
23 Correct 4 ms 7764 KB Output is correct
24 Correct 5 ms 8020 KB Output is correct
25 Correct 5 ms 8084 KB Output is correct
26 Correct 5 ms 8020 KB Output is correct
27 Correct 5 ms 7892 KB Output is correct
28 Correct 5 ms 8020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7380 KB Output is correct
5 Correct 4 ms 7636 KB Output is correct
6 Correct 4 ms 7764 KB Output is correct
7 Correct 4 ms 7764 KB Output is correct
8 Correct 5 ms 7836 KB Output is correct
9 Correct 4 ms 7764 KB Output is correct
10 Correct 79 ms 41540 KB Output is correct
11 Correct 96 ms 49312 KB Output is correct
12 Correct 101 ms 48684 KB Output is correct
13 Correct 101 ms 51088 KB Output is correct
14 Correct 91 ms 54284 KB Output is correct
15 Correct 4 ms 7764 KB Output is correct
16 Correct 4 ms 7764 KB Output is correct
17 Correct 4 ms 7764 KB Output is correct
18 Correct 4 ms 7764 KB Output is correct
19 Correct 4 ms 7764 KB Output is correct
20 Correct 4 ms 7808 KB Output is correct
21 Correct 5 ms 7764 KB Output is correct
22 Correct 5 ms 7764 KB Output is correct
23 Correct 5 ms 7764 KB Output is correct
24 Correct 4 ms 7764 KB Output is correct
25 Correct 5 ms 7764 KB Output is correct
26 Correct 5 ms 7764 KB Output is correct
27 Correct 5 ms 7996 KB Output is correct
28 Correct 4 ms 7764 KB Output is correct
29 Correct 5 ms 8020 KB Output is correct
30 Correct 5 ms 8084 KB Output is correct
31 Correct 5 ms 8020 KB Output is correct
32 Correct 5 ms 7892 KB Output is correct
33 Correct 5 ms 8020 KB Output is correct
34 Correct 14 ms 12836 KB Output is correct
35 Correct 97 ms 51100 KB Output is correct
36 Correct 99 ms 52904 KB Output is correct
37 Correct 114 ms 52884 KB Output is correct
38 Correct 98 ms 52284 KB Output is correct
39 Correct 101 ms 52016 KB Output is correct
40 Correct 87 ms 48232 KB Output is correct
41 Correct 100 ms 53312 KB Output is correct
42 Correct 102 ms 52916 KB Output is correct
43 Correct 90 ms 57144 KB Output is correct
44 Correct 104 ms 53308 KB Output is correct
45 Correct 119 ms 68704 KB Output is correct
46 Correct 98 ms 52924 KB Output is correct
47 Correct 98 ms 53052 KB Output is correct
48 Correct 104 ms 57788 KB Output is correct
49 Correct 94 ms 60016 KB Output is correct
50 Correct 72 ms 47884 KB Output is correct
51 Correct 103 ms 59148 KB Output is correct
52 Correct 140 ms 74868 KB Output is correct
53 Correct 164 ms 79568 KB Output is correct
54 Correct 172 ms 87760 KB Output is correct
55 Correct 96 ms 56772 KB Output is correct
56 Correct 150 ms 80252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7636 KB Output is correct
5 Correct 4 ms 7764 KB Output is correct
6 Correct 4 ms 7764 KB Output is correct
7 Correct 5 ms 7836 KB Output is correct
8 Correct 4 ms 7764 KB Output is correct
9 Correct 79 ms 41540 KB Output is correct
10 Correct 96 ms 49312 KB Output is correct
11 Correct 101 ms 48684 KB Output is correct
12 Correct 101 ms 51088 KB Output is correct
13 Correct 91 ms 54284 KB Output is correct
14 Correct 81 ms 41628 KB Output is correct
15 Correct 148 ms 51244 KB Output is correct
16 Correct 147 ms 50108 KB Output is correct
17 Correct 153 ms 52696 KB Output is correct
18 Correct 145 ms 55784 KB Output is correct
19 Correct 139 ms 56164 KB Output is correct
20 Correct 143 ms 58732 KB Output is correct
21 Correct 147 ms 56892 KB Output is correct
22 Correct 138 ms 58556 KB Output is correct
23 Correct 143 ms 57860 KB Output is correct
24 Correct 144 ms 55680 KB Output is correct
25 Correct 138 ms 57528 KB Output is correct
26 Correct 140 ms 55228 KB Output is correct
27 Correct 4 ms 7764 KB Output is correct
28 Correct 4 ms 7764 KB Output is correct
29 Correct 4 ms 7764 KB Output is correct
30 Correct 4 ms 7764 KB Output is correct
31 Correct 4 ms 7764 KB Output is correct
32 Correct 4 ms 7808 KB Output is correct
33 Correct 5 ms 7764 KB Output is correct
34 Correct 5 ms 7764 KB Output is correct
35 Correct 5 ms 7764 KB Output is correct
36 Correct 14 ms 12836 KB Output is correct
37 Correct 97 ms 51100 KB Output is correct
38 Correct 99 ms 52904 KB Output is correct
39 Correct 114 ms 52884 KB Output is correct
40 Correct 98 ms 52284 KB Output is correct
41 Correct 101 ms 52016 KB Output is correct
42 Correct 87 ms 48232 KB Output is correct
43 Correct 100 ms 53312 KB Output is correct
44 Correct 102 ms 52916 KB Output is correct
45 Correct 90 ms 57144 KB Output is correct
46 Correct 104 ms 53308 KB Output is correct
47 Correct 19 ms 12944 KB Output is correct
48 Correct 172 ms 56852 KB Output is correct
49 Correct 153 ms 56868 KB Output is correct
50 Correct 158 ms 56788 KB Output is correct
51 Correct 160 ms 56528 KB Output is correct
52 Correct 160 ms 53760 KB Output is correct
53 Correct 121 ms 41804 KB Output is correct
54 Correct 162 ms 57188 KB Output is correct
55 Correct 155 ms 56872 KB Output is correct
56 Correct 162 ms 60092 KB Output is correct
57 Correct 157 ms 57180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7380 KB Output is correct
5 Correct 4 ms 7636 KB Output is correct
6 Correct 4 ms 7764 KB Output is correct
7 Correct 4 ms 7764 KB Output is correct
8 Correct 5 ms 7836 KB Output is correct
9 Correct 4 ms 7764 KB Output is correct
10 Correct 79 ms 41540 KB Output is correct
11 Correct 96 ms 49312 KB Output is correct
12 Correct 101 ms 48684 KB Output is correct
13 Correct 101 ms 51088 KB Output is correct
14 Correct 91 ms 54284 KB Output is correct
15 Correct 81 ms 41628 KB Output is correct
16 Correct 148 ms 51244 KB Output is correct
17 Correct 147 ms 50108 KB Output is correct
18 Correct 153 ms 52696 KB Output is correct
19 Correct 145 ms 55784 KB Output is correct
20 Correct 139 ms 56164 KB Output is correct
21 Correct 143 ms 58732 KB Output is correct
22 Correct 147 ms 56892 KB Output is correct
23 Correct 138 ms 58556 KB Output is correct
24 Correct 143 ms 57860 KB Output is correct
25 Correct 144 ms 55680 KB Output is correct
26 Correct 138 ms 57528 KB Output is correct
27 Correct 140 ms 55228 KB Output is correct
28 Correct 4 ms 7764 KB Output is correct
29 Correct 4 ms 7764 KB Output is correct
30 Correct 4 ms 7764 KB Output is correct
31 Correct 4 ms 7764 KB Output is correct
32 Correct 4 ms 7764 KB Output is correct
33 Correct 4 ms 7808 KB Output is correct
34 Correct 5 ms 7764 KB Output is correct
35 Correct 5 ms 7764 KB Output is correct
36 Correct 5 ms 7764 KB Output is correct
37 Correct 14 ms 12836 KB Output is correct
38 Correct 97 ms 51100 KB Output is correct
39 Correct 99 ms 52904 KB Output is correct
40 Correct 114 ms 52884 KB Output is correct
41 Correct 98 ms 52284 KB Output is correct
42 Correct 101 ms 52016 KB Output is correct
43 Correct 87 ms 48232 KB Output is correct
44 Correct 100 ms 53312 KB Output is correct
45 Correct 102 ms 52916 KB Output is correct
46 Correct 90 ms 57144 KB Output is correct
47 Correct 104 ms 53308 KB Output is correct
48 Correct 19 ms 12944 KB Output is correct
49 Correct 172 ms 56852 KB Output is correct
50 Correct 153 ms 56868 KB Output is correct
51 Correct 158 ms 56788 KB Output is correct
52 Correct 160 ms 56528 KB Output is correct
53 Correct 160 ms 53760 KB Output is correct
54 Correct 121 ms 41804 KB Output is correct
55 Correct 162 ms 57188 KB Output is correct
56 Correct 155 ms 56872 KB Output is correct
57 Correct 162 ms 60092 KB Output is correct
58 Correct 157 ms 57180 KB Output is correct
59 Correct 64 ms 17008 KB Output is correct
60 Correct 149 ms 51440 KB Output is correct
61 Correct 149 ms 49600 KB Output is correct
62 Correct 158 ms 52724 KB Output is correct
63 Correct 162 ms 55888 KB Output is correct
64 Correct 4 ms 7764 KB Output is correct
65 Correct 5 ms 7764 KB Output is correct
66 Correct 5 ms 7764 KB Output is correct
67 Correct 5 ms 7996 KB Output is correct
68 Correct 4 ms 7764 KB Output is correct
69 Correct 5 ms 8020 KB Output is correct
70 Correct 5 ms 8084 KB Output is correct
71 Correct 5 ms 8020 KB Output is correct
72 Correct 5 ms 7892 KB Output is correct
73 Correct 5 ms 8020 KB Output is correct
74 Correct 119 ms 68704 KB Output is correct
75 Correct 98 ms 52924 KB Output is correct
76 Correct 98 ms 53052 KB Output is correct
77 Correct 104 ms 57788 KB Output is correct
78 Correct 94 ms 60016 KB Output is correct
79 Correct 72 ms 47884 KB Output is correct
80 Correct 103 ms 59148 KB Output is correct
81 Correct 140 ms 74868 KB Output is correct
82 Correct 164 ms 79568 KB Output is correct
83 Correct 172 ms 87760 KB Output is correct
84 Correct 96 ms 56772 KB Output is correct
85 Correct 150 ms 80252 KB Output is correct
86 Correct 55 ms 28736 KB Output is correct
87 Correct 153 ms 56804 KB Output is correct
88 Correct 156 ms 56792 KB Output is correct
89 Correct 155 ms 59304 KB Output is correct
90 Correct 144 ms 66508 KB Output is correct
91 Correct 144 ms 62332 KB Output is correct
92 Correct 152 ms 61032 KB Output is correct
93 Correct 208 ms 77800 KB Output is correct
94 Correct 216 ms 83252 KB Output is correct
95 Correct 221 ms 91596 KB Output is correct
96 Correct 151 ms 60388 KB Output is correct
97 Correct 190 ms 71796 KB Output is correct