Square Root of permutation-cf612e

Source: Internet
Author: User
Tags square root

Description

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[ C14>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 are about the inverse operation:given the permutation p You task are to find such permutation Q that q2 = p. If There is several such Q find any of them.

Input

The first line contains integer n (1≤ n ≤106)-the number of elements in Permutati On P.

The second line contains n distinct integers p1, P2, ..., pn (1≤ pin)-the elements of permutation p.

Output

If there is no permutation q such that q2 = P Print the number "-1". /c23>

If The answer exists print it. The only line should contain n different integers qi (1≤ qi< /c9>≤ n)-the elements of the permutation q. If There is several solutions print any of them.

Sample Input

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

Simple test Instructions

A permutation group p with a size of n is given, and a permutation group Q is obtained, which makes the q^2=p

Nonsense

First we observe how q^2 is calculated, the permutation group can be regarded as a ring, each point I to a[i] even a side is that figure

Q^2 actually is to turn the side of I into a[a[i], that is, two steps on the ring, then q the original ring has changed

1. Assuming the original is an odd ring, then it is an odd ring, but the order has changed

2. If the original is an even ring, then it will be split into two half-size rings

We'll see P again.

The odd ring on p may be the original odd ring, or it may be an even ring apart

The even ring on p can only be disassembled by the original even ring.

For odd rings We can construct the original odd ring by alternately inserting the back part of the ring with the first half (breaking the ring first).

For even rings we can only find an even ring of the same size alternately, that is, two even rings of the same size are merged, and if we cannot find even rings of the same size, then we know that there is no such q which makes q^2=p

1#include <cstdio>2#include <algorithm>3 using namespacestd;4 5 Const intmaxn=1000100;6 7 intN,TOT,A[MAXN],B[MAXN],S[MAXN],L[MAXN],CIR[MAXN];8 BOOLFLAG[MAXN];9 Ten BOOLcomintAintb) { One     returnl[a]<L[b]; A } -  - intMain () { thescanf"%d",&n); -     inti,j,k; -      for(i=1; i<=n;i++) scanf ("%d",&a[i]); -      for(i=1; i<=n;i++) +     if(!Flag[i]) { -cir[++tot]=i; +flag[i]=true; A++L[i]; atj=A[i]; -          while(!Flag[j]) { -flag[j]=true; -++L[i]; -j=A[j]; -         } in     } -Sort (cir+1, cir+1+tot,com); to     intx=0; +     BOOLf=true; -      for(i=1; i<=tot;i++) the     if((l[cir[i]]&1)==0){ *         if(x==0) x=L[cir[i]]; $         ElsePanax Notoginseng         if(X==l[cir[i]]) x=0; -         Elsef=false; the     } +     if(x!=0) f=false; A     if(f==false) printf ("-1"); the     Else{ +          for(i=1; i<=tot;i++){ -             if((l[cir[i]]&1)==1){ $j=Cir[i]; $k=0; -                  while(Flag[j]) { -s[k]=J; theflag[j]=false; -k= (k +2)%L[cir[i]];Wuyij=A[j]; the                 } -                  for(j=0; j<l[cir[i]]-1; j + +) b[s[j]]=s[j+1]; Wub[s[l[cir[i]]-1]]=s[0]; -             } About             Else{ $j=Cir[i]; -k=cir[i+1]; -                  while(Flag[j]) { -b[j]=K; Ab[k]=A[j]; +flag[j]=false; theflag[k]=false; -j=A[j]; $k=A[k]; the                 } the++i; the             } the         } -          for(i=1; i<=n;i++) printf ("%d", B[i]); in     } the     return 0; the}
AC Code

Square Root of permutation-cf612e

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.