Submission #576537

# Submission time Handle Problem Language Result Execution time Memory
576537 2022-06-13T07:21:54 Z radal Swapping Cities (APIO20_swap) C++17
13 / 100
602 ms 69916 KB
#include <bits/stdc++.h>
#include "swap.h"
#pragma GCC target("sse,sse2,sse4,avx2")
#pragma GCC optimize("unroll-loops,O2")
#define rep(i,l,r) for (int i = l; i < r; i++)
#define repr(i,r,l) for (int i = r; i >= l; i--)
#define X first
#define Y second
#define all(x) (x).begin() , (x).end()
#define pb push_back
#define endl '\n'
#define debug(x) cerr << #x << " : " << x << endl;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pll;
constexpr int N = 1e5+20,mod = 998244353,inf = 1e9+10;
inline int mkay(int a,int b){
    if (a+b >= mod) return a+b-mod;
//    if (a+b < 0) return a+b+mod;
    return a+b;
}
 
inline int poww(int a,int k){
    if (k < 0) return 0;
    int z = 1;
    while (k){
        if (k&1) z = 1ll*z*a%mod;
        a = 1ll*a*a%mod;
        k >>= 1;
    } 
    return z; 
}
vector<pll> adj[N];
vector<int> be[N],fr[N];
vector<pair<int,pll> > e;
int n,m;
int pr[N],par[N][20],h[N],mx[N][20],mi[N][20][2],bmi[N];
bool good[N];
multiset<int> st[N];
void dfs2(int v,int p){
    for (pll u : adj[v]){
        if (u.X == p) continue;
        dfs2(u.X,v);
        if(st[u.X].size() > st[v].size()) swap(st[v],st[u.X]);
        while (!st[u.X].empty()){
            int x = *(st[u.X].begin());
            st[v].insert(x);
            st[u.X].erase(st[u.X].begin());
        }
    }
    for (int x : be[v]) st[v].insert(x);
    for (int x : fr[v]) st[v].erase(st[v].find(x));
    if (st[v].empty()) return;
    int g = *(st[v].begin());
    bmi[v] = g;
    if (g < mi[v][0][0]){
        mi[v][0][1] = mi[v][0][0];
        mi[v][0][0] = g;
        return;
    }
    if (g < mi[v][0][1]) mi[v][0][1] = g;
}
void dfs(int v,int p){
    par[v][0] = p;
    int g[3];
    rep(i,0,3) g[i] = inf;
    for (auto u : adj[v]){
        int w = u.Y;
        if (u.X == p) continue;
        h[u.X] = h[v]+1;
        mx[u.X][0] = w;
        dfs(u.X,v);
        if (w < g[0]){
            g[2] = g[1];
            g[1] = g[0];
            g[0] = w;
            continue;
        }
        if (w < g[1]){
            g[2] = g[1];
            g[1] = w;
            continue;
        }
        if (w < g[2]) g[2] = w;
    }
    for (auto u : adj[v]){
        if (u.X == p) continue;
        int w = u.Y;
        if (g[0] < w){
            mi[u.X][0][0] = g[0];
            if (g[1] < w) mi[u.X][0][1] = g[1];
            else mi[u.X][0][1] = g[2];
        }
        else{
            mi[u.X][0][0] = g[1];
            mi[u.X][0][1] = g[2];
        }
    }
}

int getpar(int v,int k){
    rep(i,0,20)
        if (k&(1 << i))
             v = par[v][i];
    return v;
}

int lca(int u,int v){
    if (h[u] > h[v]) swap(u,v);
    if (h[v]-h[u]) v = getpar(v,h[v]-h[u]);
    if (u == v) return u;
    repr(i,19,0){
        if (par[v][i] != par[u][i]){
            v = par[v][i];
            u = par[u][i];
        }
    }
    return par[v][0];
}

