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, 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. ...
- 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 ...
随机推荐
- Java连接mysql——Establishing SSL connection without server's identity verification is not recommended.
Establishing SSL connection without server's identity verification is not recommended. 出现这个错误的原因是因为m ...
- 四则运算----C++版
一.设计思想 因java中已做过,就是简单的将java中的语句调换为C++的语句. 二.代码 #include<iostream.h> #include<Stdlib.h> v ...
- Scala 集合入门
1. 数组 1.1 定长数组 scala.Array 是定长的可变的索引型集合, JVM 中, Scala 的 Array 是以 Java 数组方式实现. String 对应 java.lang.St ...
- java 零基础搭建dubbo运行环境
一:简介 以前做项目时,分布式环境都是其它同事在搭建,自己也没参与分布式环境搭建,只负责开发,由于近段时间工作重心转到android,java后台有一段时间没有接触了,刚好这几天有空,决定自己动 ...
- php最新版本配置mysqli
从官网上下载php后(我下的是php7.2.3版本),本想做个mysql的连接,但是无论怎么配置mysqli扩展,发现mysqli都没法用. 从百度上搜的那些方法都没法用,发现都是一些在php.ini ...
- JS 实现MVC的写法
案例:当select 下拉选择框值变化时,显示其值(不是文本) 常规写法 <h3>JavaScript no MVC</h3> <div> <selec ...
- [52ABP实战课程系列]Docker&Ubuntu从入门到实战开课啦~
任何的课程都逃不开理论的支持 久等了各位,在Asp.NET Core2.0 项目实战入门视频课程结束后,根据发起的投票信息.Docker 排在首位.按照结果,我们开始进行Docker视频课程的录制. ...
- python-3.x-基本数据类型
当前学习版本为: python-3.6-4 代码: """整型 NUMBER""" a = 2 ** 5 b = a + 4 c = a / ...
- bugfree,CDbConnection 无法开启数据库连线: SQLSTATE[HY000] [2003] Can't connect to MySQL server on '192.168.0.99' (4)
安装bugfree后,访问报错:CDbConnection 无法开启数据库连线: SQLSTATE[HY000] [2003] Can't connect to MySQL server on '19 ...
- float和position
float float是欺骗父元素,让其父元素误以为其高度塌陷了,但float元素本身仍处于文档流中,文字会环绕着float元素,不会被遮蔽. absolute 但absolute其实已经不能算是欺骗 ...