目录

题目链接

AGC016D - XOR Replace

题解

可以发现一次操作相当于一次置换

对于每个a上的位置映射到b对应

可以找到置换群中的 所有轮换

一个k个元素的轮换需要k+1步完成

那么答案就是边数+轮换数-1

-1的话发现当最一个数为缺少的数时不需吧最后一步换回来

代码

#include<map>
#include<cstdio>
#include<algorithm>
#define gc getchar()
#define pc putchar
inline int read() {
int x = 0,f = 1;
char c = gc;
while(c < '0' || c > '9')c = gc;
while(c <= '9' && c >= '0') x = x * 10 + c - '0',c = gc;
return x * f;
}
void print(int x) {
if(x < 0) {
pc('-');
x = -x;
}
if(x >= 10) print(x / 10);
pc(x % 10 + '0');
}
const int maxn = 1000007;
int a[maxn],b[maxn],c[maxn],d[maxn];
int n;
std::map<int,int>f;
int fa[maxn];
int find(int x) {
if(fa[x] != x) fa[x] = find(fa[x]);
return fa[x];
}
int main() {
n = read();
for(int i = 1;i <= n;++ i) a[i] = read();
for(int i = 1;i <= n;++ i) b[i] = read();
int t = 0;
for(int i = 1;i <= n;++ i) t ^= a[i];
a[n + 1] = t;
t = 0;
for(int i = 1;i <= n;++ i) t ^= b[i];
b[++ n] = t;
for(int i = 1;i <= n;++ i) c[i] = a[i],d[i] = b[i];
std::sort(c + 1,c + n + 1);
std::sort(d + 1,d + n + 1);
for(int i = 1;i <= n;++ i) {
if(c[i] != d[i]) {
puts("-1");
return 0;
}
}
int tot = 0;
int ans = 0;
for(int i = 1;i <= n;++ i) {
if(a[i] != b[i] || i == n) {
if(i < n) ans ++;
if(!f[a[i]])f[a[i]] = ++tot;
if(!f[b[i]])f[b[i]] = ++tot; }
}
if(!ans) {
pc('0');
return 0;
}
for(int i = 1;i <= tot;++ i) fa[i] = i;
for(int i = 1;i <= n;++ i) {
if(a[i] != b[i]) fa[find(f[a[i]])] = find(f[b[i]]);
}
for(int i = 1;i <= tot;++ i) if(fa[i] == i) ans ++;
print(ans - 1);
return 0;
}

最新文章

  1. 【转】Linux查看机器负载
  2. Python多线程(1)——介绍
  3. C#开发学习——ADO.NET几个重要对象
  4. Python自动化运维之7、生成器、迭代器、列表解析、迭代器表达式
  5. delphi 容错提示语句汇总
  6. dotnet core 开发无缝兼容Http和Websocket协议的接口服务
  7. python打包分发工具setuptools使用记录
  8. PHP手动搭建环境
  9. POJ 1230 Pass-Muraille
  10. java算法----排序----(5)归并排序
  11. git server 配置
  12. springboot区分开发、测试、生产多环境的应用配置
  13. 兼容 iOS Retina(视网膜显示) 的程序
  14. Codeforces 862C - Mahmoud and Ehab and the xor
  15. Spring Cloud和Dubbo整合开发笔记(1)
  16. Selenium geckodriver异常
  17. 使用subprocess.Poen注意事项
  18. C#基础学习之StreamReader和StreamWriter
  19. Oracle管理监控之Oracle数据库存储空间监控
  20. 汇智课堂 Node.js相关课程

热门文章

  1. HDU - 1078 FatMouse and Cheese (记忆化搜索)
  2. postgres 基本操作
  3. 应用调试(二)GDB
  4. Encryption and decryption、Steganography、Decryption Tools
  5. 分布式监控系统开发【day37】:填充表配置项目(三)
  6. HTML(九)HTML 条件注释规范
  7. Latex &quot;Error: Extra alignment tab has been changed to \cr. &quot;
  8. windows下 cmd 界面的替代者 cmder 推荐!
  9. 在JS中如何判断所输入的是一个数、整数、正数、非数值?
  10. python数据结构之堆(heap)