Submission #965607

# Submission time Handle Problem Language Result Execution time Memory
965607 2024-04-19T02:51:11 Z steveonalex Swapping Cities (APIO20_swap) C++17
100 / 100
517 ms 51980 KB
#include <bits/stdc++.h>
#include "swap.h"
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
 
#define ALL(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
 
// mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng(1);
ll rngesus(ll l, ll r){return ((ull) rng()) % (r - l + 1) + l;}
 
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
 
ll LASTBIT(ll mask){return mask & (-mask);}
ll pop_cnt(ll mask){return __builtin_popcountll(mask);}
ll ctz(ll mask){return __builtin_ctzll(mask);}
ll clz(ll mask){return __builtin_clzll(mask);}
ll logOf(ll mask){return 63 - clz(mask);}
 
template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b){a = b; return true;}
        return false;
    }
template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b){a = b; return true;}
        return false;
    }
template <class T>
    void printArr(T& a, string separator = " ", string finish = "\n", ostream& out = cout){
        for(auto i: a) out << i << separator;
        out << finish;
    }
template <class T>
    void remove_dup(vector<T> &a){
        sort(ALL(a));
        a.resize(unique(ALL(a)) - a.begin());
    }

struct DSU{
    int n;
    vector<int> parent;

    DSU(int _n){
        n = _n;
        parent.resize(n);
        for(int i = 0; i<n; ++i) parent[i] = i;
    }

    int find_set(int u){return (u == parent[u]) ? u : (parent[u] = find_set(parent[u]));}
    bool join_set(int u, int v){
        u = find_set(u), v = find_set(v);
        if (u != v){
            parent[v] = u;
            return true;
        }
        return false;
    }
};

const int N = 1e5 + 69, M = N * 2, LOG_N = 17, INF = 1e9 + 69;
int n, m;
vector<pair<int, int>> graph[N];
int val[N];
int sz[N], h[N], parent[N][LOG_N], max_path[N][LOG_N], rizz[N][LOG_N];

int dp1[N], dp2[N];

int two_child(int u, int forbid){
    vector<int> S;
    for(pair<int, int> v: graph[u]) {
        if (v.first != forbid) {
            S.push_back(v.second);
        }
        if (S.size() == 2) break;
    }
    if (S.size() < 2) return INF;
    return S.back();
}

void dfs(int u, int p){
    h[u] = (u == p) ? 1 : (h[p] + 1);
    sz[u] = 1;
    for(int j = 1; MASK(j) < h[u]; ++j){
        int prev = parent[u][j-1];
        parent[u][j] = parent[prev][j-1];
        max_path[u][j] = max(max_path[u][j-1], max_path[prev][j-1]);
        rizz[u][j] = min(rizz[u][j-1], rizz[prev][j-1]);
    }

    int mi = INF, secondMi = INF;
    for(pair<int, int> v: graph[u]) if (v.first != p){
        if (mi > v.second){
            secondMi = mi;
            mi = v.second;
        }
        else minimize(secondMi, v.second);
    }

    dp1[u] = two_child(u, parent[u][0]);

    for(pair<int, int> v: graph[u]) if (v.first != p && v.first >= 0){
        parent[v.first][0] = u;
        max_path[v.first][0] = v.second;
        if (v.second == mi) rizz[v.first][0] = secondMi;
        else rizz[v.first][0] = mi;

        dfs(v.first, u);
        sz[u] += sz[v.first];
        minimize(dp1[u], max(dp1[v.first], v.second));
    }
}

void dfs2(int u, int p){
    if (h[u] > 1){
        minimize(dp2[u], two_child(parent[u][0], u));
    }

    int mi = INF, secondMi = INF;
    for(pair<int, int> v: graph[u]) if (v.first != p){
        int val = max(v.second, dp1[v.first]);
        if (mi > val){
            secondMi = mi;
            mi = val;
        }
        else minimize(secondMi, val);
    }

    for(pair<int, int> v: graph[u]) if (v.first != p && v.first >= 0){
        int val = max(v.second, dp1[v.first]);
        minimize(dp2[v.first], max(dp2[u], max_path[u][0]));
        if (val == mi) minimize(dp2[v.first], secondMi);
        else minimize(dp2[v.first], mi);

        dfs2(v.first, u);
    }
}

