Skip to content

Instantly share code, notes, and snippets.

@latte0119
Created August 17, 2016 17:08
Show Gist options
  • Save latte0119/403e27755749c1ee688ac1df0139c8bb to your computer and use it in GitHub Desktop.
Save latte0119/403e27755749c1ee688ac1df0139c8bb to your computer and use it in GitHub Desktop.
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define all(v) (v).begin(),(v).end()
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define pb push_back
#define fi first
#define se second
template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}
struct UF{
vector<int>par,sz;
vector<int>f;
void init(int n){
par.resize(n);
sz.resize(n);
f.resize(n);
for(int i=0;i<n;i++){
par[i]=i;
sz[i]=1;
f[i]=0;
}
}
int find(int x){
return x==par[x]?x:par[x]=find(par[x]);
}
void unite(int x,int y){
x=find(x);y=find(y);
if(x==y)return;
sz[x]+=sz[y];
f[x]|=f[y];
par[y]=x;
}
bool same(int x,int y){
return find(x)==find(y);
}
int size(int x){
return sz[find(x)];
}
};
int N,H,W;
int x[44444],y[44444];
int px[111111],py[111111];
int solve(int T){
UF uf;uf.init(N);
memset(px,-1,sizeof(px));
memset(py,-1,sizeof(py));
rep(i,N){
if(px[x[i]]>=0)uf.unite(px[x[i]],i);
if(py[y[i]]>=0)uf.unite(py[y[i]],i);
if(y[i]==T)uf.f[uf.find(i)]=1;
px[x[i]]=i;py[y[i]]=i;
}
int cnt1=0,cnt2=0;
rep(i,N)if(uf.find(i)==i){
cnt1++;
if(uf.f[i])cnt2++;
}
if(cnt1==1)return N-1;
return N-cnt1+cnt1-cnt2+cnt1-1;
}
signed main(){
scanf("%lld%lld%lld",&N,&W,&H);
rep(i,N)scanf("%lld%lld",&x[i],&y[i]);
int ans=1001001001;
rep(i,2){
chmin(ans,solve(1));
chmin(ans,solve(H));
swap(H,W);
rep(j,N)swap(x[j],y[j]);
}
cout<<ans<<endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment