Submission #1077936

# Submission time Handle Problem Language Result Execution time Memory
1077936 2024-08-27T10:44:58 Z GrindMachine Swapping Cities (APIO20_swap) C++17
100 / 100
484 ms 66892 KB
#include <bits/stdc++.h>
using namespace std;
 
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
 
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define conts continue
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define ff first
#define ss second
 
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)
 
template<typename T>
void amin(T &x, T y){
    x = min(x,y);
}
 
template<typename T>
void amax(T &x, T y){
    x = max(x,y);
}
 
template<typename A,typename B>
string to_string(pair<A,B> p);
 
string to_string(const string &s){
    return "'"+s+"'";
}
 
string to_string(const char* s){
    return to_string((string)s);
}
 
string to_string(bool b){
    return b?"true":"false";
}
 
string to_string(vector<bool> v){
    string res = "{";
    rep(i,sz(v)){
        res += to_string(v[i])+",";
    }
    if(res.back() == ',') res.pop_back();
    res += "}";
    return res;
}
 
template<typename A>
string to_string(A v){
    string res = "{";
    trav(x,v){
        res += to_string(x)+",";
    }
    if(res.back() == ',') res.pop_back();
    res += "}";
    return res;
}
 
template<typename A,typename B>
string to_string(pair<A,B> p){
    return "("+to_string(p.ff)+","+to_string(p.ss)+")";
}
 
#define debug(x) cout << "[" << #x << "]: "; cout << to_string(x) << endl
 
const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = 1e9 + 5;
const ll inf2 = (ll)1e18 + 5;
 
#include "swap.h"
 
vector<int> par(N);
 
int find(int u){
    if(u == par[u]) return u;
    return par[u] = find(par[u]);
}
 
void merge(int u, int v){
    u = find(u), v = find(v);
    if(u == v) return;
    par[v] = u;
}
 
vector<pii> adj1[N], adj2[N];

const int LOG = 17;

int up[N][LOG], mxw[N][LOG];
vector<int> depth(N);

void dfs1(int u, int p){
    for(auto [v,w] : adj1[u]){
        if(v == p) conts;
        depth[v] = depth[u]+1;
        up[v][0] = u;
        mxw[v][0] = w;
        rep1(j,LOG-1){
            up[v][j] = up[up[v][j-1]][j-1];
            mxw[v][j] = max(mxw[v][j-1],mxw[up[v][j-1]][j-1]);
        }
        dfs1(v,u);
    }
}
 
int lift(int u, int k){
    rep(j,LOG){
        if(k&(1<<j)){
            u = up[u][j];
        }
    }
    return u;
}
 
int get_lca(int u, int v){
    if(depth[u] < depth[v]) swap(u,v);
    int k = depth[u]-depth[v];
    u = lift(u,k);
    if(u == v) return u;
 
    rev(j,LOG-1,0){
        if(up[u][j] != up[v][j]){
            u = up[u][j];
            v = up[v][j];
        }
    }
 
    u = up[u][0], v = up[v][0];
    assert(u == v);
    return u;
}

int get_mx(int u, int k){
    int mx = -inf1;
    rep(j,LOG){
        if(k&(1<<j)){
            amax(mx,mxw[u][j]);
            u = up[u][j];
        }
    }
    return mx;
}

vector<int> best(N,inf1);
vector<int> smallest_cyc(N,inf1);
vector<int> enter[N], leave[N];
multiset<int> ms[N];

void dfs2(int u, int p){
    for(auto [v,w] : adj1[u]){
        if(v == p) conts;
        dfs2(v,u);
        if(sz(ms[v]) > sz(ms[u])){
            swap(ms[u],ms[v]);
        }
        trav(x,ms[v]){
            ms[u].insert(x);
        }
        ms[v].clear();
    }

    trav(x,enter[u]){
        ms[u].insert(x);
    }

    if(!ms[u].empty()){
        amin(smallest_cyc[u],*ms[u].begin());
    }

    trav(x,leave[u]){
        rep1(iter,2){
            ms[u].erase(ms[u].find(x));
        }
    }
}

