Description

A permutation of length n is an array containing each integer from 1 to n exactly once. For example, q = [4, 5, 1, 2, 3] is a permutation. For the permutation q the square of permutation is the permutation p that p[i] = q[q[i]] for each i = 1... n. For example, the square of q = [4, 5, 1, 2, 3] is p = q2 = [2, 3, 4, 5, 1].

This problem is about the inverse operation: given the permutation p you task is to find such permutation q that q2 = p. If there are several such q find any of them.

Input

The first line contains integer n (1 ≤ n ≤ 106) — the number of elements in permutation p.

The second line contains n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the elements of permutation p.

Output

If there is no permutation q such that q2 = p print the number "-1".

If the answer exists print it. The only line should contain n different integers qi (1 ≤ qi ≤ n) — the elements of the permutation q. If there are several solutions print any of them.

Sample Input

 

Input
4
2 1 4 3
Output
3 4 2 1
Input
4
2 1 3 4
Output
-1
Input
5
2 3 4 5 1
Output
4 5 1 2 3

简单题意

给出一个大小为n的置换群p,求一个置换群q,使得q^2=p

胡说题解

首先我们观察q^2是怎么运算的,置换群可以看成一个一个的环,每个点i向a[i]连一条边就是那个图

q^2其实就是把i的边变成a[a[i]],也就是在环上走两步,然后q原本的环就变了

1.假设原来是奇数环,那么后来还是一个奇数环,只是顺序变了

2.假设原来是偶数环,那么就会拆成两个大小为一半的环

我们再看p

p上的奇数环可能是原来的奇数环,也有可能是偶数环拆开得来的

p上的偶数环只可能是原来的偶数环拆开得来

对于奇数环我们只要把这个环的后半部分与前半部分(先把这个环断开)交替插入就可以构造出原来的那个奇数环

对于偶数环我们就只能找一个相同大小的偶数环交替插入,即两个相同大小的偶数环合并,如果找不到相同大小的偶数环,那么我们就知道不存在这样的q使得q^2=p

 #include<cstdio>
#include<algorithm>
using namespace std; const int maxn=; int n,tot,a[maxn],b[maxn],s[maxn],l[maxn],cir[maxn];
bool flag[maxn]; bool com(int a,int b){
return l[a]<l[b];
} int main(){
scanf("%d",&n);
int i,j,k;
for(i=;i<=n;i++)scanf("%d",&a[i]);
for(i=;i<=n;i++)
if(!flag[i]){
cir[++tot]=i;
flag[i]=true;
++l[i];
j=a[i];
while(!flag[j]){
flag[j]=true;
++l[i];
j=a[j];
}
}
sort(cir+,cir++tot,com);
int x=;
bool f=true;
for(i=;i<=tot;i++)
if((l[cir[i]]&)== ){
if(x==)x=l[cir[i]];
else
if(x==l[cir[i]])x=;
else f=false;
}
if(x!=)f=false;
if(f==false)printf("-1");
else{
for(i=;i<=tot;i++){
if((l[cir[i]]&)==){
j=cir[i];
k=;
while(flag[j]){
s[k]=j;
flag[j]=false;
k=(k+)%l[cir[i]];
j=a[j];
}
for(j=;j<l[cir[i]]-;j++)b[s[j]]=s[j+];
b[s[l[cir[i]]-]]=s[];
}
else{
j=cir[i];
k=cir[i+];
while(flag[j]){
b[j]=k;
b[k]=a[j];
flag[j]=false;
flag[k]=false;
j=a[j];
k=a[k];
}
++i;
}
}
for(i=;i<=n;i++)printf("%d ",b[i]);
}
return ;
}

AC代码

