Submission #1340929

#TimeUsernameProblemLanguageResultExecution timeMemory
1340929shiba_ensureSwapping Cities (APIO20_swap)C++20
100 / 100
280 ms57244 KiB
#include <bits/stdc++.h>
#include <stdio.h>

#define __Shibae__      signed main()
#define IOS             ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define fiopen(Path)    freopen(Path".INP", "r", stdin); freopen(Path".OUT", "w", stdout);
#define fipen(Path)     freopen(Path".INP", "r", stdin);
#define sz(s)           (int)s.size()
#define all(x)          x.begin(), x.end()
#define maxHeap         priority_queue<int>
#define minHeap         priority_queue<int, vector<int>, greater<int>>
#define getBit(x, k)    (((x) >> (k)) & 1)
#define MASK(i)         (1LL << (i))
#define SQR(x)          (1LL * ((x) * (x)))
#define db              double
#define ld              long double
#define ui              unsigned int
#define ll              long long
#define ii              pair<int, int>
#define pli             pair<ll, int>
#define pil             pair<int, ll>
#define pll             pair<ll, ll>
#define fi              first
#define se              second

#define FOR(i, a, b)    for(int i = a, _b = b; i <= _b; i += 1)
#define FOD(i, a, b)    for(int i = a, _b = b; i >= _b; i -= 1)
#define REP(i, a)       for(int i = 0, _a = a; i < _a; i++)
#define pb              push_back
#define fau(u, a)       for(auto &u : a)
#define debug           return cout << "debug", void();

using namespace std;

const ll mod = 1e9 + 7;
const int INF = 1e9 + 7;
const ll INFLL = (ll)2e18 + 7LL;
const ld PI = acos(-1);
const int MAX = 5e5+5;
 
const int dx[] = {1, -1, 0, 0, -1, 1, 1, -1};
const int dy[] = {0, 0, 1, -1, -1, -1, 1, 1};

mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());

ll Rand(ll l, ll r) 
{
    return uniform_int_distribution<ll>(l, r)(rd);
}

template<class SHIBA, class ENGINE>
    bool minimize(SHIBA &x, const ENGINE y)
    {
        if(x > y)
        {
            x = y;
            return true;
        } 
        else return false;
    }
template<class SHIBA, class ENGINE>
    bool maximize(SHIBA &x, const ENGINE y)
    {
        if(x < y)
        {
            x = y;
            return true;
        }
        else return false;
    }


/* Template by: Nguyen Nhat Anh from Luong Van Chanh High School for the gifted */
/* From Min Tuoi with love */
        /**       TRY HARD        **/
        /**          ORZ          **/

/* -----------------[ MAIN CODE ]----------------- */

const int lg = 17;

int id;
vector<int> g[MAX];
int par[MAX][lg + 1];
bool wei[MAX][lg + 1];
bool gay[MAX];
int h[MAX];
int a[MAX];

int find_set(vector<int> &par, int u)
{
    return u == par[u] ? u : par[u] = find_set(par, par[u]);
}

void dfs(int u)
{
    fau(v, g[u])
    {
        // cout << u << " " << v << "\n";
        h[v] = h[u] + 1;

        par[v][0] = u;
        wei[v][0] = gay[u];

        FOR(i, 1, lg) 
        {
            par[v][i] = par[par[v][i-1]][i-1];
            wei[v][i] = wei[v][i-1] | wei[par[v][i-1]][i-1];
        }
        dfs(v);
    }
}

int lca(int u, int v)
{
    if (h[u] < h[v]) swap(u, v);
    for (int k = h[u] - h[v]; k; k ^= k & -k) u = par[u][__builtin_ctz(k)];

    if (u == v) return u;
    FOD(i, __lg(h[u]), 0) if (par[u][i] != par[v][i])
    {
        u = par[u][i];
        v = par[v][i];
    }
    return par[u][0];
}

void init(int n, int m, std::vector<int> U, std::vector<int> V, std::vector<int> W) 
{
    id = n;
    vector<int> dsu(n+5, 0);
    vector<int> dsutree(n+m+5, 0);
    vector<bool> check(n+5, 0);
    vector<int> deg(n+3, 0);
    vector<int> idx(m, 0);
    iota(all(idx), 0);
    sort(all(idx), [&](const int &x, const int &y){
        return W[x] < W[y];
    });
    iota(all(dsu), 0);

    FOR(i, 1, n) dsutree[i] = i;
    FOR(i, 1, n+m) 
    {
        g[i].clear();
        gay[i] = 0;
        a[i] = 0;
    }

    REP(j, m)
    {
        int i = idx[j];
        // cout << U[i] << " " << V[i] << " " << W[i] << "\n";
        U[i]++;
        V[i]++;
        deg[U[i]]++;
        deg[V[i]]++;

        int u = find_set(dsu, U[i]);
        int v = find_set(dsu, V[i]);

        if (u == v) check[u] = 1;
        if (max(deg[V[i]], deg[U[i]]) >= 3) check[u] = 1;

        int tmp = u;

        dsu[v] = u;
        check[u] = check[u] | check[v];

        u = find_set(dsutree, U[i]);
        v = find_set(dsutree, V[i]);

        id++;
        dsutree[id] = dsutree[u] = dsutree[v] = id;
        g[id].pb(u);
        if (u != v) g[id].pb(v);

        if (check[tmp])
        {
            gay[id] = 1;
            a[id] = W[i];
        }
    }
    h[id] = 0;
    dfs(id);
}

int getMinimumFuelCapacity(int X, int Y) 
{
    X++; Y++;
    int p = lca(X, Y);
    if (gay[p]) return a[p];

    int l = 1, r = h[p];
    int res = -1;

    while(l <= r)
    {
        int m = l + r >> 1;

        int c = 0;
        int u = p;

        for (int k = m; k; k ^= k & -k)
        {
            int i = __builtin_ctz(k);
            c |= wei[u][i];
            u = par[u][i];
        }

        if (c)
        {
            res = m;
            r = m-1;
        }
        else l = m+1;
    }
    if (res == -1) return res;

    for (int k = res; k; k ^= k & -k)
    {
        int i = __builtin_ctz(k);
        p = par[p][i];
    }

    return a[p];
}

#ifdef SHABI

__Shibae__
{
    fipen("test")

    int n, m;
    vector<int> u, v, w;

    cin >> n >> m;
    REP(i, m)
    {
        int x, y, z; cin >> x >> y >> z;
        u.pb(x);
        v.pb(y);
        w.pb(z);
    }

    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;
}
#endif
#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...