Submission #878340

# Submission time Handle Problem Language Result Execution time Memory
878340 2023-11-24T08:28:22 Z boris_mihov Swapping Cities (APIO20_swap) C++17
100 / 100
333 ms 49056 KB
#include "swap.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <queue>
 
typedef long long llong;
const int MAXN = 100000 + 10;
const int MAXLOG = 17;
const int INF  = 2e9;

int n, m;
struct Sparse
{
    int par[MAXLOG][MAXN];
    int max[MAXLOG][MAXN];
    int dep[MAXN];

    void build(int p[], int e[], int d[])
    {
        for (int i = 1 ; i <= n ; ++i)
        {
            par[0][i] = p[i];
            max[0][i] = e[i];
            dep[i] = d[i];
        }

        for (int log = 1 ; (1 << log) <= n ; ++log)
        {
            for (int i = 1 ; i <= n ; ++i)
            {
                par[log][i] = par[log - 1][par[log - 1][i]];
                max[log][i] = std::max(max[log - 1][i], max[log - 1][par[log - 1][i]]);
            }
        }
    }

    void equalize(int &u, int v, int &res)
    {
        for (int log = MAXLOG - 1 ; log >= 0 ; --log)
        {
            if (dep[par[log][u]] >= dep[v])
            {
                res = std::max(res, max[log][u]);
                u = par[log][u];
            }
        }
    }

    void calcLCA(int &u, int v, int &res)
    {
        if (u == v)
        {
            return;
        }

        for (int log = MAXLOG - 1 ; log >= 0 ; --log)
        {
            if (par[log][u] != par[log][v])
            {
                res = std::max(res, max[log][u]);
                res = std::max(res, max[log][v]);
                u = par[log][u];
                v = par[log][v];
            }
        }

        res = std::max(res, max[0][v]);
        res = std::max(res, max[0][u]);
        u = par[0][u];
    }

    int findLCA(int u, int v)
    {
        int res = 0;
        if (dep[u] < dep[v])
        {
            std::swap(u, v);
        }

        equalize(u, v, res);
        calcLCA(u, v, res);
        return u;
    }

    int findMAX(int u, int v)
    {
        int res = 0;
        if (dep[u] < dep[v])
        {
            std::swap(u, v);
        }

        equalize(u, v, res);
        calcLCA(u, v, res);
        return res;
    }
};

struct DSU
{
    int par[MAXN];
    int dep[MAXN];

    void build()
    {
        std::iota(par + 1, par + 1 + n, 1);
        std::fill(dep + 1, dep + 1 + n, 1);
    }

    int find(int node)
    {
        if (par[node] == node) return node;
        return par[node] = find(par[node]);
    }

    void connect(int u, int v)
    {
        u = find(u);
        v = find(v);

        if (u == v)
        {
            return;
        }

        if (dep[u] > dep[v])
        {
            std::swap(u, v);
        }

        if (dep[u] == dep[v])
        {
            dep[v]++;
        }

        par[u] = v;
    }

    bool areConnected(int u, int v)
    {
        return find(u) == find(v);
    }
};

struct Edge
{
    int u, v;
    int dist;

    friend bool operator < (const Edge &a, const Edge &b)
    {
        return a.dist < b.dist;
    }
};

int p[MAXN];
int e[MAXN];
int dep[MAXN];
int minD[MAXN];
bool vis[MAXN];
std::priority_queue <std::pair <int,int>> relax;
std::vector <std::pair <int,int>> g[MAXN];
std::vector <std::pair <int,int>> t[MAXN];
std::vector <Edge> edges;
Sparse sparse;
DSU dsu;

void buildDFS(int node, int par)
{
    p[node] = par;
    dep[node] = dep[par] + 1;

    for (const auto &[u, d] : t[node])
    {
        if (u == par)
        {
            continue;
        }

        e[u] = d;
        buildDFS(u, node);
    }
}

