codefroces 612E Square Root of Permutation

时间:2021-01-08 13:58:15

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)
codefroces 612E Square Root of Permutation
 #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. &lbrack;CF 612E&rsqb;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&period;612E&period;Square Root of Permutation&lpar;构造&rpar;

    题目链接 \(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. Codeforces 715A&period; Plus and Square Root&lbrack;数学构造&rsqb;

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

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

  7. Codeforces 715A &amp&semi; 716C Plus and Square Root【数学规律】 &lpar;Codeforces Round &num;372 &lpar;Div&period; 2&rpar;&rpar;

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

  8. (Problem 57)Square root convergents

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

  9. Square Root

    Square RootWhen the square root functional configuration is selected, a simplified CORDIC algorithm ...

随机推荐

  1. Entity Framework 教程——安装Entity Framework环境

    安装Entity Framework环境 Entity Framework 5.0 API分布在两个地方,一个可在NuGet包管理器中找到,一个存在于.NET framework中..NET fram ...

  2. 深入理解JVM--JVM垃圾回收机制

    Java语言出来之前,大家都在拼命的写C或者C++的程序,而此时存在一个很大的矛盾,C++等语言创建对象要不断的去开辟空间,不用的时候有需要不断的去释放空间,既要写构造函数,又要写析构函数,很多时候都 ...

  3. 宣布正式发布 Biz Talk Services、Azure Active Directory 和 Traffic Manager&comma; 同时发布 Azure Active Directory 高级版预览

    除经济优势之外,云计算还在可转化为竞争优势的应用程序开发方面提供了更大的灵活性.我们很高兴看到每天创建的新 Windows Azure 订阅超过 1000 个,更令人兴奋的是,有一半客户使用价值更高的 ...

  4. keepalived(nat)&plus;ftp&plus;http

    一. 环境要求需要2台LVS和n(n>=2)台RS操作系统 负载均衡模式 VIP NVIPRHEL7.4 NAT 193.168.141.30 192.168.102.165 LVS1 LVS2 ...

  5. django项目部署上线

    前言 完善的django项目上线,有很多种上线的方法,比如apache, uwsgi, nginx等.这里只介绍2种,一种是django自带的,另外一种则是nginx + uwsgi完成介绍.这里的系 ...

  6. 将对象xml序列化和反序列化

    //将一个对象按XML序列化的方式写入到一个文件,使用的默认的UTF8编码格式 //o为要序列化的对象 //path保存文件的路径 public static object  _lockObj=new ...

  7. vue&lowbar;实例&lowbar;组件的生命周期

     重绘重排 中重复出现的是 mounted(){...} beforeUpdate(){...} uptated(){...} 其他钩子函数只会出现一次 <!DOCTYPE html> & ...

  8. windows共享文件夹权限设置

    权限设置及更改,最好在右键属性里面, 在计算机管理,共享文件夹->共享里面修改,有时候会不生效. windows的凭据修改,在用户注销后才会生效.

  9. Codeforces 786 B&period; Legacy

    题目链接:http://codeforces.com/contest/786/problem/B 典型线段树优化连边,线段树上的每一个点表示这个区间的所有点,然后边数就被优化为了至多${nlogn}$ ...

  10. CSS3关于-webkit-tap-highlight-color属性

    最近在写手机端,发现了一个问题,就是javascript点击元素时,在安卓手机上会出现半透明的蓝色背景,(经百度,在苹果手机上会出现半透明的灰色背景),后来通过百度找到了解决方案,就是利用CSS3的- ...