Square Root of Permutation - CF612E的更多相关文章

  1. Codeforces 612E - Square Root of Permutation

    E. Square Root of Permutation A permutation of length n is an array containing each integer from 1 t ...

  2. CF612E Square Root of Permutation

    题目分析 我们首先模拟一下题意 假设有一个 \(q _1\) \(p\) \(a_1\) \(a_x\) \(a_{a_1}\) \(a_{a_x}\) \(q\) \(x\) \(a_1\) \(a ...

  3. codefroces 612E Square Root of Permutation

    A permutation of length n is an array containing each integer from 1 to n exactly once. For example, ...

  4. [CF 612E]Square Root of Permutation

    A permutation of length n is an array containing each integer from 1 to n exactly once. For example, ...

  5. Codeforces.612E.Square Root of Permutation(构造)

    题目链接 \(Description\) 给定一个\(n\)的排列\(p_i\),求一个排列\(q_i\),使得对于任意\(1\leq i\leq n\),\(q_{q_i}=p_i\).无解输出\( ...

  6. Codeforces 715A. Plus and Square Root[数学构造]

    A. Plus and Square Root time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  7. Project Euler 80:Square root digital expansion 平方根数字展开

    Square root digital expansion It is well known that if the square root of a natural number is not an ...

  8. Codeforces 715A & 716C Plus and Square Root【数学规律】 (Codeforces Round #372 (Div. 2))

    C. Plus and Square Root time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  9. (Problem 57)Square root convergents

    It is possible to show that the square root of two can be expressed as an infinite continued fractio ...

随机推荐

  1. 成都Uber优步司机奖励政策(1月16日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  2. 我错了的N个学习

    其实在面对自己的失误的时候,勇敢的说我错了,就是我错了,不找借口,不找理由,然后就开始分析错误的原因,分析,总结,学习,提高,成为自己成长的垫脚石,这个才是正确的做法,做人要拿出精神头,拼死至休的劲头 ...

  3. MyBatis-SpringMVC整合

    1.添加spring相关jar包 2.配置ehcache jar包. 3.添加ehcache mybatis 适配器jar包(在mybatis官网) 4.添加spring mybatis 适配器jar ...

  4. 回写盘写速度被限速为10M左右

    问题现像如下图所示: 用hd-speed等测试虚拟盘速度都能达到90M/s左右,但复制文件到虚拟盘速度最高只有10M/s 原因:由于客户机开机加载这个随机驱动和随机进程后,会对磁盘启动进程等有扫描检查 ...

  5. wireshark抓包分析——TCP/IP协议

    本文来自网易云社区 当我们需要跟踪网络有关的信息时,经常会说"抓包".这里抓包究竟是什么?抓到的包又能分析出什么?在本文中以TCP/IP协议为例,简单介绍TCP/IP协议以及如何通 ...

  6. ogg的安装配置 配置双向同步(含DDL)

    第一部分 先配置单向同步(含DDL) 一 源端安装GoldenGate 创建用户 创建目录 mkdir -p /opt/ogg chmod -R 777 /opt/ogg chown -R oracl ...

  7. android学习十三 首选项

    1,首选项可用用来持久保存用户设置,游戏最高分等 2,首选项有,列表首选项,复选框首选项,对话框首选项.. 3,通过xml文件和代码创建首选项      addPreferencesFromResou ...

  8. VueJs 学习笔记

    VueJs学习笔记 参考资料:https://cn.vuejs.org/ 特效库:TweenJS(补间动画库)  VelocityJS(轻量级JS动画库) Animate.css(CSS预设动画库) ...

  9. Windows运行机理——窗口句柄和消息

    Windows运行机理这系列文章都是来至于<零基础学Qt4编程>——吴迪,个人觉得写得很好,所以进行了搬运和个人加工 现在我们将消息与句柄联系起来.假如有一个窗口,且拥有该窗口的一个句柄( ...

  10. 第六模块:WEB框架开发 第1章·Django框架开发1~50

    01-Django基础介绍 02-Web应用程序1 03-Web应用程序2 04-http请求协议1 05-http请求协议2 06-http协议之响应协议 07-wsgire模块1 08-wsgir ...