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
置换的整数幂有这样的结论:
T^k将长度为L的置换T分裂成gcd(L,K)份,每个循环分别是循环T中下标i mod gcd(l,k)=0,1,2…的元素的连接。
那么T^2分裂成gcd(L,2)分
也就是说,原置换平方后,偶数置换会分裂,奇数置换不变
开方运算实际上就是合并相同的置换
平方后的置换中偶数置换肯定是分裂后的结果,合并
奇数置换就不用合并
把循环求出来,排序
如果是偶数循环且没有长度相同的循环,那么说明无解,因为根本无法合并
注意奇数循环也要改变,因为奇数循环平方后顺序改变了
比如
1 4 2 5 3     ->1->4->2->5->3->
平方后就是
1 2 3 4 5     ->1->2->3->4->5->
给一张图直观理解(L=10,K=3)
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
struct ZYYS
{
int sum;
vector<int>p;
}s[];
int vis[],tot,a[],n,q[],ans[];
bool cmp(ZYYS a,ZYYS b)
{
return a.sum<b.sum;
}
int gi()
{
char ch=getchar();
int x=;
while (ch<''||ch>'') ch=getchar();
while (ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x;
}
int dfs(int x,int cnt)
{
if (vis[x]) return cnt;
vis[x]=;
s[tot].p.push_back(x);
dfs(a[x],cnt+);
}
int main()
{int i,flag=,j;
cin>>n;
for (i=;i<=n;i++)
a[i]=gi();
for (i=;i<=n;i++)
if (vis[i]==)
{
s[++tot].sum=dfs(i,);
}
sort(s+,s+tot+,cmp);
for (i=;i<=tot;i++)
{
if (s[i].sum&) continue;
else
{
if (s[i+].sum==s[i].sum) {i++;continue;}
else {flag=;break;}
}
}
if (flag)
{
cout<<-<<endl;
return ;
}
for (i=;i<=tot;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+)%s[i].sum];
}
else
{
for (j=;j<s[i].sum;j++)
{
ans[s[i].p[j]]=s[i+].p[j];
ans[s[i+].p[j]]=s[i].p[(j+)%s[i].sum];
}
i++;
}
}
for (i=;i<=n;i++)
printf("%d ",ans[i]);
}

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

  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. Struts2——第一个helloworld页面

    struts2是一个较为成熟的mvc框架,先看看怎么配置struts2并且产生helloworld页面. 首先从官网下载struts2,http://struts.apache.org/downloa ...

  2. Beta冲刺Day7

    项目进展 李明皇 今天解决的进度 部分数据传递和使用逻辑测试 林翔 今天解决的进度 服务器端查看个人发布的action,修改已发布消息状态的action,仍在尝试使用第三方云存储功能保存图片 孙敏铭 ...

  3. PYTHON 词云

    #!/usr/bin/env python # -*- coding:utf-8 -*- import matplotlib.pyplot as plt from wordcloud import W ...

  4. 根据HTML5的新方法 drag & drop 方法实现表格拖拽实例

    上一次学习了html5的drag和drop方法,传送门  就自己写了个例子加深自己对drag和drop的理解.不过一开始不是很简单,遇到了不少问题.还好网络万能的,什么都能查到,总算完成了. 说明和详 ...

  5. $(function(){})和window.onload的区别

    (1)$(function(){}):DOM节点创建 完成才执行 (2)window.onload:页面所有资源(JS/CSS)加载完成才执行

  6. 分布式系统之消息中间件rabbitmq

    分布式系统之消息中间件rabbitmq 博客分类: 感谢:  一般php 用rabbitmq  java 用activemq  http://spartan1.iteye.com/blog/11802 ...

  7. Sudoku 第一步

    看到这个问题的思路是先解决生成数独生成器的编写,然后再解决数独求解的问题.最开始第一想法就是暴力求解,仔细算一下复杂度,发现这肯定耗时很久,于是看了很多博客(见转载).我们用回溯搜出来正解.

  8. PHP模式设计之单例模式、工厂模式、注册树模式、适配器模式、观察者模式

    php模式设计之单例模式 什么是单例模式? 单例模式是指在整个应用中只有一个实例对象的设计模式 为什么要用单例模式? php经常要链接数据库,如果在一个项目中频繁建立连接数据库,会造成服务器资源的很大 ...

  9. VMware虚拟机,从厚置备改成精简置备,并减小硬盘的实际占用空间

    工作中由于前期规划不足,导致磁盘空间分配较大,而且是厚置备.后期不再需要时,无法把用不到的空间释放出来,造成空间浪费.经过摸索和实验验证,总结出来一套方法. 风险提示:这个方法在我的环境中验证通过了, ...

  10. Trensient的使用介绍

    1. transient的作用及使用方法 我们都知道一个对象只要实现了Serilizable接口,这个对象就可以被序列化,java的这种序列化模式为开发者提供了很多便利,我们可以不必关系具体序列化的过 ...