目录

题目链接

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;
}

AGC016D - XOR Replace 置换/轮换的更多相关文章

  1. 【做题】agc016d - XOR Replace——序列置换&环

    原文链接 https://www.cnblogs.com/cly-none/p/9813163.html 题意:给出初始序列\(a\)和目标序列\(b\),都有\(n\)个元素.每次操作可以把\(a\ ...

  2. agc016D - XOR Replace(图论 智商)

    题意 题目链接 给出两个长度为\(n\)的数组\(a, b\) 每次可以将\(a\)中的某个数替换为所有数\(xor\)之和. 若\(a\)数组可以转换为\(b\)数组,输出最少操作次数 否则输出\( ...

  3. [agc016d]xor replace

    题意: 题解: 棒棒的神仙题...这题只是D题???(myh:看题五分钟,讨论两小时) 首先这个异或和是假的,比如我现在有$a=(a_1,a_2,a_3,a_4)$,操作一下$a_2$,就变成了$a= ...

  4. AGC 16 D - XOR Replace

    AGC 16 D - XOR Replace 附上attack(自为风月马前卒爷) 的题解 Problem Statement There is a sequence of length N: a=( ...

  5. 【agc016D】XOR Replace

    Portal --> agc016D Description ​ 一个序列,一次操作将某个位置变成整个序列的异或和,现在给定一个目标序列,问最少几步可以得到目标序列 ​ Solution ​ 翀 ...

  6. AtcoderGrandContest 016 D.XOR Replace

    $ >AtcoderGrandContest \space 016 D.XOR\space Replace<$ 题目大意 : 有两个长度为 \(n\) 的数组 \(A, B\) ,每次操作 ...

  7. Agc016_D XOR Replace

    传送门 题目大意 给定两个长为$n$的序列$A,B$你可以进行若干组操作,每次操作选定一各位置$x$,令$A_x$等于$A$的异或和. 问能否通过一定操作使得$A$成为$B$,如果能,求最小操作书数. ...

  8. Atcoder D - XOR Replace(思维)

    题目链接:http://agc016.contest.atcoder.jp/tasks/agc016_d 题解:稍微想一下就知道除了第一次的x是所有的异或值,之后的x都是原先被替换掉的a[i]所以要想 ...

  9. AGC016题解

    呼我竟然真的去刷了016QwQ[本来以为就是个flag的233] 感觉AGC题目写起来都不是很麻烦但是确实动脑子qvq[比较适合训练我这种没脑子选手] 先扔个传送门:点我 A.Shrinking 题意 ...

随机推荐

  1. OI中卡常数技巧

    一.I/O优化 读入优化是卡常数最重要的一条! inline int read() { ,f=;char c=getchar(); ;c=getchar();} +c-';c=getchar();} ...

  2. 速查mysql数据大小

    速查mysql数据大小 # 1.查看所有数据库大小 mysql> select concat(round(sum(DATA_LENGTH/1024/1024),2),'MB') as data ...

  3. <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>

    <%String path = request.getContextPath();String basePath = request.getScheme()+"://"+re ...

  4. python Django cookie和session

    在一个会话的多个请求中共享数据,这就是会话跟踪技术.例如在一个会话中的请求如下:  请求银行主页: 请求登录(请求参数是用户名和密码): 请求转账(请求参数与转账相关的数据): 请求信誉卡还款(请求参 ...

  5. SQL Server数据库的备份和还

    转:http://blog.csdn.net/zwj7612356/article/details/8188025 在sql server数据库中,备份和还原都只能在服务器上进行,备份的数据文件在服务 ...

  6. Karma (Test runner)

    Karma https://baike.baidu.com/item/%E7%BE%AF%E7%A3%A8/7618552?fromtitle=Karma&fromid=878453 1,意译 ...

  7. 选择性搜索(SS)算法

    一.目标检测和目标识别 目标识别(object recognition)是要指明一张图像中包含哪类目标.输入是图像,输出是图像中的目标属于的类别(class probability).目标检测是识别出 ...

  8. v-model

    仅用于以下控件: <input> <select> <textarea> 组件 v-model以Vue 实例的数据作为数据来源,应当在组件的 data 选项中声明初 ...

  9. Python中应该使用%还是format来格式化字符串?

    转载自http://www.cnblogs.com/liwenzhou/p/8570701.html %的特点是,前面有几个%,后面的括号里就得有几个参数,如果只有一个%,括号可以省略 基本格式 'a ...

  10. 关于Linux的随笔笔记

    1.Ctrl+c.Ctrl+z和Ctrl+d的区别: Ctrl+c和Ctrl+z都是中断命令,但是他们的作用却不一样: Ctrl+c 是向当前进程发送SIGINT信号,用于终止进程: Ctrl+z 是 ...