Submission #564398

# Submission time Handle Problem Language Result Execution time Memory
564398 2022-05-19T06:12:43 Z ngpin04 Swapping Cities (APIO20_swap) C++14
100 / 100
691 ms 63748 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define TASK ""
#define bit(x) (1LL << (x))
#define getbit(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end() 
using namespace std;
template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
  if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
  if (a < b) {a = b; return true;} return false;
}
mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count());

int rand(int l, int r) {
  return l + rd() % (r - l + 1);
}
const int N = 2e5 + 5; 
const int oo = 1e9;
const long long ooo = 1e18;
const int mod = 1e9 + 7; // 998244353;
const long double pi = acos(-1);

vector <int> adj[N];
vector <int> s[N];

int max_e[N][20];
int ok_e[N][20];
int anc[N][20];
int ok_v[N];
int par[N];
int deg[N];
int fr[N];
int to[N];
int w[N];
int h[N];
int n,m;

bool notmst[N];

int getpar(int u) {
    return (par[u] < 0) ? u : (par[u] = getpar(par[u]));
}

bool join(int u, int v) {
    u = getpar(u);
    v = getpar(v);
    if (u == v)
        return false;
    if (par[u] > par[v])
        swap(u, v);
    par[u] += par[v];
    par[v] = u;
    return true;
}

int getlca(int u, int v) {
    if (h[u] > h[v])
        swap(u, v);

    for (int i = 19; i >= 0; i--) 
        if (h[v] - bit(i) >= h[u])
            v = anc[v][i];
    if (u == v)
        return v;

    for (int i = 19; i >= 0; i--) if (anc[u][i] != anc[v][i]) {
        u = anc[u][i];
        v = anc[v][i];
    }
    return anc[v][0];
}

void dfs(int u, int p = -1) {
    anc[u][0] = p;

    for (int i : adj[u]) {
        int v = fr[i] ^ to[i] ^ u;
        if (v == p)
            continue;
        h[v] = h[u] + 1;
        max_e[v][0] = w[i];
        dfs(v, u);
    }
}

void build() {
    vector <int> ind(m, 0);
    iota(ALL(ind), 0);
    sort(ALL(ind), [](int i, int j) {
        return w[i] < w[j];
    });

    for (int i = 0; i < n; i++)
        par[i] = -1;

    for (int i : ind) {
        if (join(fr[i], to[i])) {
            adj[fr[i]].push_back(i);
            adj[to[i]].push_back(i);
        } else 
            notmst[i] = true;
    }

    dfs(0);

    for (int j = 1; j < 20; j++)
    for (int i = 0; i < n; i++) {
        int p = anc[i][j - 1];
        anc[i][j] = (p < 0) ? -1 : (anc[p][j - 1]);
    }

    for (int i = 0; i < n; i++) 
        ok_v[i] = ok_e[i][0] = 2 * oo;
    max_e[0][0] = 2 * oo;

    for (int i = 0; i < n; i++) {
        par[i] = -1;
        s[i].push_back(i);
    }

    for (int it : ind) {
        deg[fr[it]]++;
        deg[to[it]]++;
        int mx = max(deg[fr[it]], deg[to[it]]);
        int u = getpar(fr[it]);
        int v = getpar(to[it]);
        if (s[u].size() > s[v].size())
            swap(u, v);
        if (min(s[u].size(), s[v].size()) == 0)
            mx = 3;

        if (mx == 3) {
            for (int x : s[u])
                ok_v[x] = w[it];
            s[u].clear();

            for (int x : s[v])
                ok_v[x] = w[it];
            s[v].clear();
        }

        if (u != v) {
            for (int x : s[u])
                s[v].push_back(x);
            s[u].clear();
            par[v] += par[u];
            par[u] = v;
        }

    }

    for (int i = 0; i < n; i++)
        par[i] = -1;

    for (int it : ind) if (notmst[it]) {
        int u = fr[it];
        int v = to[it];
        int p = getlca(u, v);

        for (int i = getpar(u); h[i] > h[p]; i = getpar(i)) {
            ok_e[i][0] = w[it];
            par[i] = anc[i][0];
        }   

        for (int i = getpar(v); h[i] > h[p]; i = getpar(i)) {
            ok_e[i][0] = w[it];
            par[i] = anc[i][0];
        }
    }

    for (int j = 1; j < 20; j++) 
    for (int i = 0; i < n; i++) {
        int p = anc[i][j - 1];
        if (p < 0) {
            max_e[i][j] = ok_e[i][j] = 2 * oo;
        } else {            
            ok_e[i][j] = min(ok_e[i][j - 1], ok_e[p][j - 1]);
            max_e[i][j] = max(max_e[i][j - 1], max_e[p][j - 1]);
        }
    }
}

