[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 ...
随机推荐
- Vue属性绑定
v-bind:属性动态绑定数据,简写: v-html:绑定html代码 {{}}:绑定数据,另一种v-text v-bind:class="{‘red’:isActive}" : ...
- Docker+Jenkins+Gogs 自动构建.Net Core
Docker+Jenkins+Gogs 自动构建.Net Core 引言 jenkins+gags 全部采用Docker安装,通过jenkins插件ssh调用外部Docker构建 主要实现功能: gi ...
- Win10每次开机总是自动弹出MSN网址导航如何取消
Win10每次开机总是自动弹出MSN网址导航如何取消 近来有用户在升级Win10系统后,每次开机总是会自动弹出MSN中文网的网址导航.如果不想要开机打开MSN网址导航,那么应该怎么设置来取消呢?对此, ...
- requests模块demo
import urllib.request import requests from requests.auth import HTTPBasicAuth from requests.auth imp ...
- Java框架之搭建环境maven报错
*maven Dependencies 中的地址通过POM 才会增加 正确是 1. m2所在位置 遇到的问题: java.lang.ClassNotFoundException: org.spring ...
- ADX3000二层的负载均衡设计问题
我的想法是 想在现有的局域网内部,利用ADX划分出一个新的局域网,模拟负载均衡. 现在有三台试验机器,拓扑图如下: 各个机器IP设置如下图: 我进行了如下的操作: 1 在组网配置当中,设置eth1_0 ...
- Node JS爬虫:爬取瀑布流网页高清图
原文链接:Node JS爬虫:爬取瀑布流网页高清图 静态为主的网页往往用get方法就能获取页面所有内容.动态网页即异步请求数据的网页则需要用浏览器加载完成后再进行抓取.本文介绍了如何连续爬取瀑布流网页 ...
- linux线程池
typedef struct task_node { void *arg; /* fun arg. */ void *(*fun) (void *); /* the real work of the ...
- C++的一种业务分发方案(另类的工厂模式)
在C++中,传统的业务分发.总要写一大串的switch-case,并且每次添加新业务时.都要在原有的switch-case里加一个分支,这就违反了设计模式中的开放封闭原则. 下面这样的方案,就全然去除 ...
- react 中文文档案例六 (表单)
class Reservation extends React.Component { constructor(props) { super(props); this.state = { isGoin ...