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. 关于使用栈将一般运算式翻译为后缀表达式并实现三级运算的方法及实例(cpp版)

    #include <iostream> #include <stack> #include <vector> #include <string> #de ...

  2. Tornado 协程

    同步异步I/O客户端 from tornado.httpclient import HTTPClient,AsyncHTTPClient def ssync_visit(): http_client ...

  3. Beta冲刺Day3

    项目进展 李明皇 今天解决的进度 完善了程序的运行逻辑(消息提示框等) 明天安排 前后端联动调试 林翔 今天解决的进度 向微信官方申请登录验证session以维护登录态 明天安排 继续完成维护登录态 ...

  4. 使用SecureCRTP 连接生产环境的web服务器和数据库服务器

    一.使用SecureCRTP 连接生产环境的web服务器 首先,需要知道以下参数信息: 1.web服务器的ip地址     2.服务器的端口号    3.会话连接的用户名和密码   4.服务器的用户名 ...

  5. VisualVM 使用 service:jmx:rmi:///...无法连接linux远程服务器

    VisualVM 无法使用 service:jmx:rmi:///jndi/rmi:///jmxrmi 连接到 关闭远程机器的防火墙即可:service iptables stop 不关闭防火墙的解决 ...

  6. 5种做法实现table表格中的斜线表头效果

    table表格,这个东西大家肯定都不陌生,代码中我们时常都能碰到,那么给table加一个斜线的表头有时是很有必要的,但是到底该怎么实现这种效果呢? 我总结了以下几种方法: 1.最最最简单的做法 直接去 ...

  7. MobileNet_v2

    研究动机: 神经网络彻底改变了机器智能的许多领域,实现了超人的准确性.然而,提高准确性的驱动力往往需要付出代价:现代先进网络需要高度计算资源,超出许多移动和嵌入式应用的能力. 主要贡献: 发明了一个新 ...

  8. JavaScript-Jquery实现全选反选

    html: <dl> <dt><input type="checkbox" id="checkAll" /><labe ...

  9. SpringBoot+Angular2 开发环境搭建

    https://segmentfault.com/a/1190000007921675

  10. SpringCloud的应用发布(一)SpringCloud的样例工程

    前言 这个综合例子创建了 6个微服务应用 一个服务注册中心 SvcReg(EurekaServer),生产中要考虑高可用 一个配置中心 CfgMgr + git目录存储配置(ConfigServer, ...