Submission #333433

# Submission time Handle Problem Language Result Execution time Memory
333433 2020-12-05T23:32:43 Z 534351 Swapping Cities (APIO20_swap) C++17
100 / 100
673 ms 50900 KB
#include "swap.h"
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>

using namespace std;
// using namespace __gnu_pbds;

// template<class T>
// using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

template<class T, class U>
void ckmin(T &a, U b)
{
    if (a > b) a = b;
}

template<class T, class U>
void ckmax(T &a, U b)
{
    if (a < b) a = b;
}

#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define SZ(x) ((int) (x).size())
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;

const int MAXN = 100013;
const int INF = 1e9 + 7;

int N, T;
int dsu[MAXN];
vpi edge[MAXN];
int t[MAXN];
pii corners[MAXN];
vi nodes[MAXN];
bitset<MAXN> ok;
int st[MAXN], ft[MAXN];
pii jump[20][MAXN];

bool insubt(int u, int v) //is v in the subtree of u?
{
    return (u == N || (st[u] <= st[v] && st[v] <= ft[u]));
}
void dfs(int u, int p)
{
    st[u] = T;
    ft[u] = T;
    T++;
    for (auto e : edge[u])
    {
        int d = e.fi, v = e.se;
        if (v == p) continue;
        jump[0][v] = {u, d};
        dfs(v, u);
        ft[u] = ft[v];
    }
}
int get(int u)
{
    return (u == dsu[u] ? u : dsu[u] = get(dsu[u]));
}
void init(int n, int m, vi U, vi V, vi W)
{
    N = n;
    fill(t, t + N, INF);
    vector<pair<int, pii> > edges(m);
    FOR(i, 0, m)
    {
        edges[i] = {W[i], {U[i], V[i]}};
    }
    sort(ALL(edges));
    FOR(i, 0, N)
    {
        dsu[i] = i;
        nodes[i].PB(i);
        corners[i] = {i, i};
    }
    for (auto e : edges)
    {
        int d = e.fi, u = e.se.fi, v = e.se.se;
        int cu = get(u), cv = get(v);
        if (cu == cv)
        {
            if (!ok[cu])
            {
                for (int w : nodes[cu]) t[w] = d;
                ok[cu] = true;
            }
            //they're in the same component.
        }
        else
        {
            edge[u].PB({d, v});
            edge[v].PB({d, u});
            // cerr << "EDGE " << u << " <-> " << v << ' ' << d << endl;
            if (SZ(nodes[cu]) < SZ(nodes[cv]))
            {
                swap(u, v);
                swap(cu, cv);
            }
            //they're in different components.
            bool bad = (!ok[cu] && !ok[cv]
            && (u == corners[cu].fi || u == corners[cu].se) && (v == corners[cv].fi || v == corners[cv].se));
            if (bad) //update corners
            {
                pii corn = {corners[cu].fi ^ corners[cu].se ^ u, corners[cv].fi ^ corners[cv].se ^ v};
                corners[cu] = corn;
            }
            else //mark anybody that needs to be marked.
            {
                if (!ok[cu])
                {
                    for (int w : nodes[cu]) t[w] = d;
                }
                if (!ok[cv])
                {
                    for (int w : nodes[cv]) t[w] = d;
                }
                ok[cu] = true;
            }
            nodes[cu].insert(nodes[cu].end(), ALL(nodes[cv]));
            nodes[cv].clear();
            dsu[cv] = cu;
        }
    }
    jump[0][N] = {N, 0};
    jump[0][0] = {N, 0};
    dfs(0, N);
    FOR(i, 1, 20)
    {
        FOR(j, 0, N + 1)
        {
            jump[i][j] = {jump[i - 1][jump[i - 1][j].fi].fi, max(jump[i - 1][j].se, jump[i - 1][jump[i - 1][j].fi].se)};
        }
    }
    //now you want to be able to find longest edge on path u to v
}
int ask(int u, int v)
{
    // cerr << "ASK " << u << ' ' << v << endl;
    int res = 0;
    FORD(i, 20, 0)
    {
        if (insubt(jump[i][u].fi, v)) continue;
        ckmax(res, jump[i][u].se);
        u = jump[i][u].fi;
    }
    if (!insubt(u, v))
    {
        ckmax(res, jump[0][u].se);
    }
    FORD(i, 20, 0)
    {
        if (insubt(jump[i][v].fi, u)) continue;
        ckmax(res, jump[i][v].se);
        v = jump[i][v].fi;
    }
    if (!insubt(v, u))
    {
        ckmax(res, jump[0][v].se);
    }
    return res;
}