int LCA(int u, int v){
    if (h[u] < h[v]) swap(u, v);
    int diff = h[u] - h[v];
    for(int j = 0; j < LOG_N; ++j) if (GETBIT(diff, j)) 
        u = parent[u][j];
    if (u == v) return u;
    for(int j = LOG_N - 1; j>=0; --j) if (parent[u][j] != parent[v][j]){
        u = parent[u][j];
        v = parent[v][j];
    }
    return parent[u][0];
}

void init(int _n, int _m, vector<int> U, vector<int> V, vector<int> W) {
    n = _n, m = _m;
    vector<array<int, 3>> e;
    for(int i = 0; i<m; ++i) e.push_back({{U[i], V[i], W[i]}});
    sort(ALL(e), [](array<int, 3> x, array<int, 3> y){return x[2] < y[2];});

    DSU mst(n);
    vector<array<int, 3>> outlier;
    for(auto i: e){
        int u= i[0], v = i[1], w = i[2];
        if (mst.join_set(i[0], i[1])) {
            graph[u].push_back({v, w});
            graph[v].push_back({u, w});
        }
        else {
            outlier.push_back(i);
            graph[u].push_back({-1, w});
            graph[v].push_back({-1, w});
        }
    }

    for(int i = 0; i<n; ++i){
        sort(ALL(graph[i]), [](pair<int, int> x, pair<int, int> y){return x.second < y.second;});
    }

    for(int i = 0; i<n; ++i) dp1[i] = dp2[i] = INF;
    dfs(0, 0);
    dfs2(0, 0);

    for(int i = 0; i<n; ++i) val[i] = INF;
    DSU jump(n);
    for(auto i: outlier){
        int u = i[0], v = i[1], w = i[2];

        int lck = LCA(u, v);
        while(h[u] > h[lck]){
            minimize(val[u], w);
            jump.join_set(parent[u][0], u);
            u = jump.find_set(u);
        }

        while(h[v] > h[lck]){
            minimize(val[v], w);
            jump.join_set(parent[v][0], v);
            v = jump.find_set(v);
        }
    }
}

int get_weight(int u, int v, int lck = -1){
    if (lck == -1) lck = LCA(u, v);
    int ans= -1;
    int diff = h[u] - h[lck];
    for(int j = 0; j< LOG_N; ++j) if (GETBIT(diff, j)) {
        maximize(ans, max_path[u][j]);
        u = parent[u][j];
    }
    diff = h[v] - h[lck];
    for(int j = 0; j<LOG_N; ++j) if (GETBIT(diff, j)){
        maximize(ans, max_path[v][j]);
        v = parent[v][j];
    }
    return ans;
}


int get_rizz(int u, int d){
    int ans= INF;
    for(int j = 0; j< LOG_N; ++j) if (GETBIT(d, j)) {
        minimize(ans, rizz[u][j]);
        u = parent[u][j];
    }
    return ans;
}

int binary_lift(int u, int d){
    for(int j = 0; j < LOG_N; ++j) if (GETBIT(d, j)) 
        u = parent[u][j];
    return u;
}