void init(int N, int M, std::vector<int> U, std::vector<int> V, std::vector<int> W) 
{
    n = N; m = M;
    for (int i = 0 ; i < M ; ++i)
    {
        U[i]++; V[i]++;
        g[U[i]].emplace_back(V[i], W[i]);
        g[V[i]].emplace_back(U[i], W[i]);
        edges.push_back({U[i], V[i], W[i]});
    }

    std::fill(minD + 1, minD + 1 + n, INF);
    for (int i = 1 ; i <= n ; ++i)
    {
        std::sort(g[i].begin(), g[i].end(), [&](auto x, auto y)
        {
            return x.second < y.second;
        });

        if (g[i].size() >= 3)
        {
            minD[i] = g[i][2].second;
        }
    }

    dsu.build();
    std::sort(edges.begin(), edges.end());
    for (const auto &[u, v, d] : edges)
    {
        if (dsu.areConnected(u, v))
        {
            minD[u] = std::min(minD[u], d);
            minD[v] = std::min(minD[v], d);
        } else
        {
            t[u].emplace_back(v, d);
            t[v].emplace_back(u, d);
            dsu.connect(u, v);
        }
    }

    buildDFS(1, 0);
    sparse.build(p, e, dep);

    for (int i = 1 ; i <= n ; ++i)
    {
        relax.push({-minD[i], i});
    }
    
    while (!relax.empty())
    {
        auto [dist, node] = relax.top();
        relax.pop();

        if (vis[node])
        {
            continue;
        }

        vis[node] = true;
        for (const auto &[u, d] : t[node])
        {
            if (minD[u] > std::max(minD[node], d))
            {
                minD[u] = std::max(minD[node], d);
                relax.push({-minD[u], u});
            }
        }
    }
}
 
