[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 ...
随机推荐
- HTTP状态码了解
1xx - - 消息 2xx - - 成功 3xx - - 重定向 4xx - - 请求错误 5xx - - 服务器错误 1xx-信息提示 这些状态代码表示临时的响应.客户端在收到 ...
- UIPasteboard
1.UIPasteboard 简介 顾名思义,UIPasteboard 是剪切板功能,因为 iOS 的原生控件 UITextField.UITextView.UIWebView, 我们在使用时如果长按 ...
- v-touch使用方法以及在项目中遇到的问题
上篇博客中我记得还有一个坑没有解决好,在这篇博客中详细说明一下. 在 https://github.com/dreamITGirl/vuepageturn 我的这个代码库里,更新到2.1版本. 目前解 ...
- P3941 入阵曲
\(\color{#0066ff}{ 题目描述 }\) 小 F 很喜欢数学,但是到了高中以后数学总是考不好. 有一天,他在数学课上发起了呆:他想起了过去的一年.一年前,当他初识算法竞赛的 时候,觉得整 ...
- luogu2253 好一个一中腰鼓!
先说一个小trick,一开始我们把他赋值成是红.白相间的,查询就查询的是全红或全白即可. 然后就可以做啦 题解里面好像都是线段树 暴力的题解好像都被del了 貌似暴力交上去也过不了了 然后我想说 分块 ...
- 洛谷P1894 [USACO4.2]完美的牛栏The Perfect Stall
题目描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星期,农夫约翰随便地让奶牛们进入牛栏,但是问题很快地显露出来:每头奶牛都只愿意在 ...
- Vuex基础-Module
官方API地址:https://vuex.vuejs.org/zh/guide/modules.html 前面几节课写的user.js就称为一个module,这样做的原因是:由于使用单一状态树,应用的 ...
- ftp 添加用户及修改用户目录
添加用户 : useradd 用户名 -s /sbin/nologin //限定用户test不能telnet,只能ftp; usermod -s /sbin/bash 用户名 //用户恢复正常 ;该账 ...
- java的Spring学习1--spring引用及属性(setter,getter)注入
1.目录结构 项目->src->main->java->com.test.ppmoney 设置java文件夹为代码 在Project Structure 里的 Modules ...
- django文件配置
先是 staticfile 文件配制 STATTCFILE=(os.path.join(BASE_DIR,'static'),) 然后是数据库配置 : DATABASES = { 'default' ...