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. 怎样创建XML文档

    在程序中,我们怎样创建一个XML文档.下面演示中,Insus.NET在程序创建一个和http://www.cnblogs.com/insus/p/3274220.html 一模一样的XML文档. 可以 ...

  2. 使用穷人版profiler定位调试MySQL

    此文已由作者温正湖授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 周末闲得蛋疼,来英飞特做人工空气净化器.开了电脑后,习惯性得点击xshell按钮,进入InnoSQL稳定性测 ...

  3. [Spring Boot]Request method 'GET' not supported

    在网上查了些资料没有解决,检查代码发现 @PostMapping public String add(Employee employee){ System.out.println(employee); ...

  4. html中img标签的url如何拼接变量

    <img id="pic" /> <script type="text/javascript"> var url = "xxx ...

  5. 设计一个MQ的考虑点

    转自: https://segmentfault.com/a/1190000004461970 序 这里总结下MQ的研究点,以及如果要自己设计一个MQ应该考虑的因素. 考虑点 1.远程通信功能 (1) ...

  6. git 增量打包

    git diff f506693 ccc253c3 --name-only | xargs tar -czvf update.tar.gz

  7. 洛谷P2900 [USACO08MAR]土地征用Land Acquisition(斜率优化)

    题意 约翰准备扩大他的农场,眼前他正在考虑购买N块长方形的土地.如果约翰单买一块土 地,价格就是土地的面积.但他可以选择并购一组土地,并购的价格为这些土地中最大的长 乘以最大的宽.比如约翰并购一块3 ...

  8. U19464 山村游历(Wander) LCT维护子树大小

    \(\color{#0066ff}{ 题目描述 }\) 在一个偏远的小镇上,有一些落后的山村.山村之间通过一些道路来连接.当然有的山村可能不连通. 一年当中会发生很多大事,比如说有人提议要在山村\(i ...

  9. CF1005F Berland and the Shortest Paths

    \(\color{#0066ff}{ 题目描述 }\) 一个无向图(边权为1),输出一下选边的方案使\(\sum d_i\)最小(\(d_i\)为从1到i的最短路) 输出一个方案数和方案(方案数超过k ...

  10. Git 的简单测试

    Git 简介 Git(读音为/gɪt/.)是一个开源的分布式版本控制系统,可以有效.高速的处理从很小到非常大的项目版本管理. Git 是 Linus Torvalds 为了帮助管理 Linux 内核开 ...