void init(int n, int m,
          std::vector<int> U, std::vector<int> V, std::vector<int> W) {
    
    vector<array<int,3>> edges;
    rep(i,m){
        edges.pb({W[i],U[i],V[i]});
    }
 
    sort(all(edges));
    vector<array<int,3>> span, non_span;
    iota(all(par),0);
 
    for(auto [w,u,v] : edges){
        if(find(u) != find(v)){
            span.pb({u,v,w});
            merge(u,v);
        }
        else{
            non_span.pb({u,v,w});
        }
    }
 
    for(auto [u,v,w] : span){
        adj1[u].pb({v,w}), adj1[v].pb({u,w});
    }
 
    for(auto [u,v,w] : non_span){
        adj2[u].pb({w,v}), adj2[v].pb({w,v});
    }
    
    dfs1(0,-1);
    
    rep(u,n){
        for(auto [v,w] : adj1[u]){
            adj2[u].pb({w,v});
        }
 
        sort(all(adj1[u]));
        sort(all(adj2[u]));
    }

    priority_queue<pii,vector<pii>,greater<pii>> pq;
    rep(i,n){
        if(sz(adj2[i]) >= 3){
            int val = 0;
            rep(j,3){
                amax(val,adj2[i][j].ff);
            }

            pq.push({val,i});
        }
    }

    vector<bool> vis(n);

    while(!pq.empty()){
        auto [cost,u] = pq.top();
        pq.pop();

        if(vis[u]) conts;
        vis[u] = 1;
        best[u] = cost;

        for(auto [w,v] : adj2[u]){
            pq.push({max(cost,w),v});
        }
    }

    for(auto [u,v,w] : non_span){
        int lca = get_lca(u,v);
        enter[u].pb(w), enter[v].pb(w);
        leave[lca].pb(w);
    }

    dfs2(0,-1);
}
 
