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.

Examples

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

题目大意:

给你个置换p,然后做平方运算,得到置换q,题目给你q,问你能否找到p,要构造出来。

题解:

这道题要求倒推出一个置换,由于原置换p中的环不一定全是奇数环,所以平方之后有可能有环会裂开。

对于平方后的置换q中的奇数环,直接在里面推。偶数环就看是否有相同大小的偶数环与它合并。

 //Never forget why you start
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
int n,m,a[],lm,ans[],q[];
struct node{
int sum;
vector<int>p;
friend bool operator < (const node a,const node b){
return a.sum<b.sum;
}
}s[];
int vis[],cnt;
void dfs(int r){
vis[r]=;
cnt++;
s[lm].p.push_back(r);
if(vis[a[r]])return;
else dfs(a[r]);
}
int main(){
int i,j;
scanf("%d",&n);
for(i=;i<=n;i++)scanf("%d",&a[i]);
for(i=;i<=n;i++)
if(!vis[i]){
cnt=;
lm++;
dfs(i);
s[lm].sum=cnt;
}
sort(s+,s+lm+);
bool flag=;
for(i=;i<=lm;i++){
if(s[i].sum&)continue;
else{
if(s[i+].sum==s[i].sum){i++;continue;}
else {flag=;break;}
}
}
if(flag){printf("-1\n");return ;}
for(i=;i<=lm;i++){
if(s[i].sum&){
for(j=;j<s[i].sum;j++)
q[j*%s[i].sum]=s[i].p[j];
for(j=;j<s[i].sum-;j++)
ans[q[j]]=q[j+];
ans[q[s[i].sum-]]=q[];
}
else{
int k=i+;
for(j=;j<s[i].sum;j++){
ans[s[i].p[j]]=s[k].p[j];
ans[s[k].p[j]]=s[i].p[(j+)%s[i].sum];
}
i++;
}
}
for(i=;i<=n;i++)
printf("%d ",ans[i]);
return ;
}

[CF 612E]Square Root of Permutation的更多相关文章

  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. 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, ...

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

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

  4. Square Root of Permutation - CF612E

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

  5. CF612E Square Root of Permutation

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

  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. UWP&WP8.1 基础控件——Grid

    Grid是一个面板控件  Grid是UWP和WPF,WP8.1中最重要的一个控件,相当相当重要. 他是一个面板控件,是用来添加其他控件   但是呢 用法确实简单的很. 大概就这个样子. 你用工具箱拖, ...

  2. springboot整合mybatis,redis,代码(四)

    一 说明 这是spring整合redis注解开发的系类: 二 正文 在注解开发时候,会有这几个注解需要注意: 具体含义: 1.@Cacheable 可以标记在方法上,也可以标记在类上.当标记在方法上时 ...

  3. windwos-sshfs

    从 http://www.jianshu.com/p/d79901794e3d 转载 目的 最近因为需要在linux虚拟机里进行开发程序,虽然在linux里有超强的编辑器vim,但vim开发html前 ...

  4. I2C(smbus pmbus)和SPI分析

    2C和SPI作为两种非常常用的低速外部总线 I2C I2C是以前的飞利浦半导体制定的标准,也就是如今的NXP. I2C总线由一条数据线(SDA)和一条时钟线(SCL)组成.设备分主从,主设备提供时钟, ...

  5. 读经典——《CLR via C#》(Jeffrey Richter著) 笔记_new新建对象

    CLR使用 new 操作符来创建新对象,例如:Employee e=new Employee("Param1");  以下是 new  操作符所做的事情. 它计算类型及其所有基类型 ...

  6. P2264 情书

    传送门 正常会想到字典树 然鹅数据怎么小直接map也能过 然后就写map暴力匹配了 毫无思维难度,毫无代码难度 注意逗号算单词分隔符,如果有句号就算另一句 同一句的单词重复出现只计算一次贡献 再开个m ...

  7. Flask&&人工智能AI -- 7 MongoDB

    MongoDB初识,增删改查操作,数据类型,$关键字以及$修改器.“$”的奇妙用法,Array Object的特殊操作,选取跳过排序,客户端操作 一.MongoDB初识 什么是MongoDB Mong ...

  8. 浅谈CSS3中display属性的Flex布局

    浅谈CSS3中display属性的Flex布局   最近在学习微信小程序,在设计首页布局的时候,新认识了一种布局方式display:flex 1 .container { 2 display: fle ...

  9. Java高级工程师应该掌握的东东

    今天偶然看了膜拜单车官网对java程序员的招聘要求,如下,可以对照发现自己的不足 职责 负责APP SERVER中间层等模块开发 完成各类需求开发任务,同时保证服务稳定性.茁壮性 要求 精通Java语 ...

  10. sqlserver 索引进阶(上)

    参考原文:http://www.cnblogs.com/tjy9999/p/4494662.html 2. 非聚集索引 SET STATISTICS io ON SET STATISTICS time ...