int getMinimumFuelCapacity(int x, int y) 
{
    x++; y++;
    int res = sparse.findMAX(x, y);
    res = std::max(res, minD[x]);
    res = std::max(res, minD[y]);
    if (res == INF) return -1;
    return res;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12636 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 3 ms 18780 KB Output is correct
5 Correct 4 ms 20968 KB Output is correct
6 Correct 5 ms 21084 KB Output is correct
7 Correct 5 ms 21084 KB Output is correct
8 Correct 4 ms 21080 KB Output is correct
9 Correct 65 ms 34192 KB Output is correct
10 Correct 78 ms 37852 KB Output is correct
11 Correct 79 ms 37384 KB Output is correct
12 Correct 81 ms 38512 KB Output is correct
13 Correct 79 ms 39612 KB Output is correct
14 Correct 80 ms 34292 KB Output is correct
15 Correct 230 ms 41732 KB Output is correct
16 Correct 224 ms 40272 KB Output is correct
17 Correct 198 ms 43272 KB Output is correct
18 Correct 207 ms 42504 KB Output is correct
19 Correct 91 ms 28944 KB Output is correct
20 Correct 218 ms 41860 KB Output is correct
21 Correct 224 ms 41156 KB Output is correct
22 Correct 266 ms 42716 KB Output is correct
23 Correct 241 ms 42992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12636 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 136 ms 40520 KB Output is correct
4 Correct 137 ms 41080 KB Output is correct
5 Correct 133 ms 41104 KB Output is correct
6 Correct 135 ms 40920 KB Output is correct
7 Correct 135 ms 41164 KB Output is correct
8 Correct 129 ms 40488 KB Output is correct
9 Correct 138 ms 40796 KB Output is correct
10 Correct 131 ms 40324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12636 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 3 ms 18780 KB Output is correct
5 Correct 4 ms 20968 KB Output is correct
6 Correct 5 ms 21084 KB Output is correct
7 Correct 5 ms 21084 KB Output is correct
8 Correct 4 ms 21080 KB Output is correct
9 Correct 2 ms 12636 KB Output is correct
10 Correct 4 ms 20924 KB Output is correct
11 Correct 4 ms 21084 KB Output is correct
12 Correct 4 ms 21076 KB Output is correct
13 Correct 4 ms 20828 KB Output is correct
14 Correct 4 ms 21084 KB Output is correct
15 Correct 5 ms 21084 KB Output is correct
16 Correct 4 ms 21084 KB Output is correct
17 Correct 4 ms 21084 KB Output is correct
18 Correct 4 ms 21084 KB Output is correct
19 Correct 4 ms 21084 KB Output is correct
20 Correct 4 ms 20948 KB Output is correct
21 Correct 4 ms 21080 KB Output is correct
22 Correct 3 ms 16816 KB Output is correct
23 Correct 5 ms 20944 KB Output is correct
24 Correct 5 ms 21172 KB Output is correct
25 Correct 4 ms 20988 KB Output is correct
26 Correct 5 ms 21080 KB Output is correct
27 Correct 4 ms 21084 KB Output is correct
28 Correct 4 ms 21084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12636 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 3 ms 18780 KB Output is correct
6 Correct 4 ms 20968 KB Output is correct
7 Correct 5 ms 21084 KB Output is correct
8 Correct 5 ms 21084 KB Output is correct
9 Correct 4 ms 21080 KB Output is correct
10 Correct 65 ms 34192 KB Output is correct
11 Correct 78 ms 37852 KB Output is correct
12 Correct 79 ms 37384 KB Output is correct
13 Correct 81 ms 38512 KB Output is correct
14 Correct 79 ms 39612 KB Output is correct
15 Correct 4 ms 20924 KB Output is correct
16 Correct 4 ms 21084 KB Output is correct
17 Correct 4 ms 21076 KB Output is correct
18 Correct 4 ms 20828 KB Output is correct
19 Correct 4 ms 21084 KB Output is correct
20 Correct 5 ms 21084 KB Output is correct
21 Correct 4 ms 21084 KB Output is correct
22 Correct 4 ms 21084 KB Output is correct
23 Correct 4 ms 21084 KB Output is correct
24 Correct 4 ms 21084 KB Output is correct
25 Correct 4 ms 20948 KB Output is correct
26 Correct 4 ms 21080 KB Output is correct
27 Correct 3 ms 16816 KB Output is correct
28 Correct 5 ms 20944 KB Output is correct
29 Correct 5 ms 21172 KB Output is correct
30 Correct 4 ms 20988 KB Output is correct
31 Correct 5 ms 21080 KB Output is correct
32 Correct 4 ms 21084 KB Output is correct
33 Correct 4 ms 21084 KB Output is correct
34 Correct 18 ms 22876 KB Output is correct
35 Correct 105 ms 37576 KB Output is correct
36 Correct 98 ms 36224 KB Output is correct
37 Correct 103 ms 35220 KB Output is correct
38 Correct 101 ms 34852 KB Output is correct
39 Correct 96 ms 34568 KB Output is correct
40 Correct 99 ms 32480 KB Output is correct
41 Correct 95 ms 37028 KB Output is correct
42 Correct 98 ms 37416 KB Output is correct
43 Correct 91 ms 38124 KB Output is correct
44 Correct 100 ms 35972 KB Output is correct
45 Correct 116 ms 38964 KB Output is correct
46 Correct 92 ms 35628 KB Output is correct
47 Correct 100 ms 35044 KB Output is correct
48 Correct 115 ms 35844 KB Output is correct
49 Correct 67 ms 34048 KB Output is correct
50 Correct 56 ms 30984 KB Output is correct
51 Correct 94 ms 35848 KB Output is correct
52 Correct 136 ms 41372 KB Output is correct
53 Correct 137 ms 42496 KB Output is correct
54 Correct 160 ms 45084 KB Output is correct
55 Correct 88 ms 38944 KB Output is correct
56 Correct 144 ms 41468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12636 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 3 ms 18780 KB Output is correct
5 Correct 4 ms 20968 KB Output is correct
6 Correct 5 ms 21084 KB Output is correct
7 Correct 5 ms 21084 KB Output is correct
8 Correct 4 ms 21080 KB Output is correct
9 Correct 65 ms 34192 KB Output is correct
10 Correct 78 ms 37852 KB Output is correct
11 Correct 79 ms 37384 KB Output is correct
12 Correct 81 ms 38512 KB Output is correct
13 Correct 79 ms 39612 KB Output is correct
14 Correct 80 ms 34292 KB Output is correct
15 Correct 230 ms 41732 KB Output is correct
16 Correct 224 ms 40272 KB Output is correct
17 Correct 198 ms 43272 KB Output is correct
18 Correct 207 ms 42504 KB Output is correct
19 Correct 136 ms 40520 KB Output is correct
20 Correct 137 ms 41080 KB Output is correct
21 Correct 133 ms 41104 KB Output is correct
22 Correct 135 ms 40920 KB Output is correct
23 Correct 135 ms 41164 KB Output is correct
24 Correct 129 ms 40488 KB Output is correct
25 Correct 138 ms 40796 KB Output is correct
26 Correct 131 ms 40324 KB Output is correct
27 Correct 4 ms 20924 KB Output is correct
28 Correct 4 ms 21084 KB Output is correct
29 Correct 4 ms 21076 KB Output is correct
30 Correct 4 ms 20828 KB Output is correct
31 Correct 4 ms 21084 KB Output is correct
32 Correct 5 ms 21084 KB Output is correct
33 Correct 4 ms 21084 KB Output is correct
34 Correct 4 ms 21084 KB Output is correct
35 Correct 4 ms 21084 KB Output is correct
36 Correct 18 ms 22876 KB Output is correct
37 Correct 105 ms 37576 KB Output is correct
38 Correct 98 ms 36224 KB Output is correct
39 Correct 103 ms 35220 KB Output is correct
40 Correct 101 ms 34852 KB Output is correct
41 Correct 96 ms 34568 KB Output is correct
42 Correct 99 ms 32480 KB Output is correct
43 Correct 95 ms 37028 KB Output is correct
44 Correct 98 ms 37416 KB Output is correct
45 Correct 91 ms 38124 KB Output is correct
46 Correct 100 ms 35972 KB Output is correct
47 Correct 21 ms 23380 KB Output is correct
48 Correct 232 ms 40812 KB Output is correct
49 Correct 236 ms 39932 KB Output is correct
50 Correct 217 ms 39276 KB Output is correct
51 Correct 232 ms 39044 KB Output is correct
52 Correct 204 ms 38128 KB Output is correct
53 Correct 164 ms 36096 KB Output is correct
54 Correct 239 ms 41372 KB Output is correct
55 Correct 229 ms 41092 KB Output is correct
56 Correct 210 ms 42600 KB Output is correct
57 Correct 222 ms 40556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12636 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 3 ms 18780 KB Output is correct
6 Correct 4 ms 20968 KB Output is correct
7 Correct 5 ms 21084 KB Output is correct
8 Correct 5 ms 21084 KB Output is correct
9 Correct 4 ms 21080 KB Output is correct
10 Correct 65 ms 34192 KB Output is correct
11 Correct 78 ms 37852 KB Output is correct
12 Correct 79 ms 37384 KB Output is correct
13 Correct 81 ms 38512 KB Output is correct
14 Correct 79 ms 39612 KB Output is correct
15 Correct 80 ms 34292 KB Output is correct
16 Correct 230 ms 41732 KB Output is correct
17 Correct 224 ms 40272 KB Output is correct
18 Correct 198 ms 43272 KB Output is correct
19 Correct 207 ms 42504 KB Output is correct
20 Correct 136 ms 40520 KB Output is correct
21 Correct 137 ms 41080 KB Output is correct
22 Correct 133 ms 41104 KB Output is correct
23 Correct 135 ms 40920 KB Output is correct
24 Correct 135 ms 41164 KB Output is correct
25 Correct 129 ms 40488 KB Output is correct
26 Correct 138 ms 40796 KB Output is correct
27 Correct 131 ms 40324 KB Output is correct
28 Correct 4 ms 20924 KB Output is correct
29 Correct 4 ms 21084 KB Output is correct
30 Correct 4 ms 21076 KB Output is correct
31 Correct 4 ms 20828 KB Output is correct
32 Correct 4 ms 21084 KB Output is correct
33 Correct 5 ms 21084 KB Output is correct
34 Correct 4 ms 21084 KB Output is correct
35 Correct 4 ms 21084 KB Output is correct
36 Correct 4 ms 21084 KB Output is correct
37 Correct 18 ms 22876 KB Output is correct
38 Correct 105 ms 37576 KB Output is correct
39 Correct 98 ms 36224 KB Output is correct
40 Correct 103 ms 35220 KB Output is correct
41 Correct 101 ms 34852 KB Output is correct
42 Correct 96 ms 34568 KB Output is correct
43 Correct 99 ms 32480 KB Output is correct
44 Correct 95 ms 37028 KB Output is correct
45 Correct 98 ms 37416 KB Output is correct
46 Correct 91 ms 38124 KB Output is correct
47 Correct 100 ms 35972 KB Output is correct
48 Correct 21 ms 23380 KB Output is correct
49 Correct 232 ms 40812 KB Output is correct
50 Correct 236 ms 39932 KB Output is correct
51 Correct 217 ms 39276 KB Output is correct
52 Correct 232 ms 39044 KB Output is correct
53 Correct 204 ms 38128 KB Output is correct
54 Correct 164 ms 36096 KB Output is correct
55 Correct 239 ms 41372 KB Output is correct
56 Correct 229 ms 41092 KB Output is correct
57 Correct 210 ms 42600 KB Output is correct
58 Correct 222 ms 40556 KB Output is correct
59 Correct 91 ms 28944 KB Output is correct
60 Correct 218 ms 41860 KB Output is correct
61 Correct 224 ms 41156 KB Output is correct
62 Correct 266 ms 42716 KB Output is correct
63 Correct 241 ms 42992 KB Output is correct
64 Correct 4 ms 21084 KB Output is correct
65 Correct 4 ms 20948 KB Output is correct
66 Correct 4 ms 21080 KB Output is correct
67 Correct 3 ms 16816 KB Output is correct
68 Correct 5 ms 20944 KB Output is correct
69 Correct 5 ms 21172 KB Output is correct
70 Correct 4 ms 20988 KB Output is correct
71 Correct 5 ms 21080 KB Output is correct
72 Correct 4 ms 21084 KB Output is correct
73 Correct 4 ms 21084 KB Output is correct
74 Correct 116 ms 38964 KB Output is correct
75 Correct 92 ms 35628 KB Output is correct
76 Correct 100 ms 35044 KB Output is correct
77 Correct 115 ms 35844 KB Output is correct
78 Correct 67 ms 34048 KB Output is correct
79 Correct 56 ms 30984 KB Output is correct
80 Correct 94 ms 35848 KB Output is correct
81 Correct 136 ms 41372 KB Output is correct
82 Correct 137 ms 42496 KB Output is correct
83 Correct 160 ms 45084 KB Output is correct
84 Correct 88 ms 38944 KB Output is correct
85 Correct 144 ms 41468 KB Output is correct
86 Correct 60 ms 28116 KB Output is correct
87 Correct 241 ms 39532 KB Output is correct
88 Correct 228 ms 39792 KB Output is correct
89 Correct 184 ms 38948 KB Output is correct
90 Correct 141 ms 37936 KB Output is correct
91 Correct 147 ms 38440 KB Output is correct
92 Correct 182 ms 39656 KB Output is correct
93 Correct 292 ms 45224 KB Output is correct
94 Correct 282 ms 46084 KB Output is correct
95 Correct 333 ms 49056 KB Output is correct
96 Correct 235 ms 41584 KB Output is correct
97 Correct 226 ms 43968 KB Output is correct