# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
729398 | onepunchac168 | Swapping Cities (APIO20_swap) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
typedef long long ll;
typedef pair <ll,ll> ii;
typedef pair <ll,ii> iii;
ll n,m;
ll parent[200005][20];
ll gg;
struct DSU{
ll lab[200005];
bool check[200005];
ll tmp[200005];
ll countedge[200005];
void makeset(int u)
{
lab[u]=-1;
check[u]=0;
tmp[u]=-1;
countedge[u]=0;
}
ll findset(int u)
{
if (lab[u]<0)
{
return u;
}
return lab[u]=findset(lab[u]);
}
void Union(int u,int v,ll w)
{
int r=findset(u);
int s=findset(v);
countedge[u]++;
countedge[v]++;
if (r==s)
{
if (check[r]==0)
{
check[r]=1;
tmp[r]=w;
}
return;
}
if (lab[r]>lab[s])
{
swap(r,s);
}
lab[r]+=lab[s];
lab[s]=r;
if (check[r]!=0)
}
} dsu;
ll solve(ll aa,ll bb)
{
if (aa==-1&&bb==-1)
{
return -1;
}
if (aa==-1)
{
return bb;
}
if (bb==-1)
{
return aa;
}
return min(aa,bb);
}
void dfs(int u,int vv)
{
for (auto v:vt[u])
{
if (v==vv)
{
continue;
}
parent[v][0]=u;
for (int i=1;i<=18;i++)
{
parent[v][i]=parent[parent[v][i-1]][i-1];
}
dsu.tmp[v]=solve(dsu.tmp[v],dsu.tmp[u]);
dfs(v,u);
}
}
void init(int N, int M,std::vector<int> U, std::vector<int> V, std::vector<int> W) {
n=N-1;
m=M-1;
gg=n;
vector <iii> vt;
for (int i=0;i<=2*n+1;i++)
{
dsu.makeset(i);
tmp[i]=-1;
}
for (int i=0;i<=m;i++)
{
ll u=U[i];
ll v=V[i];
ll w=W[i];
vt.pb({w,{u,v}});
}
sort (vt.begin(),vt.end());
for (auto v:vt)
{
dsu.Union(v.se.fi,v.se.se,v.fi);
}
dfs(gg,-1);
}
int getMinimumFuelCapacity(int X, int Y) {
return 0;
}