Language: C++

#include <cstdio>
#include <cstring>
#include <algorithm>
#define inf 0x3f3f3f3f

using namespace std;

namespace SETS {

int fa[100005];

void init(int n) {
  for(int i=1;i<=n;i++) fa[i]=i;
}

int find_father(int x) {
  return (fa[x]==x)?x:fa[x]=find_father(fa[x]);
}

void merge(int x,int y) {
  x=find_father(x);y=find_father(y);
  if (x==y) return;
  fa[x]=y;
}

}

int a[100005],b[100005];
vector <int> vt1[200005],vt2[200005];

int calc(int n,int m) {
  SETS::init(n);
  for(int i=1;i<=m;i++) {
  	vt1[i].clear();
  	vt2[i].clear();
  }
  for(int i=1;i<=n;i++) {
  	vt1[a[i]].push_back(i);
  	vt2[b[i]].push_back(i);
  }
  for(int i=1;i<=m;i++) {
  	for(int j=0;j<vt1[i].size();j++) SETS::merge(vt1[i][j],vt2[i][j]);
  	for(int j=1;j<vt1[i].size();j++) SETS::merge(vt1[i][j],vt1[i][j-1]);
  }
  int ans=0;
  for(int i=1;i<=n;i++)
    if (SETS::find_father(i)==i) ans++;
  return n+ans;
}

int num[200005],num2[100005],num3[100005];
int cnt1[200005],cnt2[200005];

int main() {
  int n,s=0;
  scanf("%d",&n);
  for(int i=1;i<=n;i++) {
    scanf("%d",&a[i]);
    num[i]=a[i];
    s^=a[i];
  } 
  for(int i=1;i<=n;i++) {
    scanf("%d",&b[i]);
    num[n+i]=b[i];
  }
  int m=2*n;
  sort(num+1,num+m+1);
  int fir=s;
  s=lower_bound(num+1,num+m+1,s)-num;
  if (num[s]!=fir) s=0;
  int pp=0;
  for(int i=1;i<=n;i++)
    if (a[i]!=b[i]) {
    	pp++;
    	num2[pp]=lower_bound(num+1,num+m+1,a[i])-num;
    	num3[pp]=lower_bound(num+1,num+m+1,b[i])-num;
	}
  n=pp;
  for(int i=1;i<=n;i++) {
  	a[i]=num2[i];
  	b[i]=num3[i];
  	cnt1[a[i]]++;
  	cnt2[b[i]]++;
  }
  int diff=0;
  for(int i=1;i<=m;i++)
    if (cnt1[i]!=cnt2[i]) diff+=abs(cnt1[i]-cnt2[i]);
  int ans=inf;
  if (!diff) ans=min(ans,calc(n,m));
  if (diff==2) {
  	int p=0;
  	for(int i=1;i<=n;i++)
  	  if (cnt1[a[i]]>cnt2[a[i]]) p=a[i];
  	for(int i=1;i<=n;i++)
  	  if (b[i]==s&&cnt2[b[i]]>cnt1[b[i]]) {
  	  	  swap(b[i],p);
  	  	  ans=min(ans,calc(n,m)-1);
  	  	  swap(b[i],p);
  	  	  break;
		}
  }
  if (!diff) {
  	for(int i=1;i<=n;i++)
  	  if (s==b[i]) {
  	  	  ans=min(ans,calc(n,m)-1);
  	  	  break;
		}
  }
  printf("%d\n",(ans<inf)?ans:-1);
  return 0;
}

Compiler output:
a.cc:29:1: error: 'vector' does not name a type
   29 | vector <int> vt1[200005],vt2[200005];
      | ^~~~~~
a.cc: In function 'int calc(int, int)':
a.cc:34:9: error: 'vt1' was not declared in this scope
   34 |         vt1[i].clear();
      |         ^~~
a.cc:35:9: error: 'vt2' was not declared in this scope
   35 |         vt2[i].clear();
      |         ^~~
a.cc:38:9: error: 'vt1' was not declared in this scope
   38 |         vt1[a[i]].push_back(i);
      |         ^~~
a.cc:39:9: error: 'vt2' was not declared in this scope
   39 |         vt2[b[i]].push_back(i);
      |         ^~~
a.cc:42:23: error: 'vt1' was not declared in this scope
   42 |         for(int j=0;j<vt1[i].size();j++) SETS::merge(vt1[i][j],vt2[i][j]);
      |                       ^~~
a.cc:42:64: error: 'vt2' was not declared in this scope
   42 |         for(int j=0;j<vt1[i].size();j++) SETS::merge(vt1[i][j],vt2[i][j]);
      |                                                                ^~~
a.cc:43:23: error: 'vt1' was not declared in this scope
   43 |         for(int j=1;j<vt1[i].size();j++) SETS::merge(vt1[i][j],vt1[i][j-1]);
      |                       ^~~