【AGC 016 D】XOR Replace

Description

There are two sequences({Ai},{Bi}) of length N.
Your goal is to minimize the times of operations that turning A into B.You can repeatly perform the following operations:
1.Let the XOR of all elements in A be X
2.Replace a arbitrary element in A with X
 Especially,if it is impossible to turn A into B,output “-1”
 N<=10^5,Ai,Bi<=2^30
 

Analysis

let a[0] be a[1] xor a[2] xor …. xor a[n],and so as b[0],and then it is simple to judge -1
the operation equal to swap a[0] and a[i]
After considering some simple cases,we notice that the procedure corresponds permutations,which mind us using graph to solve it.
Conclusion:If the connected component(including k vertex) including a[0],then ans+=k else ans+=k+1
Obviously if a[i]=b[i] we will never change it,else we link an edge from a[i] to b[i].
Since the in-degree equals to the out-degree for any vertex,every component constitute a Eulerian circuit.So we prove the conclusion above.
Now the problem is simple:Only need to count the number of connected component in the graph by union-find sets.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值