int getMinimumFuelCapacity(int x, int y) {
    if (h[x] > h[y]) swap(x, y);
    int lck = LCA(x, y);
    int lim = get_weight(x, y, lck);

    int ans = INF;
    if (lck == x){
        int _x = binary_lift(y, h[y] - h[x] - 1);
        int t1 = dp2[_x], t2 = dp1[y];
        minimize(ans, min(t1, t2));
        minimize(ans, val[y]);
        minimize(ans, val[_x]);
        minimize(ans, get_rizz(y, h[y] - h[lck] - 1));
    }
    else{
        int t1 = dp1[x], t2 = dp1[y];
        minimize(ans, min(t1, t2));
        minimize(ans, val[x]); 
        minimize(ans, val[y]);
        minimize(ans, get_rizz(x, h[x] - h[lck] - 1));
        minimize(ans, get_rizz(y, h[y] - h[lck] - 1));
        int _x = binary_lift(x, h[x] - h[lck] - 1);
        int _y = binary_lift(y, h[y] - h[lck] - 1);
        for(pair<int, int> v: graph[lck]) {
            if (v.first == _x || v.first == _y) continue;
            minimize(ans, v.second);
            break;
        } 
    }

    if (ans == INF) return -1;
    maximize(ans, lim);
    return ans;
}


// int main(void){
//     ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

//     // freopen("input.inp", "r", stdin);

//     int n, m; cin >> n >> m;
//     vector<int> U, V, W;
//     for(int i = 0; i<m; ++i){
//         int u, v, w; cin >> u >> v >> w;
//         U.push_back(u); V.push_back(v); W.push_back(w);
//     }

//     init(n, m, U, V, W);
//     int q; cin >> q;
//     while(q--){
//         int x, y; cin >> x >> y;
//         cout << getMinimumFuelCapacity(x, y) << "\n";
//     }
 
