Submission #655566

# Submission time Handle Problem Language Result Execution time Memory
655566 2022-11-04T18:07:12 Z Alexandruabcde Swapping Cities (APIO20_swap) C++14
13 / 100
182 ms 56436 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;
    WhatNode[repr_y] = 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;
}

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][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 5 ms 7380 KB Output is correct
2 Correct 5 ms 7380 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7508 KB Output is correct
5 Correct 5 ms 7764 KB Output is correct
6 Correct 5 ms 7764 KB Output is correct
7 Correct 5 ms 7764 KB Output is correct
8 Correct 5 ms 7764 KB Output is correct
9 Correct 82 ms 42432 KB Output is correct
10 Correct 108 ms 46288 KB Output is correct
11 Correct 101 ms 45960 KB Output is correct
12 Correct 108 ms 47208 KB Output is correct
13 Correct 96 ms 50176 KB Output is correct
14 Correct 100 ms 42784 KB Output is correct
15 Correct 165 ms 50236 KB Output is correct
16 Correct 149 ms 49648 KB Output is correct
17 Correct 151 ms 50904 KB Output is correct
18 Correct 144 ms 54068 KB Output is correct
19 Correct 60 ms 18888 KB Output is correct
20 Correct 157 ms 50104 KB Output is correct
21 Correct 168 ms 49468 KB Output is correct
22 Correct 182 ms 50944 KB Output is correct
23 Correct 151 ms 54068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7380 KB Output is correct
2 Correct 5 ms 7380 KB Output is correct
3 Correct 148 ms 55224 KB Output is correct
4 Correct 182 ms 56436 KB Output is correct
5 Correct 144 ms 55812 KB Output is correct
6 Correct 140 ms 56264 KB Output is correct
7 Correct 151 ms 56128 KB Output is correct
8 Correct 138 ms 54924 KB Output is correct
9 Correct 142 ms 55856 KB Output is correct
10 Correct 157 ms 54740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7380 KB Output is correct
2 Correct 5 ms 7380 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7508 KB Output is correct
5 Correct 5 ms 7764 KB Output is correct
6 Correct 5 ms 7764 KB Output is correct
7 Correct 5 ms 7764 KB Output is correct
8 Correct 5 ms 7764 KB Output is correct
9 Correct 4 ms 7380 KB Output is correct
10 Correct 5 ms 7768 KB Output is correct
11 Incorrect 5 ms 7764 KB Output isn't correct
12 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 5 ms 7380 KB Output is correct
3 Correct 5 ms 7380 KB Output is correct
4 Correct 4 ms 7380 KB Output is correct
5 Correct 4 ms 7508 KB Output is correct
6 Correct 5 ms 7764 KB Output is correct
7 Correct 5 ms 7764 KB Output is correct
8 Correct 5 ms 7764 KB Output is correct
9 Correct 5 ms 7764 KB Output is correct
10 Correct 82 ms 42432 KB Output is correct
11 Correct 108 ms 46288 KB Output is correct
12 Correct 101 ms 45960 KB Output is correct
13 Correct 108 ms 47208 KB Output is correct
14 Correct 96 ms 50176 KB Output is correct
15 Correct 5 ms 7768 KB Output is correct
16 Incorrect 5 ms 7764 KB Output isn't correct
17 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7380 KB Output is correct
2 Correct 5 ms 7380 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7508 KB Output is correct
5 Correct 5 ms 7764 KB Output is correct
6 Correct 5 ms 7764 KB Output is correct
7 Correct 5 ms 7764 KB Output is correct
8 Correct 5 ms 7764 KB Output is correct
9 Correct 82 ms 42432 KB Output is correct
10 Correct 108 ms 46288 KB Output is correct
11 Correct 101 ms 45960 KB Output is correct
12 Correct 108 ms 47208 KB Output is correct
13 Correct 96 ms 50176 KB Output is correct
14 Correct 100 ms 42784 KB Output is correct
15 Correct 165 ms 50236 KB Output is correct
16 Correct 149 ms 49648 KB Output is correct
17 Correct 151 ms 50904 KB Output is correct
18 Correct 144 ms 54068 KB Output is correct
19 Correct 148 ms 55224 KB Output is correct
20 Correct 182 ms 56436 KB Output is correct
21 Correct 144 ms 55812 KB Output is correct
22 Correct 140 ms 56264 KB Output is correct
23 Correct 151 ms 56128 KB Output is correct
24 Correct 138 ms 54924 KB Output is correct
25 Correct 142 ms 55856 KB Output is correct
26 Correct 157 ms 54740 KB Output is correct
27 Correct 5 ms 7768 KB Output is correct
28 Incorrect 5 ms 7764 KB Output isn't correct
29 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 5 ms 7380 KB Output is correct
3 Correct 5 ms 7380 KB Output is correct
4 Correct 4 ms 7380 KB Output is correct
5 Correct 4 ms 7508 KB Output is correct
6 Correct 5 ms 7764 KB Output is correct
7 Correct 5 ms 7764 KB Output is correct
8 Correct 5 ms 7764 KB Output is correct
9 Correct 5 ms 7764 KB Output is correct
10 Correct 82 ms 42432 KB Output is correct
11 Correct 108 ms 46288 KB Output is correct
12 Correct 101 ms 45960 KB Output is correct
13 Correct 108 ms 47208 KB Output is correct
14 Correct 96 ms 50176 KB Output is correct
15 Correct 100 ms 42784 KB Output is correct
16 Correct 165 ms 50236 KB Output is correct
17 Correct 149 ms 49648 KB Output is correct
18 Correct 151 ms 50904 KB Output is correct
19 Correct 144 ms 54068 KB Output is correct
20 Correct 148 ms 55224 KB Output is correct
21 Correct 182 ms 56436 KB Output is correct
22 Correct 144 ms 55812 KB Output is correct
23 Correct 140 ms 56264 KB Output is correct
24 Correct 151 ms 56128 KB Output is correct
25 Correct 138 ms 54924 KB Output is correct
26 Correct 142 ms 55856 KB Output is correct
27 Correct 157 ms 54740 KB Output is correct
28 Correct 5 ms 7768 KB Output is correct
29 Incorrect 5 ms 7764 KB Output isn't correct
30 Halted 0 ms 0 KB -