Submission #1269936

#TimeUsernameProblemLanguageResultExecution timeMemory
1269936sasdeSwapping Cities (APIO20_swap)C++20
0 / 100
169 ms36008 KiB
#include<bits/stdc++.h>
#include "swap.h"
using namespace std;
// #define sz(a) (int)a.size()
#define all(x) x.begin(),x.end()
#define ii pair<int,int>
#define se second
#define fi first
#define pb push_back
#define emb emplace_back
const int N=3e5+5,lg=18,mod=1e9+7;
struct pt
{
    int u,v,w;
};
vector<int>ver[N];
int r[N],n,st[N],ed[N],a[N],root[N];
bool check[N],ok[N];
int acs(int u){
    return r[u]==u?u:r[u]=acs(r[u]);
}
bool join(int u,int v,int pos){
    int pu=acs(u);
    int pv=acs(v);
    if(pu==pv){
        root[pu]=pos;
        ok[pu]=false;
        return true;
    }
    ok[pu]&=ok[pv];
    if(ok[pu]){
        if(st[pu]==u&&(st[pv]==v||ed[pv]==v)){
            st[pu]=st[pv]==v?st[pv]:ed[pv];
        }
        else{
            if(ed[pu]==u&&(st[pv]==v||ed[pv]==v)){
                ed[pu]=st[pv]==v?st[pv]:ed[pv];
            }
            else {
                ok[u]=true;
            }
        }
    }
    root[u]=pos;
    r[v]=u;
    return !ok[u];
}

int up[N][lg+5],h[N];

void dfs(int u,int cha)
{
    // cout <<u<<'\n';
    for(auto v:ver[u])
    {
        if(v==cha)continue;
        up[v][0]=u;
        h[v]=h[u]+1;
        dfs(v,u);
    }
}

int lca(int u,int v)
{
    if(h[u]<h[v])swap(u,v);
    for(int j=lg;j>=0;--j)
       {
        if(h[u]-h[v]>=(1<<j))
            u=up[u][j];
       }
     if(u==v)return u;
     for(int j=lg;j>=0;--j)
     {
        if(up[u][j]!=up[v][j])
        {
            u=up[u][j];
            v=up[v][j];
        }
     }
    return up[u][0];
}

void build(int val){
    dfs(val,-1);
    for(int j=1;j<=lg;++j)
    for(int i=1;i<=val;++i)up[i][j]=up[up[i][j-1]][j-1];
}

vector<pt>b;
int nod;
void init(int node,int edge,vector<int>u,vector<int>v,vector<int>w){
    nod=node;
    for(int i=0;i<edge;++i){
        ++u[i];
        ++v[i];
        b.pb({u[i],v[i],w[i]});
    }
    sort(all(b),[&](const pt &x,const pt &y){
        return x.w<y.w;
    });
    for(int i=1;i<=node;++i){
        r[i]=i;
        st[i]=ed[i]=i;
        ok[i]=true;
        root[i]=i;
    }
    for(int i=0;i<edge;++i){
        int u=acs(b[i].u);
        int v=acs(b[i].v);
        // cout <<i+1+node<<" "<<root[u]<<" "<<root[v]<<'\n';
        if(u==v){
            ver[i+1+node].emb(root[u]);
            check[i+1+node]=join(b[i].u,b[i].v,i+1+node);
        }
        else{
            ver[i+1+node].emb(root[u]);
            ver[i+1+node].emb(root[v]);
            check[i+1+node]=join(b[i].u,b[i].v,i+1+node);
        }
        // cout <<i+1+edge<<'\n';
    }
    build(node+edge);
    check[0]=true;

}
int getMinimumFuelCapacity(int x,int y){
    ++x;
    ++y;
    int l=lca(x,y);
    // cout <<x<<" "<<y<<" "<<l<<'\n';
    if(check[l]){
        return b[l-nod-1].w;
    }
    for(int j=lg;j>=0;--j){
        int x=up[l][j];
        if(check[x])continue;
        l=x;
    }
    l=up[l][0];
    if(l==0)return -1;
    return b[l-nod-1].w;

}
// main()
// {
//     ios_base::sync_with_stdio(false);
//     cin.tie(NULL);
//     cout.tie(NULL);
//     #define task "aws"
//     if(fopen(task".inp","r")){
//       freopen(task".inp","r",stdin);
//       freopen(task".out","w",stdout);
//     }
//     int node,edge,query;
//     cin >> node >> edge;
//     vector<int>a,b,c;
//     for(int i=1;i<=edge;++i){
//         int u,v,w;
//         cin >> u >> v >> w;
//         a.emb(u);
//         b.emb(v);
//         c.emb(w);
//     }
//     init(node,edge,a,b,c);
//     cin >> query;
//     // cout <<query<<'\n';
//     while(query--){
//         int u,v;
//         cin >> u >> v;
//         cout<<getMinimumFuelCapacity(u,v);cout<<'\n';
//     }

// }
#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...