//     return 0;
// }
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8028 KB Output is correct
2 Correct 2 ms 8136 KB Output is correct
3 Correct 2 ms 8028 KB Output is correct
4 Correct 3 ms 8028 KB Output is correct
5 Correct 3 ms 8144 KB Output is correct
6 Correct 3 ms 8280 KB Output is correct
7 Correct 2 ms 8284 KB Output is correct
8 Correct 2 ms 8284 KB Output is correct
9 Correct 95 ms 35232 KB Output is correct
10 Correct 155 ms 40628 KB Output is correct
11 Correct 126 ms 39620 KB Output is correct
12 Correct 143 ms 41268 KB Output is correct
13 Correct 127 ms 43460 KB Output is correct
14 Correct 135 ms 34752 KB Output is correct
15 Correct 411 ms 44216 KB Output is correct
16 Correct 368 ms 41672 KB Output is correct
17 Correct 353 ms 47340 KB Output is correct
18 Correct 358 ms 45508 KB Output is correct
19 Correct 112 ms 20604 KB Output is correct
20 Correct 423 ms 43208 KB Output is correct
21 Correct 370 ms 41784 KB Output is correct
22 Correct 366 ms 44488 KB Output is correct
23 Correct 378 ms 45456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8028 KB Output is correct
2 Correct 2 ms 8136 KB Output is correct
3 Correct 159 ms 37388 KB Output is correct
4 Correct 168 ms 38128 KB Output is correct
5 Correct 182 ms 37872 KB Output is correct
6 Correct 149 ms 38088 KB Output is correct
7 Correct 194 ms 38072 KB Output is correct
8 Correct 153 ms 37268 KB Output is correct
9 Correct 154 ms 37836 KB Output is correct
10 Correct 154 ms 37184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8028 KB Output is correct
2 Correct 2 ms 8136 KB Output is correct
3 Correct 2 ms 8028 KB Output is correct
4 Correct 3 ms 8028 KB Output is correct
5 Correct 3 ms 8144 KB Output is correct
6 Correct 3 ms 8280 KB Output is correct
7 Correct 2 ms 8284 KB Output is correct
8 Correct 2 ms 8284 KB Output is correct
9 Correct 2 ms 8028 KB Output is correct
10 Correct 2 ms 8284 KB Output is correct
11 Correct 2 ms 8284 KB Output is correct
12 Correct 2 ms 8280 KB Output is correct
13 Correct 3 ms 8284 KB Output is correct
14 Correct 2 ms 8284 KB Output is correct
15 Correct 2 ms 8284 KB Output is correct
16 Correct 2 ms 8284 KB Output is correct
17 Correct 2 ms 8284 KB Output is correct
18 Correct 4 ms 8152 KB Output is correct
19 Correct 2 ms 8160 KB Output is correct
20 Correct 2 ms 8148 KB Output is correct
21 Correct 2 ms 8284 KB Output is correct
22 Correct 3 ms 8284 KB Output is correct
23 Correct 2 ms 8284 KB Output is correct
24 Correct 3 ms 8280 KB Output is correct
25 Correct 3 ms 8284 KB Output is correct
26 Correct 3 ms 8284 KB Output is correct
27 Correct 2 ms 8284 KB Output is correct
28 Correct 3 ms 8284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8028 KB Output is correct
2 Correct 2 ms 8028 KB Output is correct
3 Correct 2 ms 8136 KB Output is correct
4 Correct 2 ms 8028 KB Output is correct
5 Correct 3 ms 8028 KB Output is correct
6 Correct 3 ms 8144 KB Output is correct
7 Correct 3 ms 8280 KB Output is correct
8 Correct 2 ms 8284 KB Output is correct
9 Correct 2 ms 8284 KB Output is correct
10 Correct 95 ms 35232 KB Output is correct
11 Correct 155 ms 40628 KB Output is correct
12 Correct 126 ms 39620 KB Output is correct
13 Correct 143 ms 41268 KB Output is correct
14 Correct 127 ms 43460 KB Output is correct
15 Correct 2 ms 8284 KB Output is correct
16 Correct 2 ms 8284 KB Output is correct
17 Correct 2 ms 8280 KB Output is correct
18 Correct 3 ms 8284 KB Output is correct
19 Correct 2 ms 8284 KB Output is correct
20 Correct 2 ms 8284 KB Output is correct
21 Correct 2 ms 8284 KB Output is correct
22 Correct 2 ms 8284 KB Output is correct
23 Correct 4 ms 8152 KB Output is correct
24 Correct 2 ms 8160 KB Output is correct
25 Correct 2 ms 8148 KB Output is correct
26 Correct 2 ms 8284 KB Output is correct
27 Correct 3 ms 8284 KB Output is correct
28 Correct 2 ms 8284 KB Output is correct
29 Correct 3 ms 8280 KB Output is correct
30 Correct 3 ms 8284 KB Output is correct
31 Correct 3 ms 8284 KB Output is correct
32 Correct 2 ms 8284 KB Output is correct
33 Correct 3 ms 8284 KB Output is correct
34 Correct 11 ms 11920 KB Output is correct
35 Correct 132 ms 40376 KB Output is correct
36 Correct 122 ms 37268 KB Output is correct
37 Correct 139 ms 34976 KB Output is correct
38 Correct 107 ms 34244 KB Output is correct
39 Correct 114 ms 33744 KB Output is correct
40 Correct 103 ms 32600 KB Output is correct
41 Correct 155 ms 38340 KB Output is correct
42 Correct 141 ms 39620 KB Output is correct
43 Correct 119 ms 41016 KB Output is correct
44 Correct 102 ms 34756 KB Output is correct
45 Correct 129 ms 38524 KB Output is correct
46 Correct 129 ms 36024 KB Output is correct
47 Correct 113 ms 33992 KB Output is correct
48 Correct 117 ms 34756 KB Output is correct
49 Correct 69 ms 23748 KB Output is correct
50 Correct 64 ms 22216 KB Output is correct
51 Correct 110 ms 33360 KB Output is correct
52 Correct 188 ms 43400 KB Output is correct
53 Correct 185 ms 42684 KB Output is correct
54 Correct 225 ms 48668 KB Output is correct
55 Correct 122 ms 42536 KB Output is correct
56 Correct 210 ms 41748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8028 KB Output is correct
2 Correct 2 ms 8136 KB Output is correct
3 Correct 2 ms 8028 KB Output is correct
4 Correct 3 ms 8028 KB Output is correct
5 Correct 3 ms 8144 KB Output is correct
6 Correct 3 ms 8280 KB Output is correct
7 Correct 2 ms 8284 KB Output is correct
8 Correct 2 ms 8284 KB Output is correct
9 Correct 95 ms 35232 KB Output is correct
10 Correct 155 ms 40628 KB Output is correct
11 Correct 126 ms 39620 KB Output is correct
12 Correct 143 ms 41268 KB Output is correct
13 Correct 127 ms 43460 KB Output is correct
14 Correct 135 ms 34752 KB Output is correct
15 Correct 411 ms 44216 KB Output is correct
16 Correct 368 ms 41672 KB Output is correct
17 Correct 353 ms 47340 KB Output is correct
18 Correct 358 ms 45508 KB Output is correct
19 Correct 159 ms 37388 KB Output is correct
20 Correct 168 ms 38128 KB Output is correct
21 Correct 182 ms 37872 KB Output is correct
22 Correct 149 ms 38088 KB Output is correct
23 Correct 194 ms 38072 KB Output is correct
24 Correct 153 ms 37268 KB Output is correct
25 Correct 154 ms 37836 KB Output is correct
26 Correct 154 ms 37184 KB Output is correct
27 Correct 2 ms 8284 KB Output is correct
28 Correct 2 ms 8284 KB Output is correct
29 Correct 2 ms 8280 KB Output is correct
30 Correct 3 ms 8284 KB Output is correct
31 Correct 2 ms 8284 KB Output is correct
32 Correct 2 ms 8284 KB Output is correct
33 Correct 2 ms 8284 KB Output is correct
34 Correct 2 ms 8284 KB Output is correct
35 Correct 4 ms 8152 KB Output is correct
36 Correct 11 ms 11920 KB Output is correct
37 Correct 132 ms 40376 KB Output is correct
38 Correct 122 ms 37268 KB Output is correct
39 Correct 139 ms 34976 KB Output is correct
40 Correct 107 ms 34244 KB Output is correct
41 Correct 114 ms 33744 KB Output is correct
42 Correct 103 ms 32600 KB Output is correct
43 Correct 155 ms 38340 KB Output is correct
44 Correct 141 ms 39620 KB Output is correct
45 Correct 119 ms 41016 KB Output is correct
46 Correct 102 ms 34756 KB Output is correct
47 Correct 22 ms 12172 KB Output is correct
48 Correct 370 ms 41956 KB Output is correct
49 Correct 412 ms 40136 KB Output is correct
50 Correct 415 ms 38872 KB Output is correct
51 Correct 357 ms 38272 KB Output is correct
52 Correct 336 ms 36824 KB Output is correct
53 Correct 277 ms 32404 KB Output is correct
54 Correct 384 ms 41412 KB Output is correct
55 Correct 398 ms 42716 KB Output is correct
56 Correct 396 ms 45760 KB Output is correct
57 Correct 305 ms 38660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8028 KB Output is correct
2 Correct 2 ms 8028 KB Output is correct
3 Correct 2 ms 8136 KB Output is correct
4 Correct 2 ms 8028 KB Output is correct
5 Correct 3 ms 8028 KB Output is correct
6 Correct 3 ms 8144 KB Output is correct
7 Correct 3 ms 8280 KB Output is correct
8 Correct 2 ms 8284 KB Output is correct
9 Correct 2 ms 8284 KB Output is correct
10 Correct 95 ms 35232 KB Output is correct
11 Correct 155 ms 40628 KB Output is correct
12 Correct 126 ms 39620 KB Output is correct
13 Correct 143 ms 41268 KB Output is correct
14 Correct 127 ms 43460 KB Output is correct
15 Correct 135 ms 34752 KB Output is correct
16 Correct 411 ms 44216 KB Output is correct
17 Correct 368 ms 41672 KB Output is correct
18 Correct 353 ms 47340 KB Output is correct
19 Correct 358 ms 45508 KB Output is correct
20 Correct 159 ms 37388 KB Output is correct
21 Correct 168 ms 38128 KB Output is correct
22 Correct 182 ms 37872 KB Output is correct
23 Correct 149 ms 38088 KB Output is correct
24 Correct 194 ms 38072 KB Output is correct
25 Correct 153 ms 37268 KB Output is correct
26 Correct 154 ms 37836 KB Output is correct
27 Correct 154 ms 37184 KB Output is correct
28 Correct 2 ms 8284 KB Output is correct
29 Correct 2 ms 8284 KB Output is correct
30 Correct 2 ms 8280 KB Output is correct
31 Correct 3 ms 8284 KB Output is correct
32 Correct 2 ms 8284 KB Output is correct
33 Correct 2 ms 8284 KB Output is correct
34 Correct 2 ms 8284 KB Output is correct
35 Correct 2 ms 8284 KB Output is correct
36 Correct 4 ms 8152 KB Output is correct
37 Correct 11 ms 11920 KB Output is correct
38 Correct 132 ms 40376 KB Output is correct
39 Correct 122 ms 37268 KB Output is correct
40 Correct 139 ms 34976 KB Output is correct
41 Correct 107 ms 34244 KB Output is correct
42 Correct 114 ms 33744 KB Output is correct
43 Correct 103 ms 32600 KB Output is correct
44 Correct 155 ms 38340 KB Output is correct
45 Correct 141 ms 39620 KB Output is correct
46 Correct 119 ms 41016 KB Output is correct
47 Correct 102 ms 34756 KB Output is correct
48 Correct 22 ms 12172 KB Output is correct
49 Correct 370 ms 41956 KB Output is correct
50 Correct 412 ms 40136 KB Output is correct
51 Correct 415 ms 38872 KB Output is correct
52 Correct 357 ms 38272 KB Output is correct
53 Correct 336 ms 36824 KB Output is correct
54 Correct 277 ms 32404 KB Output is correct
55 Correct 384 ms 41412 KB Output is correct
56 Correct 398 ms 42716 KB Output is correct
57 Correct 396 ms 45760 KB Output is correct
58 Correct 305 ms 38660 KB Output is correct
59 Correct 112 ms 20604 KB Output is correct
60 Correct 423 ms 43208 KB Output is correct
61 Correct 370 ms 41784 KB Output is correct
62 Correct 366 ms 44488 KB Output is correct
63 Correct 378 ms 45456 KB Output is correct
64 Correct 2 ms 8160 KB Output is correct
65 Correct 2 ms 8148 KB Output is correct
66 Correct 2 ms 8284 KB Output is correct
67 Correct 3 ms 8284 KB Output is correct
68 Correct 2 ms 8284 KB Output is correct
69 Correct 3 ms 8280 KB Output is correct
70 Correct 3 ms 8284 KB Output is correct
71 Correct 3 ms 8284 KB Output is correct
72 Correct 2 ms 8284 KB Output is correct
73 Correct 3 ms 8284 KB Output is correct
74 Correct 129 ms 38524 KB Output is correct
75 Correct 129 ms 36024 KB Output is correct
76 Correct 113 ms 33992 KB Output is correct
77 Correct 117 ms 34756 KB Output is correct
78 Correct 69 ms 23748 KB Output is correct
79 Correct 64 ms 22216 KB Output is correct
80 Correct 110 ms 33360 KB Output is correct
81 Correct 188 ms 43400 KB Output is correct
82 Correct 185 ms 42684 KB Output is correct
83 Correct 225 ms 48668 KB Output is correct
84 Correct 122 ms 42536 KB Output is correct
85 Correct 210 ms 41748 KB Output is correct
86 Correct 73 ms 20728 KB Output is correct
87 Correct 369 ms 39620 KB Output is correct
88 Correct 388 ms 39880 KB Output is correct
89 Correct 253 ms 37572 KB Output is correct
90 Correct 173 ms 28864 KB Output is correct
91 Correct 196 ms 29636 KB Output is correct
92 Correct 242 ms 36648 KB Output is correct
93 Correct 457 ms 47300 KB Output is correct
94 Correct 405 ms 46528 KB Output is correct
95 Correct 517 ms 51980 KB Output is correct
96 Correct 386 ms 43140 KB Output is correct
97 Correct 328 ms 43620 KB Output is correct