Union Find On Codeforces

Can you help me with this problem
here is my solution getting wa / I have used union find method `

#include<bits/stdc++.h>
using namespace std;

int main(){
    ios_base::sync_with_stdio(false);
int i,j,k=0,d,n=0,m,a,b,c,q,l,r;
cin>>n;
vector<int> x[1001],y[1001];
int arr[n+1];
for(i=0;i<=n;i++)
    arr[i]=i;
for(i=1;i<=n;i++){
    cin>>a>>b;
    if(x[a].size())
        arr[i]=x[a][0];
    else if(y[b].size())
        arr[i]=y[b][0];
    else {
        x[a].emplace_back(i);
        y[b].emplace_back(i);
    }

}
    for(i=1;i<=n;i++)
        if(arr[i]==i)
            k++;
cout<<k-1<<"\n";

return 0;
}

ps. I don’t know why codechef has so pathetic code sharing . use this linnk as an alternative for my solution code .