[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, 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
题目大意:
给你个置换p,然后做平方运算,得到置换q,题目给你q,问你能否找到p,要构造出来。
题解:
这道题要求倒推出一个置换,由于原置换p中的环不一定全是奇数环,所以平方之后有可能有环会裂开。
对于平方后的置换q中的奇数环,直接在里面推。偶数环就看是否有相同大小的偶数环与它合并。
//Never forget why you start
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
int n,m,a[],lm,ans[],q[];
struct node{
int sum;
vector<int>p;
friend bool operator < (const node a,const node b){
return a.sum<b.sum;
}
}s[];
int vis[],cnt;
void dfs(int r){
vis[r]=;
cnt++;
s[lm].p.push_back(r);
if(vis[a[r]])return;
else dfs(a[r]);
}
int main(){
int i,j;
scanf("%d",&n);
for(i=;i<=n;i++)scanf("%d",&a[i]);
for(i=;i<=n;i++)
if(!vis[i]){
cnt=;
lm++;
dfs(i);
s[lm].sum=cnt;
}
sort(s+,s+lm+);
bool flag=;
for(i=;i<=lm;i++){
if(s[i].sum&)continue;
else{
if(s[i+].sum==s[i].sum){i++;continue;}
else {flag=;break;}
}
}
if(flag){printf("-1\n");return ;}
for(i=;i<=lm;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+];
ans[q[s[i].sum-]]=q[];
}
else{
int k=i+;
for(j=;j<s[i].sum;j++){
ans[s[i].p[j]]=s[k].p[j];
ans[s[k].p[j]]=s[i].p[(j+)%s[i].sum];
}
i++;
}
}
for(i=;i<=n;i++)
printf("%d ",ans[i]);
return ;
}
[CF 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 ...
- codefroces 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. ...
- CF612E Square Root of Permutation
题目分析 我们首先模拟一下题意 假设有一个 \(q _1\) \(p\) \(a_1\) \(a_x\) \(a_{a_1}\) \(a_{a_x}\) \(q\) \(x\) \(a_1\) \(a ...
- 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 ...
随机推荐
- scala lambda 表达式 & spark RDD函数操作
形式:(参数)=> 表达式 [ 一种匿名函数 ] 例1:map(x => x._2) 解:x=输入参数,“=>” 右边是表达式(处理参数): x._2 : x变为(**,x,**. ...
- UINavigationController + UIScrollView组合,视图尺寸的设置探秘(三)
还是在苹果的 View Controller Catalog for iOS 文章中找到答案.文中提到了两点: 1.If the navigation bar or toolbar are visib ...
- slice()、substring()、substr()的区别用法
在js中字符截取函数有常用的三个slice().substring().substr()了,下面我来给大家介绍slice().substring().substr()函数在字符截取时的一些用法与区别吧 ...
- ECMAScript 6 一些有意思的特性
主要参考了下面两篇博文,对ES6的新特性做一些笔记,加深印象 ES6新特性概览 - 刘哇勇 - 博客园 es6快速入门 - _marven - 博客园 *号函数 迭代函数生成器 我能想到的生成器使用场 ...
- python解决最小二乘法的相关问题笔记。
计算最小二乘法求目标函数的系数,部分代码来源于张若愚老师的<Python科学计算> 题目 某检测装备输入数据: x:0.9, 2.5, 3.3, 4.5, 5.7, 6.9 g:1.1, ...
- luogu2723 丑数
提供一种单调队列做法(非正解) 显然每一个丑数能够由一个质数乘以另一个丑数得到 所以我们开k个单调递增队列,每次从这些队列顶部找到一个最小的元素把他捞出来,然后枚举所有质数,用这个元素乘以质数,放入相 ...
- J.U.C-其他组件
J.U.C-其他组件 FutureTask 介绍Callable时我们知道它可以有返回值,返回值通过Future进行封装.FutrueTask实现了RunnableFuture接口,该接口继承了R ...
- linux1--常用命令
1.目录结构 2./etc:系统配置文件存放地 比如你的wsgi的配置文件,nginx 3./usr 应用程序存放地 比如你的jdk应用程序存放地 4./root 系统管理员root的家目录 4.1像 ...
- C语言抽象数据类型ADT
根据编程的问题匹配合适的数据类型.数据项连接构成了链表,定义了一个结构代表单独的项.设计了一些方法把一系列结构构成一个链表.本质上,我们使用C语言的功能设计了一种符合程序要求的新的数据类型.但是上述的 ...
- H - 逆序数(树状数组)
在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序.一个排列中逆序的总数就称为这个排列的逆序数. 如2 4 3 1中,2 1,4 3,4 1,3 1是逆序 ...