void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
    n = N;
    m = M;
    for (int i = 0; i < m; i++) {
        fr[i] = U[i];
        to[i] = V[i];
        w[i] = W[i];
    }
    build();
}

int getmax(int u, int p) {
    int res = 0;
    for (int i = 19; i >= 0; i--) if (h[u] - bit(i) >= h[p]) {
        maxi(res, max_e[u][i]);
        u = anc[u][i];
    }
    return res;
}

int minEdge(int u, int p) {
    int res = 2 * oo;
    for (int i = 19; i >= 0; i--) if (h[u] - bit(i) >= h[p]) {
        mini(res, ok_e[u][i]);
        u = anc[u][i];
    }
    return res;
}

int getMinimumFuelCapacity(int u, int v) {
    if (h[u] > h[v])
        swap(u, v);
    int p = getlca(u, v);
    int res = max(getmax(u, p), getmax(v, p));
    int ed = min(minEdge(u, p), minEdge(v, p));
    int vt = min(ok_v[u], ok_v[v]);
    // cerr << u << " " << v << " " << vt << " " << ed << "\n";
    maxi(res, min(vt, ed));
    if (res == 2 * oo)
        res = -1;
    return res;
}

#include "swap.h"
//#include "grader.cpp"
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9728 KB Output is correct
2 Correct 6 ms 9728 KB Output is correct
3 Correct 6 ms 9772 KB Output is correct
4 Correct 6 ms 9812 KB Output is correct
5 Correct 6 ms 10040 KB Output is correct
6 Correct 6 ms 10068 KB Output is correct
7 Correct 6 ms 10068 KB Output is correct
8 Correct 6 ms 10196 KB Output is correct
9 Correct 144 ms 44228 KB Output is correct
10 Correct 185 ms 52952 KB Output is correct
11 Correct 185 ms 51636 KB Output is correct
12 Correct 207 ms 54524 KB Output is correct
13 Correct 162 ms 53424 KB Output is correct
14 Correct 183 ms 43668 KB Output is correct
15 Correct 520 ms 54628 KB Output is correct
16 Correct 519 ms 51916 KB Output is correct
17 Correct 516 ms 58056 KB Output is correct
18 Correct 506 ms 53652 KB Output is correct
19 Correct 143 ms 20416 KB Output is correct
20 Correct 527 ms 54808 KB Output is correct
21 Correct 586 ms 52812 KB Output is correct
22 Correct 521 ms 56808 KB Output is correct
23 Correct 530 ms 54272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9728 KB Output is correct
2 Correct 6 ms 9728 KB Output is correct
3 Correct 330 ms 46004 KB Output is correct
4 Correct 303 ms 48380 KB Output is correct
5 Correct 318 ms 47384 KB Output is correct
6 Correct 316 ms 48200 KB Output is correct
7 Correct 342 ms 47908 KB Output is correct
8 Correct 312 ms 46496 KB Output is correct
9 Correct 304 ms 47564 KB Output is correct
10 Correct 322 ms 46156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9728 KB Output is correct
2 Correct 6 ms 9728 KB Output is correct
3 Correct 6 ms 9772 KB Output is correct
4 Correct 6 ms 9812 KB Output is correct
5 Correct 6 ms 10040 KB Output is correct
6 Correct 6 ms 10068 KB Output is correct
7 Correct 6 ms 10068 KB Output is correct
8 Correct 6 ms 10196 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 6 ms 10068 KB Output is correct
11 Correct 6 ms 10068 KB Output is correct
12 Correct 6 ms 10068 KB Output is correct
13 Correct 6 ms 9940 KB Output is correct
14 Correct 7 ms 10068 KB Output is correct
15 Correct 6 ms 10100 KB Output is correct
16 Correct 6 ms 10068 KB Output is correct
17 Correct 8 ms 10140 KB Output is correct
18 Correct 6 ms 10068 KB Output is correct
19 Correct 8 ms 9984 KB Output is correct
20 Correct 6 ms 10068 KB Output is correct
21 Correct 7 ms 10068 KB Output is correct
22 Correct 8 ms 9940 KB Output is correct
23 Correct 7 ms 9940 KB Output is correct
24 Correct 7 ms 10196 KB Output is correct
25 Correct 7 ms 10068 KB Output is correct
26 Correct 7 ms 10232 KB Output is correct
27 Correct 6 ms 10068 KB Output is correct
28 Correct 6 ms 10068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 6 ms 9728 KB Output is correct
3 Correct 6 ms 9728 KB Output is correct
4 Correct 6 ms 9772 KB Output is correct
5 Correct 6 ms 9812 KB Output is correct
6 Correct 6 ms 10040 KB Output is correct
7 Correct 6 ms 10068 KB Output is correct
8 Correct 6 ms 10068 KB Output is correct
9 Correct 6 ms 10196 KB Output is correct
10 Correct 144 ms 44228 KB Output is correct
11 Correct 185 ms 52952 KB Output is correct
12 Correct 185 ms 51636 KB Output is correct
13 Correct 207 ms 54524 KB Output is correct
14 Correct 162 ms 53424 KB Output is correct
15 Correct 6 ms 10068 KB Output is correct
16 Correct 6 ms 10068 KB Output is correct
17 Correct 6 ms 10068 KB Output is correct
18 Correct 6 ms 9940 KB Output is correct
19 Correct 7 ms 10068 KB Output is correct
20 Correct 6 ms 10100 KB Output is correct
21 Correct 6 ms 10068 KB Output is correct
22 Correct 8 ms 10140 KB Output is correct
23 Correct 6 ms 10068 KB Output is correct
24 Correct 8 ms 9984 KB Output is correct
25 Correct 6 ms 10068 KB Output is correct
26 Correct 7 ms 10068 KB Output is correct
27 Correct 8 ms 9940 KB Output is correct
28 Correct 7 ms 9940 KB Output is correct
29 Correct 7 ms 10196 KB Output is correct
30 Correct 7 ms 10068 KB Output is correct
31 Correct 7 ms 10232 KB Output is correct
32 Correct 6 ms 10068 KB Output is correct
33 Correct 6 ms 10068 KB Output is correct
34 Correct 19 ms 14836 KB Output is correct
35 Correct 190 ms 55124 KB Output is correct
36 Correct 213 ms 52076 KB Output is correct
37 Correct 180 ms 49388 KB Output is correct
38 Correct 198 ms 47948 KB Output is correct
39 Correct 207 ms 46776 KB Output is correct
40 Correct 138 ms 43340 KB Output is correct
41 Correct 193 ms 54068 KB Output is correct
42 Correct 239 ms 54916 KB Output is correct
43 Correct 154 ms 52736 KB Output is correct
44 Correct 156 ms 47356 KB Output is correct
45 Correct 221 ms 43568 KB Output is correct
46 Correct 203 ms 51092 KB Output is correct
47 Correct 171 ms 47896 KB Output is correct
48 Correct 220 ms 46924 KB Output is correct
49 Correct 111 ms 19224 KB Output is correct
50 Correct 79 ms 19276 KB Output is correct
51 Correct 164 ms 37576 KB Output is correct
52 Correct 304 ms 55532 KB Output is correct
53 Correct 228 ms 52064 KB Output is correct
54 Correct 318 ms 60040 KB Output is correct
55 Correct 216 ms 54220 KB Output is correct
56 Correct 237 ms 51764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9728 KB Output is correct
2 Correct 6 ms 9728 KB Output is correct
3 Correct 6 ms 9772 KB Output is correct
4 Correct 6 ms 9812 KB Output is correct
5 Correct 6 ms 10040 KB Output is correct
6 Correct 6 ms 10068 KB Output is correct
7 Correct 6 ms 10068 KB Output is correct
8 Correct 6 ms 10196 KB Output is correct
9 Correct 144 ms 44228 KB Output is correct
10 Correct 185 ms 52952 KB Output is correct
11 Correct 185 ms 51636 KB Output is correct
12 Correct 207 ms 54524 KB Output is correct
13 Correct 162 ms 53424 KB Output is correct
14 Correct 183 ms 43668 KB Output is correct
15 Correct 520 ms 54628 KB Output is correct
16 Correct 519 ms 51916 KB Output is correct
17 Correct 516 ms 58056 KB Output is correct
18 Correct 506 ms 53652 KB Output is correct
19 Correct 330 ms 46004 KB Output is correct
20 Correct 303 ms 48380 KB Output is correct
21 Correct 318 ms 47384 KB Output is correct
22 Correct 316 ms 48200 KB Output is correct
23 Correct 342 ms 47908 KB Output is correct
24 Correct 312 ms 46496 KB Output is correct
25 Correct 304 ms 47564 KB Output is correct
26 Correct 322 ms 46156 KB Output is correct
27 Correct 6 ms 10068 KB Output is correct
28 Correct 6 ms 10068 KB Output is correct
29 Correct 6 ms 10068 KB Output is correct
30 Correct 6 ms 9940 KB Output is correct
31 Correct 7 ms 10068 KB Output is correct
32 Correct 6 ms 10100 KB Output is correct
33 Correct 6 ms 10068 KB Output is correct
34 Correct 8 ms 10140 KB Output is correct
35 Correct 6 ms 10068 KB Output is correct
36 Correct 19 ms 14836 KB Output is correct
37 Correct 190 ms 55124 KB Output is correct
38 Correct 213 ms 52076 KB Output is correct
39 Correct 180 ms 49388 KB Output is correct
40 Correct 198 ms 47948 KB Output is correct
41 Correct 207 ms 46776 KB Output is correct
42 Correct 138 ms 43340 KB Output is correct
43 Correct 193 ms 54068 KB Output is correct
44 Correct 239 ms 54916 KB Output is correct
45 Correct 154 ms 52736 KB Output is correct
46 Correct 156 ms 47356 KB Output is correct
47 Correct 31 ms 14876 KB Output is correct
48 Correct 603 ms 57848 KB Output is correct
49 Correct 582 ms 55412 KB Output is correct
50 Correct 533 ms 53892 KB Output is correct
51 Correct 580 ms 52672 KB Output is correct
52 Correct 446 ms 49632 KB Output is correct
53 Correct 302 ms 40356 KB Output is correct
54 Correct 547 ms 58376 KB Output is correct
55 Correct 567 ms 59008 KB Output is correct
56 Correct 517 ms 57876 KB Output is correct
57 Correct 397 ms 52364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 6 ms 9728 KB Output is correct
3 Correct 6 ms 9728 KB Output is correct
4 Correct 6 ms 9772 KB Output is correct
5 Correct 6 ms 9812 KB Output is correct
6 Correct 6 ms 10040 KB Output is correct
7 Correct 6 ms 10068 KB Output is correct
8 Correct 6 ms 10068 KB Output is correct
9 Correct 6 ms 10196 KB Output is correct
10 Correct 144 ms 44228 KB Output is correct
11 Correct 185 ms 52952 KB Output is correct
12 Correct 185 ms 51636 KB Output is correct
13 Correct 207 ms 54524 KB Output is correct
14 Correct 162 ms 53424 KB Output is correct
15 Correct 183 ms 43668 KB Output is correct
16 Correct 520 ms 54628 KB Output is correct
17 Correct 519 ms 51916 KB Output is correct
18 Correct 516 ms 58056 KB Output is correct
19 Correct 506 ms 53652 KB Output is correct
20 Correct 330 ms 46004 KB Output is correct
21 Correct 303 ms 48380 KB Output is correct
22 Correct 318 ms 47384 KB Output is correct
23 Correct 316 ms 48200 KB Output is correct
24 Correct 342 ms 47908 KB Output is correct
25 Correct 312 ms 46496 KB Output is correct
26 Correct 304 ms 47564 KB Output is correct
27 Correct 322 ms 46156 KB Output is correct
28 Correct 6 ms 10068 KB Output is correct
29 Correct 6 ms 10068 KB Output is correct
30 Correct 6 ms 10068 KB Output is correct
31 Correct 6 ms 9940 KB Output is correct
32 Correct 7 ms 10068 KB Output is correct
33 Correct 6 ms 10100 KB Output is correct
34 Correct 6 ms 10068 KB Output is correct
35 Correct 8 ms 10140 KB Output is correct
36 Correct 6 ms 10068 KB Output is correct
37 Correct 19 ms 14836 KB Output is correct
38 Correct 190 ms 55124 KB Output is correct
39 Correct 213 ms 52076 KB Output is correct
40 Correct 180 ms 49388 KB Output is correct
41 Correct 198 ms 47948 KB Output is correct
42 Correct 207 ms 46776 KB Output is correct
43 Correct 138 ms 43340 KB Output is correct
44 Correct 193 ms 54068 KB Output is correct
45 Correct 239 ms 54916 KB Output is correct
46 Correct 154 ms 52736 KB Output is correct
47 Correct 156 ms 47356 KB Output is correct
48 Correct 31 ms 14876 KB Output is correct
49 Correct 603 ms 57848 KB Output is correct
50 Correct 582 ms 55412 KB Output is correct
51 Correct 533 ms 53892 KB Output is correct
52 Correct 580 ms 52672 KB Output is correct
53 Correct 446 ms 49632 KB Output is correct
54 Correct 302 ms 40356 KB Output is correct
55 Correct 547 ms 58376 KB Output is correct
56 Correct 567 ms 59008 KB Output is correct
57 Correct 517 ms 57876 KB Output is correct
58 Correct 397 ms 52364 KB Output is correct
59 Correct 143 ms 20416 KB Output is correct
60 Correct 527 ms 54808 KB Output is correct
61 Correct 586 ms 52812 KB Output is correct
62 Correct 521 ms 56808 KB Output is correct
63 Correct 530 ms 54272 KB Output is correct
64 Correct 8 ms 9984 KB Output is correct
65 Correct 6 ms 10068 KB Output is correct
66 Correct 7 ms 10068 KB Output is correct
67 Correct 8 ms 9940 KB Output is correct
68 Correct 7 ms 9940 KB Output is correct
69 Correct 7 ms 10196 KB Output is correct
70 Correct 7 ms 10068 KB Output is correct
71 Correct 7 ms 10232 KB Output is correct
72 Correct 6 ms 10068 KB Output is correct
73 Correct 6 ms 10068 KB Output is correct
74 Correct 221 ms 43568 KB Output is correct
75 Correct 203 ms 51092 KB Output is correct
76 Correct 171 ms 47896 KB Output is correct
77 Correct 220 ms 46924 KB Output is correct
78 Correct 111 ms 19224 KB Output is correct
79 Correct 79 ms 19276 KB Output is correct
80 Correct 164 ms 37576 KB Output is correct
81 Correct 304 ms 55532 KB Output is correct
82 Correct 228 ms 52064 KB Output is correct
83 Correct 318 ms 60040 KB Output is correct
84 Correct 216 ms 54220 KB Output is correct
85 Correct 237 ms 51764 KB Output is correct
86 Correct 111 ms 23480 KB Output is correct
87 Correct 543 ms 54980 KB Output is correct
88 Correct 526 ms 55120 KB Output is correct
89 Correct 416 ms 49052 KB Output is correct
90 Correct 223 ms 23244 KB Output is correct
91 Correct 246 ms 26024 KB Output is correct
92 Correct 337 ms 42056 KB Output is correct
93 Correct 616 ms 59444 KB Output is correct
94 Correct 502 ms 56104 KB Output is correct
95 Correct 691 ms 63748 KB Output is correct
96 Correct 511 ms 56012 KB Output is correct
97 Correct 437 ms 54144 KB Output is correct