Submission #1304027

#TimeUsernameProblemLanguageResultExecution timeMemory
1304027FaggiSwapping Cities (APIO20_swap)C++20
0 / 100
1793 ms589824 KiB
#include <bits/stdc++.h>
#define ll long long
#define sz(x) int(x.size())
#define forn(i, n) for (i = 0; i < n; i++)
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define fr first
#define se second
using namespace std;
const int MAXN = 1e5 + 1;
const int LOG = 18;
vector<pair<ll, ll>> grafo[MAXN], grafo2[MAXN];
ll up[MAXN][LOG], miU[MAXN][LOG], mi3[MAXN][LOG], depth[MAXN], mi1[MAXN], mi2[MAXN], mi2U[MAXN];
ll n;
void dfs(ll nod, ll pad, ll dis)
{
    up[nod][0] = pad;
    miU[nod][0] = dis;
    if (sz(grafo[nod]) >= 3)
        mi3[nod][0] = grafo[nod][2].fr;
    else
        mi3[nod][0] = LLONG_MAX;
    for (ll i = 1; i < LOG; i++)
    {
        up[nod][i] = up[up[nod][i - 1]][i - 1];
        miU[nod][i] = max(miU[nod][i - 1], miU[up[nod][i - 1]][i - 1]);
        mi3[nod][i] = min(mi3[nod][i - 1], mi3[up[nod][i - 1]][i - 1]);
    }
    ll cant = 0, act = 0;
    for (auto k : grafo[nod])
    {
        if (k.se == pad)
            continue;
        cant++;
        act = max(act, k.fr);
        if (cant == 2)
            mi2[nod] = min(mi2[nod], act);
        depth[k.se] = depth[nod] + 1;
        dfs(k.se, nod, k.fr);
        mi2[nod] = min(mi2[nod], max(mi2[k.se], k.fr));
    }
}

void dfs2(ll nod, ll pad)
{
    for (auto k : grafo[nod])
    {
        if (k.se == pad)
            continue;
        mi2U[k.se] = min(mi2U[k.se], max(k.fr, mi2U[nod]));
        ll act = 0, cant = 0, act2 = LLONG_MAX;
        for (auto l : grafo[nod])
        {
            if (l.se == pad || l.se == k.se)
                continue;
            cant++;
            if (cant > 2)
                break;
            act = max(l.fr, act);
        }
        if(cant<2)
            act=LLONG_MAX;
        for (auto l : grafo2[nod])
        {
            if (l.se == pad || l.se == k.se)
                continue;
            act2 = min(l.fr, act2);
            break;
        }
        act = min(act, act2);
        act = max(act, k.fr);
        mi2U[k.se] = min(mi2U[k.se], act);
    }
}
ll m;
void init(int N, int M,
          std::vector<int> U, std::vector<int> V, std::vector<int> W)
{
    ll i;
    n = N;
    m = M;
    for (i = 0; i < N; i++)
        mi2U[i] = mi1[i] = mi2[i] = LLONG_MAX;
    for (i = 0; i < M; i++)
    {
        grafo[U[i]].pb({W[i], V[i]});
        grafo[V[i]].pb({W[i], U[i]});
    }
    for (i = 0; i < N; i++)
        sort(all(grafo[i]));
    dfs(0, 0, 0);
    for (i = 0; i < M; i++)
    {
        grafo2[U[i]].pb({max(mi2[i], 1ll * W[i]), V[i]});
        grafo2[V[i]].pb({max(mi2[i], 1ll * W[i]), U[i]});
    }
    for (i = 0; i < N; i++)
        sort(all(grafo2[i]));
}

ll k_ans(ll x, ll d)
{
    for (ll i = LOG - 1; i >= 0; i--)
        if ((1 << i) & d)
            x = up[x][i];
    return x;
}

ll lca(ll x, ll y)
{
    if (depth[x] < depth[y])
        swap(x, y);
    if (x == y)
        return x;
    x = k_ans(x, depth[x] - depth[y]);
    if (x == y)
        return x;
    for (ll i = LOG - 1; i >= 0; i--)
    {
        if (up[x][i] != up[y][i])
        {
            x = up[x][i];
            y = up[y][i];
        }
    }
    return up[x][0];
}

ll calc(ll x, ll y)
{
    ll ret = 0, d = depth[x] - depth[y];
    for (ll i = 0; i < LOG; i++)
    {
        if ((1 << i) & d)
        {
            ret = max(ret, miU[x][i]);
            x = up[x][i];
        }
    }
    return ret;
}

ll calc2(ll x, ll y, ll z)
{
    ll ret = LLONG_MAX, d = depth[x] - depth[y];
    if (x != y)
        ret = mi2[x];
    if (x == y)
    {
        ret = mi2U[x];
        d = depth[z] - depth[x]-1;
        for (ll i = 0; i < LOG; i++)
            if ((1 << i) & d)
                z = up[z][i];
        ll act=0, cant=0, act2=LLONG_MAX;
        for(auto k:grafo[x])
        {
            if(k.se==z)
                continue;
            cant++;
            act=max(k.fr,act);
            if(cant==2)
                break;
        }
        if(cant<2)
            act=LLONG_MAX;
        for(auto k:grafo2[x])
        {
            if(k.se==z)
                continue;
            act2=max(k.fr,mi2[k.se]);
            break;
        }
        ret=min(ret,min(act,act2));
        return ret;
    }
    x = up[x][0];
    for (ll i = 0; i < LOG; i++)
    {
        if ((1 << i) & d)
        {
            ret = min(ret, mi3[x][i]);
            x = up[x][i];
        }
    }
    return ret;
}

int getMinimumFuelCapacity(int X, int Y)
{
    ll Z = lca(X, Y), ans, act = LLONG_MAX;
    if (m != n - 1)
        return -1;
    if (Z != X && Z != Y)
        act = mi3[Z][0];
    ans = max(calc(X, Z), calc(Y, Z));
    ans = max(ans, min(min(calc2(X, Z, Y), calc2(Y, Z, X)), act));
    if (ans == LLONG_MAX)
        return -1;
    return ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...