Submission #655571

# Submission time Handle Problem Language Result Execution time Memory
655571 2022-11-04T18:17:03 Z Alexandruabcde Swapping Cities (APIO20_swap) C++14
30 / 100
177 ms 52536 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][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 3 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 3 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 4 ms 7764 KB Output is correct
7 Correct 4 ms 7764 KB Output is correct
8 Correct 4 ms 7736 KB Output is correct
9 Correct 83 ms 40792 KB Output is correct
10 Correct 95 ms 44188 KB Output is correct
11 Correct 90 ms 43884 KB Output is correct
12 Correct 97 ms 44932 KB Output is correct
13 Correct 90 ms 48060 KB Output is correct
14 Correct 81 ms 40784 KB Output is correct
15 Correct 147 ms 45872 KB Output is correct
16 Correct 142 ms 45416 KB Output is correct
17 Correct 156 ms 46504 KB Output is correct
18 Correct 141 ms 49600 KB Output is correct
19 Correct 62 ms 16852 KB Output is correct
20 Correct 149 ms 45920 KB Output is correct
21 Correct 177 ms 45304 KB Output is correct
22 Correct 160 ms 46572 KB Output is correct
23 Correct 148 ms 49640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 140 ms 51360 KB Output is correct
4 Correct 166 ms 52536 KB Output is correct
5 Correct 143 ms 51772 KB Output is correct
6 Correct 145 ms 52456 KB Output is correct
7 Correct 151 ms 52136 KB Output is correct
8 Correct 143 ms 51132 KB Output is correct
9 Correct 145 ms 51972 KB Output is correct
10 Correct 146 ms 51004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 3 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 4 ms 7764 KB Output is correct
7 Correct 4 ms 7764 KB Output is correct
8 Correct 4 ms 7736 KB Output is correct
9 Correct 4 ms 7380 KB Output is correct
10 Correct 5 ms 7812 KB Output is correct
11 Correct 5 ms 7780 KB Output is correct
12 Correct 5 ms 7764 KB Output is correct
13 Correct 6 ms 7764 KB Output is correct
14 Correct 5 ms 7748 KB Output is correct
15 Correct 5 ms 7764 KB Output is correct
16 Correct 5 ms 7764 KB Output is correct
17 Correct 6 ms 7764 KB Output is correct
18 Correct 5 ms 7768 KB Output is correct
19 Correct 5 ms 7764 KB Output is correct
20 Correct 5 ms 7768 KB Output is correct
21 Correct 5 ms 7764 KB Output is correct
22 Correct 5 ms 7892 KB Output is correct
23 Correct 5 ms 7764 KB Output is correct
24 Correct 5 ms 8020 KB Output is correct
25 Correct 5 ms 8016 KB Output is correct
26 Correct 6 ms 8020 KB Output is correct
27 Correct 5 ms 7764 KB Output is correct
28 Correct 6 ms 8108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
4 Correct 3 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 4 ms 7764 KB Output is correct
8 Correct 4 ms 7764 KB Output is correct
9 Correct 4 ms 7736 KB Output is correct
10 Correct 83 ms 40792 KB Output is correct
11 Correct 95 ms 44188 KB Output is correct
12 Correct 90 ms 43884 KB Output is correct
13 Correct 97 ms 44932 KB Output is correct
14 Correct 90 ms 48060 KB Output is correct
15 Correct 5 ms 7812 KB Output is correct
16 Correct 5 ms 7780 KB Output is correct
17 Correct 5 ms 7764 KB Output is correct
18 Correct 6 ms 7764 KB Output is correct
19 Correct 5 ms 7748 KB Output is correct
20 Correct 5 ms 7764 KB Output is correct
21 Correct 5 ms 7764 KB Output is correct
22 Correct 6 ms 7764 KB Output is correct
23 Correct 5 ms 7768 KB Output is correct
24 Correct 5 ms 7764 KB Output is correct
25 Correct 5 ms 7768 KB Output is correct
26 Correct 5 ms 7764 KB Output is correct
27 Correct 5 ms 7892 KB Output is correct
28 Correct 5 ms 7764 KB Output is correct
29 Correct 5 ms 8020 KB Output is correct
30 Correct 5 ms 8016 KB Output is correct
31 Correct 6 ms 8020 KB Output is correct
32 Correct 5 ms 7764 KB Output is correct
33 Correct 6 ms 8108 KB Output is correct
34 Correct 15 ms 13072 KB Output is correct
35 Incorrect 102 ms 46752 KB Output isn't correct
36 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 3 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 4 ms 7764 KB Output is correct
7 Correct 4 ms 7764 KB Output is correct
8 Correct 4 ms 7736 KB Output is correct
9 Correct 83 ms 40792 KB Output is correct
10 Correct 95 ms 44188 KB Output is correct
11 Correct 90 ms 43884 KB Output is correct
12 Correct 97 ms 44932 KB Output is correct
13 Correct 90 ms 48060 KB Output is correct
14 Correct 81 ms 40784 KB Output is correct
15 Correct 147 ms 45872 KB Output is correct
16 Correct 142 ms 45416 KB Output is correct
17 Correct 156 ms 46504 KB Output is correct
18 Correct 141 ms 49600 KB Output is correct
19 Correct 140 ms 51360 KB Output is correct
20 Correct 166 ms 52536 KB Output is correct
21 Correct 143 ms 51772 KB Output is correct
22 Correct 145 ms 52456 KB Output is correct
23 Correct 151 ms 52136 KB Output is correct
24 Correct 143 ms 51132 KB Output is correct
25 Correct 145 ms 51972 KB Output is correct
26 Correct 146 ms 51004 KB Output is correct
27 Correct 5 ms 7812 KB Output is correct
28 Correct 5 ms 7780 KB Output is correct
29 Correct 5 ms 7764 KB Output is correct
30 Correct 6 ms 7764 KB Output is correct
31 Correct 5 ms 7748 KB Output is correct
32 Correct 5 ms 7764 KB Output is correct
33 Correct 5 ms 7764 KB Output is correct
34 Correct 6 ms 7764 KB Output is correct
35 Correct 5 ms 7768 KB Output is correct
36 Correct 15 ms 13072 KB Output is correct
37 Incorrect 102 ms 46752 KB Output isn't correct
38 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
4 Correct 3 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 4 ms 7764 KB Output is correct
8 Correct 4 ms 7764 KB Output is correct
9 Correct 4 ms 7736 KB Output is correct
10 Correct 83 ms 40792 KB Output is correct
11 Correct 95 ms 44188 KB Output is correct
12 Correct 90 ms 43884 KB Output is correct
13 Correct 97 ms 44932 KB Output is correct
14 Correct 90 ms 48060 KB Output is correct
15 Correct 81 ms 40784 KB Output is correct
16 Correct 147 ms 45872 KB Output is correct
17 Correct 142 ms 45416 KB Output is correct
18 Correct 156 ms 46504 KB Output is correct
19 Correct 141 ms 49600 KB Output is correct
20 Correct 140 ms 51360 KB Output is correct
21 Correct 166 ms 52536 KB Output is correct
22 Correct 143 ms 51772 KB Output is correct
23 Correct 145 ms 52456 KB Output is correct
24 Correct 151 ms 52136 KB Output is correct
25 Correct 143 ms 51132 KB Output is correct
26 Correct 145 ms 51972 KB Output is correct
27 Correct 146 ms 51004 KB Output is correct
28 Correct 5 ms 7812 KB Output is correct
29 Correct 5 ms 7780 KB Output is correct
30 Correct 5 ms 7764 KB Output is correct
31 Correct 6 ms 7764 KB Output is correct
32 Correct 5 ms 7748 KB Output is correct
33 Correct 5 ms 7764 KB Output is correct
34 Correct 5 ms 7764 KB Output is correct
35 Correct 6 ms 7764 KB Output is correct
36 Correct 5 ms 7768 KB Output is correct
37 Correct 15 ms 13072 KB Output is correct
38 Incorrect 102 ms 46752 KB Output isn't correct
39 Halted 0 ms 0 KB -