int getMinimumFuelCapacity(int u, int v) {
    if(depth[u] > depth[v]) swap(u,v);
    int lca = get_lca(u,v);
 
    int du = depth[u]-depth[lca];
    int dv = depth[v]-depth[lca];
    int pmx = max(get_mx(u,du),get_mx(v,dv));
    
    int mn = min({best[u],best[v],smallest_cyc[u],smallest_cyc[v]});

    int ans = max(pmx,mn);
    if(ans == inf1) ans = -1;
 
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 19036 KB Output is correct
2 Correct 5 ms 19036 KB Output is correct
3 Correct 5 ms 19036 KB Output is correct
4 Correct 6 ms 19036 KB Output is correct
5 Correct 6 ms 19288 KB Output is correct
6 Correct 6 ms 19292 KB Output is correct
7 Correct 6 ms 19216 KB Output is correct
8 Correct 7 ms 19292 KB Output is correct
9 Correct 78 ms 42568 KB Output is correct
10 Correct 100 ms 48964 KB Output is correct
11 Correct 94 ms 47868 KB Output is correct
12 Correct 94 ms 49732 KB Output is correct
13 Correct 101 ms 52688 KB Output is correct
14 Correct 86 ms 41544 KB Output is correct
15 Correct 212 ms 50268 KB Output is correct
16 Correct 213 ms 47064 KB Output is correct
17 Correct 208 ms 54108 KB Output is correct
18 Correct 219 ms 51776 KB Output is correct
19 Correct 87 ms 27004 KB Output is correct
20 Correct 215 ms 49140 KB Output is correct
21 Correct 217 ms 46860 KB Output is correct
22 Correct 234 ms 50500 KB Output is correct
23 Correct 226 ms 51124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 19036 KB Output is correct
2 Correct 5 ms 19036 KB Output is correct
3 Correct 149 ms 44100 KB Output is correct
4 Correct 166 ms 45124 KB Output is correct
5 Correct 157 ms 44352 KB Output is correct
6 Correct 157 ms 45048 KB Output is correct
7 Correct 170 ms 44868 KB Output is correct
8 Correct 148 ms 43792 KB Output is correct
9 Correct 155 ms 44616 KB Output is correct
10 Correct 157 ms 43648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 19036 KB Output is correct
2 Correct 5 ms 19036 KB Output is correct
3 Correct 5 ms 19036 KB Output is correct
4 Correct 6 ms 19036 KB Output is correct
5 Correct 6 ms 19288 KB Output is correct
6 Correct 6 ms 19292 KB Output is correct
7 Correct 6 ms 19216 KB Output is correct
8 Correct 7 ms 19292 KB Output is correct
9 Correct 6 ms 19036 KB Output is correct
10 Correct 7 ms 19292 KB Output is correct
11 Correct 7 ms 19412 KB Output is correct
12 Correct 7 ms 19292 KB Output is correct
13 Correct 7 ms 19292 KB Output is correct
14 Correct 6 ms 19292 KB Output is correct
15 Correct 7 ms 19292 KB Output is correct
16 Correct 6 ms 19292 KB Output is correct
17 Correct 7 ms 19344 KB Output is correct
18 Correct 6 ms 19292 KB Output is correct
19 Correct 6 ms 19292 KB Output is correct
20 Correct 10 ms 19292 KB Output is correct
21 Correct 6 ms 19292 KB Output is correct
22 Correct 7 ms 19292 KB Output is correct
23 Correct 7 ms 19292 KB Output is correct
24 Correct 8 ms 19544 KB Output is correct
25 Correct 8 ms 19292 KB Output is correct
26 Correct 8 ms 19548 KB Output is correct
27 Correct 6 ms 19288 KB Output is correct
28 Correct 7 ms 19292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 19036 KB Output is correct
2 Correct 6 ms 19036 KB Output is correct
3 Correct 5 ms 19036 KB Output is correct
4 Correct 5 ms 19036 KB Output is correct
5 Correct 6 ms 19036 KB Output is correct
6 Correct 6 ms 19288 KB Output is correct
7 Correct 6 ms 19292 KB Output is correct
8 Correct 6 ms 19216 KB Output is correct
9 Correct 7 ms 19292 KB Output is correct
10 Correct 78 ms 42568 KB Output is correct
11 Correct 100 ms 48964 KB Output is correct
12 Correct 94 ms 47868 KB Output is correct
13 Correct 94 ms 49732 KB Output is correct
14 Correct 101 ms 52688 KB Output is correct
15 Correct 7 ms 19292 KB Output is correct
16 Correct 7 ms 19412 KB Output is correct
17 Correct 7 ms 19292 KB Output is correct
18 Correct 7 ms 19292 KB Output is correct
19 Correct 6 ms 19292 KB Output is correct
20 Correct 7 ms 19292 KB Output is correct
21 Correct 6 ms 19292 KB Output is correct
22 Correct 7 ms 19344 KB Output is correct
23 Correct 6 ms 19292 KB Output is correct
24 Correct 6 ms 19292 KB Output is correct
25 Correct 10 ms 19292 KB Output is correct
26 Correct 6 ms 19292 KB Output is correct
27 Correct 7 ms 19292 KB Output is correct
28 Correct 7 ms 19292 KB Output is correct
29 Correct 8 ms 19544 KB Output is correct
30 Correct 8 ms 19292 KB Output is correct
31 Correct 8 ms 19548 KB Output is correct
32 Correct 6 ms 19288 KB Output is correct
33 Correct 7 ms 19292 KB Output is correct
34 Correct 19 ms 21724 KB Output is correct
35 Correct 109 ms 48988 KB Output is correct
36 Correct 115 ms 44864 KB Output is correct
37 Correct 99 ms 41792 KB Output is correct
38 Correct 103 ms 40888 KB Output is correct
39 Correct 105 ms 40424 KB Output is correct
40 Correct 104 ms 39496 KB Output is correct
41 Correct 113 ms 46052 KB Output is correct
42 Correct 104 ms 47940 KB Output is correct
43 Correct 105 ms 49756 KB Output is correct
44 Correct 104 ms 42052 KB Output is correct
45 Correct 248 ms 50880 KB Output is correct
46 Correct 102 ms 43080 KB Output is correct
47 Correct 106 ms 40524 KB Output is correct
48 Correct 135 ms 43328 KB Output is correct
49 Correct 250 ms 46664 KB Output is correct
50 Correct 212 ms 41376 KB Output is correct
51 Correct 233 ms 46788 KB Output is correct
52 Correct 253 ms 56000 KB Output is correct
53 Correct 273 ms 54464 KB Output is correct
54 Correct 363 ms 66892 KB Output is correct
55 Correct 119 ms 51988 KB Output is correct
56 Correct 295 ms 53952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 19036 KB Output is correct
2 Correct 5 ms 19036 KB Output is correct
3 Correct 5 ms 19036 KB Output is correct
4 Correct 6 ms 19036 KB Output is correct
5 Correct 6 ms 19288 KB Output is correct
6 Correct 6 ms 19292 KB Output is correct
7 Correct 6 ms 19216 KB Output is correct
8 Correct 7 ms 19292 KB Output is correct
9 Correct 78 ms 42568 KB Output is correct
10 Correct 100 ms 48964 KB Output is correct
11 Correct 94 ms 47868 KB Output is correct
12 Correct 94 ms 49732 KB Output is correct
13 Correct 101 ms 52688 KB Output is correct
14 Correct 86 ms 41544 KB Output is correct
15 Correct 212 ms 50268 KB Output is correct
16 Correct 213 ms 47064 KB Output is correct
17 Correct 208 ms 54108 KB Output is correct
18 Correct 219 ms 51776 KB Output is correct
19 Correct 149 ms 44100 KB Output is correct
20 Correct 166 ms 45124 KB Output is correct
21 Correct 157 ms 44352 KB Output is correct
22 Correct 157 ms 45048 KB Output is correct
23 Correct 170 ms 44868 KB Output is correct
24 Correct 148 ms 43792 KB Output is correct
25 Correct 155 ms 44616 KB Output is correct
26 Correct 157 ms 43648 KB Output is correct
27 Correct 7 ms 19292 KB Output is correct
28 Correct 7 ms 19412 KB Output is correct
29 Correct 7 ms 19292 KB Output is correct
30 Correct 7 ms 19292 KB Output is correct
31 Correct 6 ms 19292 KB Output is correct
32 Correct 7 ms 19292 KB Output is correct
33 Correct 6 ms 19292 KB Output is correct
34 Correct 7 ms 19344 KB Output is correct
35 Correct 6 ms 19292 KB Output is correct
36 Correct 19 ms 21724 KB Output is correct
37 Correct 109 ms 48988 KB Output is correct
38 Correct 115 ms 44864 KB Output is correct
39 Correct 99 ms 41792 KB Output is correct
40 Correct 103 ms 40888 KB Output is correct
41 Correct 105 ms 40424 KB Output is correct
42 Correct 104 ms 39496 KB Output is correct
43 Correct 113 ms 46052 KB Output is correct
44 Correct 104 ms 47940 KB Output is correct
45 Correct 105 ms 49756 KB Output is correct
46 Correct 104 ms 42052 KB Output is correct
47 Correct 26 ms 21724 KB Output is correct
48 Correct 233 ms 47496 KB Output is correct
49 Correct 236 ms 45124 KB Output is correct
50 Correct 239 ms 43332 KB Output is correct
51 Correct 234 ms 42540 KB Output is correct
52 Correct 234 ms 41028 KB Output is correct
53 Correct 173 ms 36680 KB Output is correct
54 Correct 248 ms 46252 KB Output is correct
55 Correct 232 ms 48508 KB Output is correct
56 Correct 235 ms 52392 KB Output is correct
57 Correct 217 ms 43892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 19036 KB Output is correct
2 Correct 6 ms 19036 KB Output is correct
3 Correct 5 ms 19036 KB Output is correct
4 Correct 5 ms 19036 KB Output is correct
5 Correct 6 ms 19036 KB Output is correct
6 Correct 6 ms 19288 KB Output is correct
7 Correct 6 ms 19292 KB Output is correct
8 Correct 6 ms 19216 KB Output is correct
9 Correct 7 ms 19292 KB Output is correct
10 Correct 78 ms 42568 KB Output is correct
11 Correct 100 ms 48964 KB Output is correct
12 Correct 94 ms 47868 KB Output is correct
13 Correct 94 ms 49732 KB Output is correct
14 Correct 101 ms 52688 KB Output is correct
15 Correct 86 ms 41544 KB Output is correct
16 Correct 212 ms 50268 KB Output is correct
17 Correct 213 ms 47064 KB Output is correct
18 Correct 208 ms 54108 KB Output is correct
19 Correct 219 ms 51776 KB Output is correct
20 Correct 149 ms 44100 KB Output is correct
21 Correct 166 ms 45124 KB Output is correct
22 Correct 157 ms 44352 KB Output is correct
23 Correct 157 ms 45048 KB Output is correct
24 Correct 170 ms 44868 KB Output is correct
25 Correct 148 ms 43792 KB Output is correct
26 Correct 155 ms 44616 KB Output is correct
27 Correct 157 ms 43648 KB Output is correct
28 Correct 7 ms 19292 KB Output is correct
29 Correct 7 ms 19412 KB Output is correct
30 Correct 7 ms 19292 KB Output is correct
31 Correct 7 ms 19292 KB Output is correct
32 Correct 6 ms 19292 KB Output is correct
33 Correct 7 ms 19292 KB Output is correct
34 Correct 6 ms 19292 KB Output is correct
35 Correct 7 ms 19344 KB Output is correct
36 Correct 6 ms 19292 KB Output is correct
37 Correct 19 ms 21724 KB Output is correct
38 Correct 109 ms 48988 KB Output is correct
39 Correct 115 ms 44864 KB Output is correct
40 Correct 99 ms 41792 KB Output is correct
41 Correct 103 ms 40888 KB Output is correct
42 Correct 105 ms 40424 KB Output is correct
43 Correct 104 ms 39496 KB Output is correct
44 Correct 113 ms 46052 KB Output is correct
45 Correct 104 ms 47940 KB Output is correct
46 Correct 105 ms 49756 KB Output is correct
47 Correct 104 ms 42052 KB Output is correct
48 Correct 26 ms 21724 KB Output is correct
49 Correct 233 ms 47496 KB Output is correct
50 Correct 236 ms 45124 KB Output is correct
51 Correct 239 ms 43332 KB Output is correct
52 Correct 234 ms 42540 KB Output is correct
53 Correct 234 ms 41028 KB Output is correct
54 Correct 173 ms 36680 KB Output is correct
55 Correct 248 ms 46252 KB Output is correct
56 Correct 232 ms 48508 KB Output is correct
57 Correct 235 ms 52392 KB Output is correct
58 Correct 217 ms 43892 KB Output is correct
59 Correct 87 ms 27004 KB Output is correct
60 Correct 215 ms 49140 KB Output is correct
61 Correct 217 ms 46860 KB Output is correct
62 Correct 234 ms 50500 KB Output is correct
63 Correct 226 ms 51124 KB Output is correct
64 Correct 6 ms 19292 KB Output is correct
65 Correct 10 ms 19292 KB Output is correct
66 Correct 6 ms 19292 KB Output is correct
67 Correct 7 ms 19292 KB Output is correct
68 Correct 7 ms 19292 KB Output is correct
69 Correct 8 ms 19544 KB Output is correct
70 Correct 8 ms 19292 KB Output is correct
71 Correct 8 ms 19548 KB Output is correct
72 Correct 6 ms 19288 KB Output is correct
73 Correct 7 ms 19292 KB Output is correct
74 Correct 248 ms 50880 KB Output is correct
75 Correct 102 ms 43080 KB Output is correct
76 Correct 106 ms 40524 KB Output is correct
77 Correct 135 ms 43328 KB Output is correct
78 Correct 250 ms 46664 KB Output is correct
79 Correct 212 ms 41376 KB Output is correct
80 Correct 233 ms 46788 KB Output is correct
81 Correct 253 ms 56000 KB Output is correct
82 Correct 273 ms 54464 KB Output is correct
83 Correct 363 ms 66892 KB Output is correct
84 Correct 119 ms 51988 KB Output is correct
85 Correct 295 ms 53952 KB Output is correct
86 Correct 98 ms 30412 KB Output is correct
87 Correct 247 ms 44356 KB Output is correct
88 Correct 245 ms 44608 KB Output is correct
89 Correct 229 ms 43600 KB Output is correct
90 Correct 336 ms 46928 KB Output is correct
91 Correct 332 ms 46272 KB Output is correct
92 Correct 297 ms 47044 KB Output is correct
93 Correct 398 ms 57284 KB Output is correct
94 Correct 381 ms 56944 KB Output is correct
95 Correct 484 ms 66748 KB Output is correct
96 Correct 228 ms 49220 KB Output is correct
97 Correct 302 ms 50576 KB Output is correct