meaning of the title
Give two arrays $a, b with a length of $n $$
You can replace one of $a $with the sum of all of $xor $at a time.
If $a $array can be converted to $b $array, output the minimum number of operations
Otherwise output $- 1$
Sol
Generally, if you see this kind of $N \leqslant 10^5 $and can't do it, you must find the conclusion first
It's not hard to see that when we replace all the numbers $xor $and then $xor $again, we must get the replaced numbers.
In fact, we can put the number from xor into a new position $N+1$, so that each operation becomes the number of exchanging position $N+1$and any position $x$
And the general problem becomes
Two arrays of $a, b $with length of $N+1 $are given. The number of $\ forall i \in [1, n] $positions and $N+1 $positions can be exchanged in $a $each time. The minimum number of exchanges is changed to $b $array
First, judge the situation of $- 1 $. Obviously, after sorting two arrays, if there is a position different, there must be no solution
Otherwise, there must be solutions.
I won't be here....
The official explanation is very immortal.
For $I $locations, if $a_i \not = b_i $, then connect $a_i $to $b_i $
Final answer = total number of sides + number of connected blocks - 1
Think about why, for the points in the connection block, if their size is $x $, we can change their corresponding $a $and $b $by $x-1 $operations
For different connecting blocks, we also need one step to convert the number of the $N+1 $positions between the two connecting blocks (except the first one)
For the position of $N+1 $, you need to consider it separately: if it is already in the Unicom block, you don't need to consider it; otherwise, you need to consider it as a separate Unicom block
otherwise
2 1 3 3 1
The number of connecting blocks can be maintained by parallel query sets
#include<bits/stdc++.h> const int MAXN = 4e5 + 10; using namespace std; inline int read() { char c = getchar(); int x = 0, f = 1; while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();} while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } int N; int a[MAXN], b[MAXN], ta[MAXN], tb[MAXN], sa, sb, tot = 0, date[MAXN], fa[MAXN]; map<int, bool> ti; int find(int x) { return fa[x] == x ? fa[x] : fa[x] = find(fa[x]); } int unionn(int x, int y) { fa[x] = y; } int main() { N = read(); for(int i = 1; i <= N; i++) a[i] = read(), sa ^= a[i]; a[N + 1] = sa; for(int i = 1; i <= N; i++) b[i] = read(), sb ^= b[i]; b[N + 1] = sb; N++; memcpy(ta, a, sizeof(a)); memcpy(tb, b, sizeof(b)); sort(ta + 1, ta + N + 1); sort(tb + 1, tb + N + 1); for(int i = 1; i <= N - 1; i++) if(ta[i] != tb[i]) return puts("-1"), 0; int ans = 0, num = 0; for(int i = 1; i <= N; i++) if(a[i] != b[i] || (i == N)) { date[++num] = a[i]; date[++num] = b[i]; if(i < N)ans++;//The last one is considered separately } if(ans == 0) return puts("0"), 0; sort(date + 1, date + num + 1); num = unique(date + 1, date + num + 1) - date - 1; for(int i = 1; i <= num; i++) fa[i] = i; for(int i = 1; i <= N; i++) if(a[i] != b[i]) { a[i] = lower_bound(date + 1, date + num + 1, a[i]) - date, b[i] = lower_bound(date + 1, date + num + 1, b[i]) - date; if(!ti[a[i]]) ti[a[i]] = 1; if(!ti[b[i]]) ti[b[i]] = 1; unionn(find(a[i]), find(b[i])); } for(int i = 1; i <= num; i++) if(fa[i] == i) ans++; printf("%d", ans - 1); return 0; }