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.
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.
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.
#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的更多相关文章
-
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 ...
-
[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, ...
-
Codeforces.612E.Square Root of Permutation(构造)
题目链接 \(Description\) 给定一个\(n\)的排列\(p_i\),求一个排列\(q_i\),使得对于任意\(1\leq i\leq n\),\(q_{q_i}=p_i\).无解输出\( ...
-
Square Root of Permutation - CF612E
Description A permutation of length n is an array containing each integer from 1 to n exactly once. ...
-
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 ...
-
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 ...
-
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 ...
-
(Problem 57)Square root convergents
It is possible to show that the square root of two can be expressed as an infinite continued fractio ...
-
Square Root
Square RootWhen the square root functional configuration is selected, a simplified CORDIC algorithm ...
随机推荐
-
Entity Framework 教程——安装Entity Framework环境
安装Entity Framework环境 Entity Framework 5.0 API分布在两个地方,一个可在NuGet包管理器中找到,一个存在于.NET framework中..NET fram ...
-
深入理解JVM--JVM垃圾回收机制
Java语言出来之前,大家都在拼命的写C或者C++的程序,而此时存在一个很大的矛盾,C++等语言创建对象要不断的去开辟空间,不用的时候有需要不断的去释放空间,既要写构造函数,又要写析构函数,很多时候都 ...
-
宣布正式发布 Biz Talk Services、Azure Active Directory 和 Traffic Manager, 同时发布 Azure Active Directory 高级版预览
除经济优势之外,云计算还在可转化为竞争优势的应用程序开发方面提供了更大的灵活性.我们很高兴看到每天创建的新 Windows Azure 订阅超过 1000 个,更令人兴奋的是,有一半客户使用价值更高的 ...
-
keepalived(nat)+ftp+http
一. 环境要求需要2台LVS和n(n>=2)台RS操作系统 负载均衡模式 VIP NVIPRHEL7.4 NAT 193.168.141.30 192.168.102.165 LVS1 LVS2 ...
-
django项目部署上线
前言 完善的django项目上线,有很多种上线的方法,比如apache, uwsgi, nginx等.这里只介绍2种,一种是django自带的,另外一种则是nginx + uwsgi完成介绍.这里的系 ...
-
将对象xml序列化和反序列化
//将一个对象按XML序列化的方式写入到一个文件,使用的默认的UTF8编码格式 //o为要序列化的对象 //path保存文件的路径 public static object _lockObj=new ...
-
vue_实例_组件的生命周期
重绘重排 中重复出现的是 mounted(){...} beforeUpdate(){...} uptated(){...} 其他钩子函数只会出现一次 <!DOCTYPE html> & ...
-
windows共享文件夹权限设置
权限设置及更改,最好在右键属性里面, 在计算机管理,共享文件夹->共享里面修改,有时候会不生效. windows的凭据修改,在用户注销后才会生效.
-
Codeforces 786 B. Legacy
题目链接:http://codeforces.com/contest/786/problem/B 典型线段树优化连边,线段树上的每一个点表示这个区间的所有点,然后边数就被优化为了至多${nlogn}$ ...
-
CSS3关于-webkit-tap-highlight-color属性
最近在写手机端,发现了一个问题,就是javascript点击元素时,在安卓手机上会出现半透明的蓝色背景,(经百度,在苹果手机上会出现半透明的灰色背景),后来通过百度找到了解决方案,就是利用CSS3的- ...