int getMinimumFuelCapacity(int u, int v)
{
    // cerr << "ASK " << ask(u, v) << endl;
    int res = max(ask(u, v), max(t[u], t[v]));
    if (res == INF) res = -1;
    return res;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5120 KB Output is correct
3 Correct 4 ms 5112 KB Output is correct
4 Correct 4 ms 5248 KB Output is correct
5 Correct 4 ms 5484 KB Output is correct
6 Correct 5 ms 5632 KB Output is correct
7 Correct 4 ms 5484 KB Output is correct
8 Correct 4 ms 5504 KB Output is correct
9 Correct 122 ms 34924 KB Output is correct
10 Correct 157 ms 42412 KB Output is correct
11 Correct 159 ms 41432 KB Output is correct
12 Correct 168 ms 43992 KB Output is correct
13 Correct 118 ms 42912 KB Output is correct
14 Correct 161 ms 34556 KB Output is correct
15 Correct 622 ms 44304 KB Output is correct
16 Correct 599 ms 41544 KB Output is correct
17 Correct 673 ms 47060 KB Output is correct
18 Correct 613 ms 43212 KB Output is correct
19 Correct 156 ms 16556 KB Output is correct
20 Correct 596 ms 46140 KB Output is correct
21 Correct 598 ms 44428 KB Output is correct
22 Correct 638 ms 48016 KB Output is correct
23 Correct 585 ms 45680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5120 KB Output is correct
3 Correct 317 ms 34468 KB Output is correct
4 Correct 319 ms 35628 KB Output is correct
5 Correct 323 ms 35228 KB Output is correct
6 Correct 310 ms 35648 KB Output is correct
7 Correct 315 ms 35420 KB Output is correct
8 Correct 310 ms 34236 KB Output is correct
9 Correct 310 ms 35168 KB Output is correct
10 Correct 309 ms 34004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5228 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5120 KB Output is correct
4 Correct 4 ms 5112 KB Output is correct
5 Correct 4 ms 5248 KB Output is correct
6 Correct 4 ms 5484 KB Output is correct
7 Correct 5 ms 5632 KB Output is correct
8 Correct 4 ms 5484 KB Output is correct
9 Correct 4 ms 5504 KB Output is correct
10 Correct 4 ms 5484 KB Output is correct
11 Correct 4 ms 5484 KB Output is correct
12 Correct 5 ms 5484 KB Output is correct
13 Correct 4 ms 5356 KB Output is correct
14 Correct 4 ms 5484 KB Output is correct
15 Correct 6 ms 5484 KB Output is correct
16 Correct 5 ms 5484 KB Output is correct
17 Correct 5 ms 5484 KB Output is correct
18 Correct 4 ms 5484 KB Output is correct
19 Correct 5 ms 5356 KB Output is correct
20 Correct 5 ms 5484 KB Output is correct
21 Correct 5 ms 5484 KB Output is correct
22 Correct 4 ms 5356 KB Output is correct
23 Correct 4 ms 5356 KB Output is correct
24 Correct 5 ms 5484 KB Output is correct
25 Correct 5 ms 5484 KB Output is correct
26 Correct 5 ms 5612 KB Output is correct
27 Correct 4 ms 5484 KB Output is correct
28 Correct 5 ms 5484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5228 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5120 KB Output is correct
4 Correct 4 ms 5112 KB Output is correct
5 Correct 4 ms 5248 KB Output is correct
6 Correct 4 ms 5484 KB Output is correct
7 Correct 5 ms 5632 KB Output is correct
8 Correct 4 ms 5484 KB Output is correct
9 Correct 4 ms 5504 KB Output is correct
10 Correct 122 ms 34924 KB Output is correct
11 Correct 157 ms 42412 KB Output is correct
12 Correct 159 ms 41432 KB Output is correct
13 Correct 168 ms 43992 KB Output is correct
14 Correct 118 ms 42912 KB Output is correct
15 Correct 4 ms 5484 KB Output is correct
16 Correct 4 ms 5484 KB Output is correct
17 Correct 5 ms 5484 KB Output is correct
18 Correct 4 ms 5356 KB Output is correct
19 Correct 4 ms 5484 KB Output is correct
20 Correct 6 ms 5484 KB Output is correct
21 Correct 5 ms 5484 KB Output is correct
22 Correct 5 ms 5484 KB Output is correct
23 Correct 4 ms 5484 KB Output is correct
24 Correct 16 ms 9600 KB Output is correct
25 Correct 159 ms 43372 KB Output is correct
26 Correct 165 ms 40712 KB Output is correct
27 Correct 166 ms 38256 KB Output is correct
28 Correct 153 ms 37240 KB Output is correct
29 Correct 154 ms 36756 KB Output is correct
30 Correct 140 ms 33828 KB Output is correct
31 Correct 163 ms 41876 KB Output is correct
32 Correct 161 ms 42668 KB Output is correct
33 Correct 120 ms 41352 KB Output is correct
34 Correct 158 ms 37524 KB Output is correct
35 Correct 5 ms 5356 KB Output is correct
36 Correct 5 ms 5484 KB Output is correct
37 Correct 5 ms 5484 KB Output is correct
38 Correct 4 ms 5356 KB Output is correct
39 Correct 4 ms 5356 KB Output is correct
40 Correct 5 ms 5484 KB Output is correct
41 Correct 5 ms 5484 KB Output is correct
42 Correct 5 ms 5612 KB Output is correct
43 Correct 4 ms 5484 KB Output is correct
44 Correct 5 ms 5484 KB Output is correct
45 Correct 144 ms 33536 KB Output is correct
46 Correct 161 ms 39448 KB Output is correct
47 Correct 156 ms 36388 KB Output is correct
48 Correct 152 ms 35824 KB Output is correct
49 Correct 75 ms 13804 KB Output is correct
50 Correct 60 ms 13676 KB Output is correct
51 Correct 115 ms 28596 KB Output is correct
52 Correct 193 ms 42704 KB Output is correct
53 Correct 197 ms 41384 KB Output is correct
54 Correct 210 ms 47100 KB Output is correct
55 Correct 123 ms 42488 KB Output is correct
56 Correct 184 ms 40404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5120 KB Output is correct
3 Correct 4 ms 5112 KB Output is correct
4 Correct 4 ms 5248 KB Output is correct
5 Correct 4 ms 5484 KB Output is correct
6 Correct 5 ms 5632 KB Output is correct
7 Correct 4 ms 5484 KB Output is correct
8 Correct 4 ms 5504 KB Output is correct
9 Correct 122 ms 34924 KB Output is correct
10 Correct 157 ms 42412 KB Output is correct
11 Correct 159 ms 41432 KB Output is correct
12 Correct 168 ms 43992 KB Output is correct
13 Correct 118 ms 42912 KB Output is correct
14 Correct 161 ms 34556 KB Output is correct
15 Correct 622 ms 44304 KB Output is correct
16 Correct 599 ms 41544 KB Output is correct
17 Correct 673 ms 47060 KB Output is correct
18 Correct 613 ms 43212 KB Output is correct
19 Correct 317 ms 34468 KB Output is correct
20 Correct 319 ms 35628 KB Output is correct
21 Correct 323 ms 35228 KB Output is correct
22 Correct 310 ms 35648 KB Output is correct
23 Correct 315 ms 35420 KB Output is correct
24 Correct 310 ms 34236 KB Output is correct
25 Correct 310 ms 35168 KB Output is correct
26 Correct 309 ms 34004 KB Output is correct
27 Correct 4 ms 5484 KB Output is correct
28 Correct 4 ms 5484 KB Output is correct
29 Correct 5 ms 5484 KB Output is correct
30 Correct 4 ms 5356 KB Output is correct
31 Correct 4 ms 5484 KB Output is correct
32 Correct 6 ms 5484 KB Output is correct
33 Correct 5 ms 5484 KB Output is correct
34 Correct 5 ms 5484 KB Output is correct
35 Correct 4 ms 5484 KB Output is correct
36 Correct 16 ms 9600 KB Output is correct
37 Correct 159 ms 43372 KB Output is correct
38 Correct 165 ms 40712 KB Output is correct
39 Correct 166 ms 38256 KB Output is correct
40 Correct 153 ms 37240 KB Output is correct
41 Correct 154 ms 36756 KB Output is correct
42 Correct 140 ms 33828 KB Output is correct
43 Correct 163 ms 41876 KB Output is correct
44 Correct 161 ms 42668 KB Output is correct
45 Correct 120 ms 41352 KB Output is correct
46 Correct 158 ms 37524 KB Output is correct
47 Correct 30 ms 9580 KB Output is correct
48 Correct 605 ms 45308 KB Output is correct
49 Correct 588 ms 43288 KB Output is correct
50 Correct 566 ms 42436 KB Output is correct
51 Correct 563 ms 41588 KB Output is correct
52 Correct 469 ms 39028 KB Output is correct
53 Correct 353 ms 31652 KB Output is correct
54 Correct 592 ms 45232 KB Output is correct
55 Correct 614 ms 46196 KB Output is correct
56 Correct 591 ms 45716 KB Output is correct
57 Correct 460 ms 41824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5228 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5120 KB Output is correct
4 Correct 4 ms 5112 KB Output is correct
5 Correct 4 ms 5248 KB Output is correct
6 Correct 4 ms 5484 KB Output is correct
7 Correct 5 ms 5632 KB Output is correct
8 Correct 4 ms 5484 KB Output is correct
9 Correct 4 ms 5504 KB Output is correct
10 Correct 122 ms 34924 KB Output is correct
11 Correct 157 ms 42412 KB Output is correct
12 Correct 159 ms 41432 KB Output is correct
13 Correct 168 ms 43992 KB Output is correct
14 Correct 118 ms 42912 KB Output is correct
15 Correct 161 ms 34556 KB Output is correct
16 Correct 622 ms 44304 KB Output is correct
17 Correct 599 ms 41544 KB Output is correct
18 Correct 673 ms 47060 KB Output is correct
19 Correct 613 ms 43212 KB Output is correct
20 Correct 317 ms 34468 KB Output is correct
21 Correct 319 ms 35628 KB Output is correct
22 Correct 323 ms 35228 KB Output is correct
23 Correct 310 ms 35648 KB Output is correct
24 Correct 315 ms 35420 KB Output is correct
25 Correct 310 ms 34236 KB Output is correct
26 Correct 310 ms 35168 KB Output is correct
27 Correct 309 ms 34004 KB Output is correct
28 Correct 4 ms 5484 KB Output is correct
29 Correct 4 ms 5484 KB Output is correct
30 Correct 5 ms 5484 KB Output is correct
31 Correct 4 ms 5356 KB Output is correct
32 Correct 4 ms 5484 KB Output is correct
33 Correct 6 ms 5484 KB Output is correct
34 Correct 5 ms 5484 KB Output is correct
35 Correct 5 ms 5484 KB Output is correct
36 Correct 4 ms 5484 KB Output is correct
37 Correct 16 ms 9600 KB Output is correct
38 Correct 159 ms 43372 KB Output is correct
39 Correct 165 ms 40712 KB Output is correct
40 Correct 166 ms 38256 KB Output is correct
41 Correct 153 ms 37240 KB Output is correct
42 Correct 154 ms 36756 KB Output is correct
43 Correct 140 ms 33828 KB Output is correct
44 Correct 163 ms 41876 KB Output is correct
45 Correct 161 ms 42668 KB Output is correct
46 Correct 120 ms 41352 KB Output is correct
47 Correct 158 ms 37524 KB Output is correct
48 Correct 30 ms 9580 KB Output is correct
49 Correct 605 ms 45308 KB Output is correct
50 Correct 588 ms 43288 KB Output is correct
51 Correct 566 ms 42436 KB Output is correct
52 Correct 563 ms 41588 KB Output is correct
53 Correct 469 ms 39028 KB Output is correct
54 Correct 353 ms 31652 KB Output is correct
55 Correct 592 ms 45232 KB Output is correct
56 Correct 614 ms 46196 KB Output is correct
57 Correct 591 ms 45716 KB Output is correct
58 Correct 460 ms 41824 KB Output is correct
59 Correct 156 ms 16556 KB Output is correct
60 Correct 596 ms 46140 KB Output is correct
61 Correct 598 ms 44428 KB Output is correct
62 Correct 638 ms 48016 KB Output is correct
63 Correct 585 ms 45680 KB Output is correct
64 Correct 5 ms 5356 KB Output is correct
65 Correct 5 ms 5484 KB Output is correct
66 Correct 5 ms 5484 KB Output is correct
67 Correct 4 ms 5356 KB Output is correct
68 Correct 4 ms 5356 KB Output is correct
69 Correct 5 ms 5484 KB Output is correct
70 Correct 5 ms 5484 KB Output is correct
71 Correct 5 ms 5612 KB Output is correct
72 Correct 4 ms 5484 KB Output is correct
73 Correct 5 ms 5484 KB Output is correct
74 Correct 144 ms 33536 KB Output is correct
75 Correct 161 ms 39448 KB Output is correct
76 Correct 156 ms 36388 KB Output is correct
77 Correct 152 ms 35824 KB Output is correct
78 Correct 75 ms 13804 KB Output is correct
79 Correct 60 ms 13676 KB Output is correct
80 Correct 115 ms 28596 KB Output is correct
81 Correct 193 ms 42704 KB Output is correct
82 Correct 197 ms 41384 KB Output is correct
83 Correct 210 ms 47100 KB Output is correct
84 Correct 123 ms 42488 KB Output is correct
85 Correct 184 ms 40404 KB Output is correct
86 Correct 94 ms 16828 KB Output is correct
87 Correct 567 ms 42692 KB Output is correct
88 Correct 584 ms 43220 KB Output is correct
89 Correct 404 ms 37816 KB Output is correct
90 Correct 193 ms 17772 KB Output is correct
91 Correct 211 ms 19820 KB Output is correct
92 Correct 359 ms 32232 KB Output is correct
93 Correct 627 ms 46616 KB Output is correct
94 Correct 482 ms 45400 KB Output is correct
95 Correct 643 ms 50900 KB Output is correct
96 Correct 560 ms 43660 KB Output is correct
97 Correct 464 ms 42184 KB Output is correct