int calc_mx(int v,int k){
    int ans = 0;
    repr(i,19,0){
        if (k >= (1 << i)){
            ans = max(ans,mx[v][i]);
            v = par[v][i];
            k -= (1 << i);
        }
    }
    return ans;
}
int calc_mi(int v,int k){
    int ans = inf;
    repr(i,19,0){
        if (k >= (1 << i)){
            ans = min(ans,mi[v][i][0]);
            v = par[v][i];
            k -= (1 << i);
        }
    }
    return ans;
}
int getpr(int v){
    if (pr[v] == v) return v;
    return pr[v] = getpr(pr[v]);
}

void init (int nn,int mm,vector<int> U,vector<int> V,vector<int> W){
    memset(mi,63,sizeof mi);
    memset(bmi,63,sizeof bmi);
    n = nn;
    m = mm;
    rep(i,0,n) pr[i] = i;
    rep(i,0,m){
        U[i]++;
        V[i]++;
        e.pb({W[i],{U[i],V[i]}});
    }
    sort(all(e));
    rep(i,0,m){
        int x = e[i].Y.X,y = e[i].Y.Y;
        if (getpr(x) != getpr(y)){
            good[i] = 1;
            pr[pr[x]] = pr[y];
            adj[x].pb({y,e[i].X});
            adj[y].pb({x,e[i].X});
        }
    }
    dfs(1,0);
    rep(j,1,20){
        rep(i,1,n+1){
            par[i][j] = par[par[i][j-1]][j-1];
            mx[i][j] = max(mx[i][j-1],mx[par[i][j-1]][j-1]);
        }
    }
    rep(i,0,m){
        if (good[i]) continue;
        int u = e[i].Y.X,v = e[i].Y.Y,w = e[i].X;
        if (h[u] > h[v]) swap(u,v);
        int g = lca(u,v);
        if (u == g){
            fr[u].pb(w);
            be[v].pb(w);
            continue;
        }
        be[v].pb(w);
        be[u].pb(w);
        fr[g].pb(w);
        fr[g].pb(w);
    }
    dfs2(1,0);
    rep(j,1,20){
        rep(i,1,n+1){
            int x0 = mi[par[i][j-1]][j-1][0],x1 = mi[par[i][j-1]][j-1][1];
            rep(k,0,2) mi[i][j][k] = mi[i][j-1][k];
            if (x0 < mi[i][j][0]){
                mi[i][j][1] = min(x1,mi[i][j][0]);
                mi[i][j][0] = x0;
                continue;
            }
            if (x0 < mi[i][j][1])
                mi[i][j][1] = x0;
        }
    }
    debug("end");
}
int getMinimumFuelCapacity(int u, int v){
    u++;
    v++;
    if (h[u] > h[v]) swap(u,v);
    int w = lca(u,v);
    if (u == w){
        int ans = calc_mi(v,h[v]-h[u]-1);
        int x = getpar(v,h[v]-h[u]-1);
        ans = min(ans,bmi[x]);
        if (ans >= inf && adj[u].size() <= 2) return -1;
        if (ans >= inf){
            if (u == 1 || mx[u][0] >= mi[x][0][1]) ans = max(mi[x][0][1],calc_mx(v,h[v]-h[u]));
            else ans = max({mx[u][0],mi[x][0][0],calc_mx(v,h[v]-h[u])});
            return ans;
        }
        int mas = calc_mx(v,h[v]-h[u]);
        if (adj[u].size() <= 2) return max(mas,ans);
        ans = min(ans,mi[x][0][1]);
        if (u != 1) ans = min(ans,max(mx[u][0],mi[x][0][0]));
        return max(ans,mas);
    }
    else{
        int x = getpar(v,h[v]-h[w]-1),y = getpar(u,h[u]-h[w]-1);
        int ans = min(calc_mi(v,h[v]-h[w]-1),calc_mi(u,h[u]-h[w]-1));
        ans = min({ans,bmi[x],bmi[y]});
        if (w != 1) ans = min(ans,mx[w][0]);
        if (mi[x][0][0] != mx[y][0]) ans = min(ans,mi[x][0][0]);
        else ans = min(ans,mi[x][0][1]);
        if (ans >= inf) return -1;
        int mas = max(calc_mx(v,h[v]-h[w]),calc_mx(u,h[u]-h[w]));
        return max(ans,mas);
    }
}
# Verdict Execution time Memory Grader output
1 Correct 13 ms 28116 KB Output is correct
2 Correct 14 ms 28140 KB Output is correct
3 Correct 13 ms 28116 KB Output is correct
4 Correct 14 ms 28244 KB Output is correct
5 Correct 14 ms 28372 KB Output is correct
6 Correct 14 ms 28372 KB Output is correct
7 Correct 14 ms 28360 KB Output is correct
8 Correct 14 ms 28500 KB Output is correct
9 Correct 142 ms 54568 KB Output is correct
10 Correct 178 ms 63400 KB Output is correct
11 Correct 216 ms 61748 KB Output is correct
12 Correct 212 ms 64348 KB Output is correct
13 Correct 168 ms 68128 KB Output is correct
14 Correct 185 ms 53152 KB Output is correct
15 Correct 426 ms 64648 KB Output is correct
16 Correct 459 ms 60400 KB Output is correct
17 Correct 411 ms 69916 KB Output is correct
18 Correct 451 ms 66856 KB Output is correct
19 Correct 137 ms 37900 KB Output is correct
20 Correct 566 ms 64440 KB Output is correct
21 Correct 571 ms 61572 KB Output is correct
22 Correct 602 ms 66360 KB Output is correct
23 Correct 533 ms 67364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 28116 KB Output is correct
2 Correct 14 ms 28140 KB Output is correct
3 Correct 245 ms 53716 KB Output is correct
4 Correct 239 ms 54884 KB Output is correct
5 Correct 255 ms 54288 KB Output is correct
6 Correct 234 ms 54672 KB Output is correct
7 Correct 233 ms 54592 KB Output is correct
8 Correct 261 ms 53616 KB Output is correct
9 Correct 233 ms 54420 KB Output is correct
10 Correct 251 ms 53492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 28116 KB Output is correct
2 Correct 14 ms 28140 KB Output is correct
3 Correct 13 ms 28116 KB Output is correct
4 Correct 14 ms 28244 KB Output is correct
5 Correct 14 ms 28372 KB Output is correct
6 Correct 14 ms 28372 KB Output is correct
7 Correct 14 ms 28360 KB Output is correct
8 Correct 14 ms 28500 KB Output is correct
9 Correct 13 ms 28116 KB Output is correct
10 Correct 16 ms 28360 KB Output is correct
11 Correct 13 ms 28372 KB Output is correct
12 Correct 13 ms 28372 KB Output is correct
13 Correct 14 ms 28244 KB Output is correct
14 Correct 16 ms 28244 KB Output is correct
15 Incorrect 13 ms 28308 KB Output isn't correct
16 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 13 ms 28116 KB Output is correct
2 Correct 13 ms 28116 KB Output is correct
3 Correct 14 ms 28140 KB Output is correct
4 Correct 13 ms 28116 KB Output is correct
5 Correct 14 ms 28244 KB Output is correct
6 Correct 14 ms 28372 KB Output is correct
7 Correct 14 ms 28372 KB Output is correct
8 Correct 14 ms 28360 KB Output is correct
9 Correct 14 ms 28500 KB Output is correct
10 Correct 142 ms 54568 KB Output is correct
11 Correct 178 ms 63400 KB Output is correct
12 Correct 216 ms 61748 KB Output is correct
13 Correct 212 ms 64348 KB Output is correct
14 Correct 168 ms 68128 KB Output is correct
15 Correct 16 ms 28360 KB Output is correct
16 Correct 13 ms 28372 KB Output is correct
17 Correct 13 ms 28372 KB Output is correct
18 Correct 14 ms 28244 KB Output is correct
19 Correct 16 ms 28244 KB Output is correct
20 Incorrect 13 ms 28308 KB Output isn't correct
21 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 13 ms 28116 KB Output is correct
2 Correct 14 ms 28140 KB Output is correct
3 Correct 13 ms 28116 KB Output is correct
4 Correct 14 ms 28244 KB Output is correct
5 Correct 14 ms 28372 KB Output is correct
6 Correct 14 ms 28372 KB Output is correct
7 Correct 14 ms 28360 KB Output is correct
8 Correct 14 ms 28500 KB Output is correct
9 Correct 142 ms 54568 KB Output is correct
10 Correct 178 ms 63400 KB Output is correct
11 Correct 216 ms 61748 KB Output is correct
12 Correct 212 ms 64348 KB Output is correct
13 Correct 168 ms 68128 KB Output is correct
14 Correct 185 ms 53152 KB Output is correct
15 Correct 426 ms 64648 KB Output is correct
16 Correct 459 ms 60400 KB Output is correct
17 Correct 411 ms 69916 KB Output is correct
18 Correct 451 ms 66856 KB Output is correct
19 Correct 245 ms 53716 KB Output is correct
20 Correct 239 ms 54884 KB Output is correct
21 Correct 255 ms 54288 KB Output is correct
22 Correct 234 ms 54672 KB Output is correct
23 Correct 233 ms 54592 KB Output is correct
24 Correct 261 ms 53616 KB Output is correct
25 Correct 233 ms 54420 KB Output is correct
26 Correct 251 ms 53492 KB Output is correct
27 Correct 16 ms 28360 KB Output is correct
28 Correct 13 ms 28372 KB Output is correct
29 Correct 13 ms 28372 KB Output is correct
30 Correct 14 ms 28244 KB Output is correct
31 Correct 16 ms 28244 KB Output is correct
32 Incorrect 13 ms 28308 KB Output isn't correct
33 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 13 ms 28116 KB Output is correct
2 Correct 13 ms 28116 KB Output is correct
3 Correct 14 ms 28140 KB Output is correct
4 Correct 13 ms 28116 KB Output is correct
5 Correct 14 ms 28244 KB Output is correct
6 Correct 14 ms 28372 KB Output is correct
7 Correct 14 ms 28372 KB Output is correct
8 Correct 14 ms 28360 KB Output is correct
9 Correct 14 ms 28500 KB Output is correct
10 Correct 142 ms 54568 KB Output is correct
11 Correct 178 ms 63400 KB Output is correct
12 Correct 216 ms 61748 KB Output is correct
13 Correct 212 ms 64348 KB Output is correct
14 Correct 168 ms 68128 KB Output is correct
15 Correct 185 ms 53152 KB Output is correct
16 Correct 426 ms 64648 KB Output is correct
17 Correct 459 ms 60400 KB Output is correct
18 Correct 411 ms 69916 KB Output is correct
19 Correct 451 ms 66856 KB Output is correct
20 Correct 245 ms 53716 KB Output is correct
21 Correct 239 ms 54884 KB Output is correct
22 Correct 255 ms 54288 KB Output is correct
23 Correct 234 ms 54672 KB Output is correct
24 Correct 233 ms 54592 KB Output is correct
25 Correct 261 ms 53616 KB Output is correct
26 Correct 233 ms 54420 KB Output is correct
27 Correct 251 ms 53492 KB Output is correct
28 Correct 16 ms 28360 KB Output is correct
29 Correct 13 ms 28372 KB Output is correct
30 Correct 13 ms 28372 KB Output is correct
31 Correct 14 ms 28244 KB Output is correct
32 Correct 16 ms 28244 KB Output is correct
33 Incorrect 13 ms 28308 KB Output isn't correct
34 Halted 0 ms 0 KB -