Language: C++

#include <bits/stdc++.h>
using namespace std;
const int MaxN = 100003;
int n,a[MaxN],b[MaxN];
int t1[MaxN],t2[MaxN];

int fa[MaxN],rank[MaxN];
bool vis[MaxN];
int getfa(int x) {
	if (x!=fa[x]) fa[x]=getfa(fa[x]);
	return fa[x];
}
void uni(int a,int b) {
	a=getfa(a),b=getfa(b);
	if (a!=b) {
		if (rank[a]<rank[b]) {
			fa[a]=b;
			rank[b]+=a;
		} else {
			fa[b]=a;
			rank[a]+=b;
		}
	}
}

int main() {
	scanf("%d",&n);
	for (int i=1;i<=n;++i) scanf("%d",a+i);
	for (int i=1;i<=n;++i) scanf("%d",b+i);
	for (int i=0;i<=n;++i) a[0]^=a[i],b[0]^=b[i];
	for (int i=0;i<=n;++i) t1[i]=a[i],t2[i]=b[i];
	sort(t1,t1+n+1),sort(t2,t2+n+1);
	for (int i=0;i<=n;++i)
		if (t1[i]!=t2[i]) {
			puts("-1");
			return 0;
		}
	int* ed=unique(t1,t1+n+1);
	for (int i=0;i<=n;++i) a[i]=lower_bound(t1,ed,a[i])-t1,b[i]=lower_bound(t1,ed,b[i])-t1;
	for (int i=0;i<=n;++i) fa[i]=i,rank[i]=1;
	int ans=0;
	for (int i=1;i<=n;++i) 
		if (a[i]==b[i]) a[i]=b[i]=-1;
		else {
			uni(a[i],b[i]);
			ans++;
		}
	if (ans==0) {
		puts("0");
		return 0;
	}
	for (int i=1;i<=n;++i) if (a[i]!=-1 && !vis[getfa(a[i])]) ans++,vis[getfa(a[i])]=true;
	if (vis[getfa(a[0])]) ans--;
	printf("%d\n",ans);
	return 0;
} 

Compiler output:
a.cc: In function 'void uni(int, int)':
a.cc:16:21: error: reference to 'rank' is ambiguous
   16 |                 if (rank[a]<rank[b]) {
      |                     ^~~~
In file included from /usr/include/c++/14/bits/stl_pair.h:60,
                 from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:7:14: note:                 'int rank [100003]'
    7 | int fa[MaxN],rank[MaxN];
      |              ^~~~
a.cc:16:29: error: reference to 'rank' is ambiguous
   16 |                 if (rank[a]<rank[b]) {
      |                             ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:7:14: note:                 'int rank [100003]'
    7 | int fa[MaxN],rank[MaxN];
      |              ^~~~
a.cc:18:25: error: reference to 'rank' is ambiguous
   18 |                         rank[b]+=a;
      |                         ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:7:14: note:                 'int rank [100003]'
    7 | int fa[MaxN],rank[MaxN];
      |              ^~~~
a.cc:21:25: error: reference to 'rank' is ambiguous
   21 |                         rank[a]+=b;
      |                         ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:7:14: note:                 'int rank [100003]'
    7 | int fa[MaxN],rank[MaxN];
      |              ^~~~
a.cc: In function 'int main()':
a.cc:40:40: error: reference to 'rank' is ambiguous
   40 |         for (int i=0;i<=n;++i) fa[i]=i,rank[i]=1;
      |                                        ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:7:14: note:                 'int rank [100003]'
    7 | int fa[MaxN],rank[MaxN];
      |              ^~~~