Language: C++

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

struct node {
    int x,id;
}ord[100005],ord2[100005];

int n,flag;
int fa[100005],a[100005],b[100005],c[100005],c2[100005],vis[100005],v[100005];

map<int,int> mp;

bool cmp(node a, node b) {
    return a.x<b.x;
}

int Find(int x){
    if(fa[x]!=x) fa[x]=Find(fa[x]);
    return fa[x];
}
void Unionn(int x,int y) {
    x=Find(x); y=Find(y);
    fa[x]=y;
}

void Discrete(){
    int cnt=1;
    sort(ord+1, ord+2+n, cmp);
    c[ord[1].id]=cnt++;
    for(int i=2; i<=n+1; i++) {
        if(ord[i].x==ord[i-1].x) cnt--;
        c[ord[i].id]=cnt++;
    }
    cnt=1;
    sort(ord2+1, ord2+2+n, cmp);
    c2[ord2[1].id]=cnt++;
    for(int i=2; i<=n+1; i++) {
        if(ord2[i].x==ord2[i-1].x) cnt--;
        c2[ord2[i].id]=cnt++;
    }
}

int main(void)
{   
    int ans=0;
    cin >>n;
    for(int i=1; i<=n; i++) {
        scanf("%d",&a[i]);
        ord[i]=(node){a[i],i};
        a[n+1]^=a[i];
        mp[a[i]]++;
    }
    mp[a[n+1]]++;
    ord[n+1]=(node){a[n+1],n+1};
    for(int i=1; i<=n; i++) {
        scanf("%d",&b[i]);
        ord2[i] =(node){b[i],i};
        b[n+1]^=b[i];
        mp[b[i]]--;
    }
    ord2[n+1]=(node){b[n+1],n+1};
    mp[b[n+1]]--;
    for(map<int,int>::iterator it=mp.begin(); it!=mp.end(); it++) {
        if(it->second !=0) {
            printf("-1\n");
            return 0;
        }
    }
    if(b[n+1]!=a[n+1]) ans++;
    Discrete();

    // for(int i=1; i<=n+1; i++) cout <<c[i]<<" ";
    // cout <<endl;
    // for(int i=1; i<=n+1; i++) cout <<c2[i]<<" ";
    // cout <<endl;

    int flag=1;
    vector<int> n1,n2;
    for(int i=1; i<=n+1; i++) {
        if(c[i]==c2[i]) continue;
        if(flag && c[n+1]==c2[i]) ans++,flag=0;
        n1.push_back(c[i]); n2.push_back(c2[i]);
    }
    for(int i=0; i<n1.size(); i++) fa[n1[i]]=n1[i];
    for(int i=0; i<n1.size(); i++) Unionn(n1[i], n2[i]);

        // for(int i=0; i<n1.size(); i++) cout<<n1[i]<<": "<<fa[n1[i]]<<endl;

    int cnt=0,local[100005];
    // memset(ig,0,sizeof(ig));
    for(int i=0; i<n1.size(); i++){ 
        if(fa[n1[i]]==n1[i] && !local[n1[i]]) {
            local[n1[i]]=1;
            cnt++;
        }
    }
    unique(n1.begin(), n1.end());
    int gg=0;
    for (int i=n1.begin();i<=n1.end();++i) ++gg;
    // cout <<gg<<" "<<ans<<" "<<cnt<<endl;
    printf("%d\n", gg-ans+cnt);

    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:98:11: warning: ignoring return value of '_FIter std::unique(_FIter, _FIter) [with _FIter = __gnu_cxx::__normal_iterator<int*, vector<int> >]', declared with attribute 'nodiscard' [-Wunused-result]
   98 |     unique(n1.begin(), n1.end());
      |     ~~~~~~^~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:61,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algo.h:891:5: note: declared here
  891 |     unique(_ForwardIterator __first, _ForwardIterator __last)
      |     ^~~~~~
a.cc:100:24: error: cannot convert 'std::vector<int>::iterator' to 'int' in initialization
  100 |     for (int i=n1.begin();i<=n1.end();++i) ++gg;
      |                ~~~~~~~~^~
      |                        |
      |                        std::vector<int>::iterator
a.cc:100:28: error: no match for 'operator<=' (operand types are 'int' and 'std::vector<int>::iterator')
  100 |     for (int i=n1.begin();i<=n1.end();++i) ++gg;
      |                           ~^~~~~~~~~~
      |                           |        |
      |                           int      std::vector<int>::iterator
In file included from /usr/include/c++/14/regex:68,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:181:
/usr/include/c++/14/bits/regex.h:1154:5: note: candidate: 'template<class _BiIter> bool std::__cxx11::operator<=(const sub_match<_BiIter>&, const sub_match<_BiIter>&)'
 1154 |     operator<=(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1154:5: note:   template argument deduction/substitution failed:
a.cc:100:37: note:   mismatched types 'const std::__cxx11::sub_match<_BiIter>' and 'int'
  100 |     for (int i=n1.begin();i<=n1.end();++i) ++gg;
      |                                     ^
/usr/include/c++/14/bits/regex.h:1260:5: note: candidate: 'template<class _Bi_iter, class _Ch_traits, class _Ch_alloc> bool std::__cxx11::operator<=(__sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>&, const sub_match<_BiIter>&)'
 1260 |     operator<=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1260:5: note:   template argument deduction/substitution failed:
a.cc:100:37: note:   mismatched types 'std::__cxx11::__sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>' and 'int'
  100 |     for (int i=n1.begin();i<=n1.end();++i) ++gg;
      |                                     ^
/usr/include/c++/14/bits/regex.h:1353:5: note: candidate: 'template<class _Bi_iter, class _Ch_traits, class _Ch_alloc> bool std::__cxx11::operator<=(const sub_match<_BiIter>&, __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>&)'
 1353 |     operator<=(const sub_match<_Bi_iter>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1353:5: note:   template argument deduction/substitution failed:
a.cc:100:37: note:   mismatched types 'const std::__cxx11::sub_match<_BiIter>' and 'int'
  100 |     for (int i=n1.begin();i<=n1.end();++i) ++gg;
      |                                     ^
/usr/include/c++/14/bits/regex.h:1427:5: note: candidate: 'template<class _Bi_iter> bool std::__cxx11::operator<=(const typename std::iterator_traits<_Iter>::value_type*, const sub_match<_BiIter>&)'
 1427 |     operator<=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1427:5: note:   template argument deduction/substitution failed:
a.cc:100:37: note:   'std::vector<int>::iterator' is not derived from 'const std::__cxx11::sub_match<_BiIter>'
  100 |     for (int i=n1.begin();i<=n1.end();++i) ++gg;
      |                                     ^
/usr/include/c++/14/bits/regex.h:1521:5: note: candidate: 'template<class _Bi_iter> bool std::__cxx11::operator<=(const sub_match<_BiIter>&, const typename std::iterator_traits<_Iter>::value_type*)'
 1521 |     operator<=(const sub_match<_Bi_iter>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1521:5: note:   template argument deduction/substitution failed:
a.cc:100:37: note:   mismatched types 'const std::__cxx11::sub_match<_BiIter>' and 'int'
  100 |     for (int i=n1.begin();i<=n1.end();++i) ++gg;
      |                                     ^
/usr/include/c++/14/bits/regex.h:1599:5: note: candidate: 'template<class _Bi_iter> bool std::__cxx11::operator<=(const typename std::iterator_traits<_Iter>::value_type&, const sub_match<_BiIter>&)'
 1599 |     operator<=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1599:5: note:   template argument deduction/substitution failed:
a.cc:100:37: note:   'std::vector<int>::iterator' is not derived from 'const std::__cxx11::sub_match<_BiIter>'
  100 |     for (int i=n1.begin();i<=n1.end();++i) ++gg;
      |                                     ^
/usr/include/c++/14/bits/regex.h:1699:5: note: candidate: 'template<class _Bi_iter> bool std::__cxx11::operator<=(const sub_match<_BiIter>&, const typename std::iterator_traits<_Iter>::value_type&)'
 1699 |     operator<=(const sub_match<_Bi_iter>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1699:5: note:   template argument deduction/substitution failed:
a.cc:100:37: note:   mismatched types 'const std::__cxx11::sub_match<_BiIter>' and 'int'
  100 |     for (int i=n1.begin();i<=n1.end();++i) ++gg;
      |                                     ^
In file included from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/algorithm:60:
/usr/include/c++/14/bits/stl_pair.h:1064:5: note: candidate: 'template<class _T1, class _T2> constexpr bool std::operator<=(const pair<_T1, _T2>&, const pair<_T1, _T2>&)'
 1064 |     operator<=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_pair.h:1064:5: note:   template argument deduction/substitution failed:
a.cc:100:37: note:   mismatched types 'const std::pair<_T1, _T2>' and 'int'
  100 |     for (int i=n1.begin();i<=n1.end();++i) ++gg;
      |                                     ^
In file included from /usr/include/c++/14/bits/stl_algobase.h:67:
/usr/include/c++/14/bits/stl_iterator.h:469:5: note: candidate: 'template<class _Iterator> constexpr bool std::operator<=(const reverse_iterator<_Iterator>&, const reverse_iterator<_Iterator>&)'
  469 |     operator<=(const reverse_iterator<_Iterator>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:469:5: note:   template argument deduction/substitution failed:
a.cc:100:37: note:   mismatched types 'const std::reverse_iterator<_Iterator>' and 'int'
  100 |     for (int i=n1.begin();i<=n1.end();++i) ++gg;
      |                                     ^
/usr/include/c++/14/bits/stl_iterator.h:513:5: note: candidate: 'template<class _IteratorL, class _IteratorR> constexpr bool std::operator<=(const reverse_iterator<_Iterator>&, const reverse_iterator<_IteratorR>&)'
  513 |     operator<=(const reverse_iterator<_IteratorL>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:513:5: note:   template argument deduction/substitution failed:
a.cc:100:37: note:   mismatched types 'const std::reverse_iterator<_Iterator>' and 'int'
  100 |     for (int i=n1.begin();i<=n1.end();++i) ++gg;
      |                                     ^
/usr/include/c++/14/bits/stl_iterator.h:1704:5: note: candidate: 'template<class _IteratorL, class _IteratorR> constexpr bool std::operator<=(const move_iterator<_IteratorL>&, const move_iterator<_IteratorR>&)'
 1704 |     operator<=(const move_iterator<_IteratorL>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1704:5: note:   template argument deduction/substitution failed:
a.cc:100:37: note:   mismatched types 'const std::move_iterator<_IteratorL>' and 'int'
  100 |     for (int i=n1.begin();i<=n1.end();++i) ++gg;
      |                                     ^
/usr/include/c++/14/bits/stl_iterator.h:1767:5: note: candidate: 'template<class _Iterator> constexpr bool std::operator<=(const move_iterator<_IteratorL>&, const move_iterator<_IteratorL>&)'
 1767 |     operator<=(const move_iterator<_Iterator>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1767:5: note:   template argument deduction/substitution failed:
a.cc:100:37: note:   mismatched types 'const std::move_iterator<_IteratorL>' and 'int'
  100 |     for (int i=n1.begin();i<=n1.end();++i) ++gg;
      |                                     ^
In file included from /usr/include/c++/14/bits/basic_string.h:47,
                 from /usr/include/c++/14/string:54,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52:
/usr/include/c++/14/string_view:717:5: note: candidate: 'template<class _CharT, class _Traits> constexpr bool std::operator<=(basic_string_view<_CharT, _Traits>, basic_string_view<_CharT, _Traits>)'
  717 |     operator<=(basic_string_view<_CharT, _Traits> __x,
      |     ^~~~~~~~
/usr/include/c++/14/string_view:717:5: note:   template argument deduction/substitution failed:
a.cc:100:37: note:   mismatched types 'std::basic_string_view<_CharT, _Traits>' and 'int'
  100 |     for (int i=n1.begin();i<=n1.end();++i) ++gg;
      |                                     ^
/usr/include/c++/14/string_view:724:5: note: candidate: 'template<class _CharT, class _Traits> constexpr bool std::operator<=(basic_string_view<_CharT, _Traits>, __type_identity_t<basic_string_view<_CharT, _Traits> >)'
  724 |     operator<=(basic_string_view<_CharT, _Traits> __x,
      |     ^~~~~~~~
/usr/include/c++/14/string_view:724:5: note:   template argument deduction/substitution failed:
a.cc:100:37: note:   mismatched types 'std::basic_string_view<_CharT, _Traits>' and 'int'
  100 |     for (int i=n1.begin();i<=n1.end();++i) ++gg;
      |                                     ^
/usr/include/c++/14/string_view:732:5: note: candidate: 'template<class _CharT, class _Traits> constexpr bool std::operator<=(__type_identity_t<basic_string_view<_CharT, _Traits> >, basic_string_view<_CharT, _Trait