题目链接


\(Description\)

给定一个\(n\)的排列\(p_i\),求一个排列\(q_i\),使得对于任意\(1\leq i\leq n\),\(q_{q_i}=p_i\)。无解输出\(-1\)。

\(1\leq n\leq10^6\)。

\(Solution\)

对排列\(q_i\)我们建一张图,边为\(i\to q_i\)。显然这张图是由几个环构成。

发现对于\(q_{q_i}\)的图,原来\(q_i\)中的奇环它们还是类似的一个奇环,原来的偶环会分裂成两个大小相等的偶环。

所以对\(p_i\)建图,找出里面的环,是奇环就把相邻点间隔为\(2\)地插入到环里,是偶环就找到和它一样大的一个合并,找不到就无解。这样就可以得到\(q_i\)的图了。(每个偶环只能合并一次→_→)

复杂度\(O(n)\)。


//249ms	21300KB
#include <cstdio>
#include <cctype>
#include <algorithm>
//#define gc() getchar()
#define MAXIN 1000000
#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
typedef long long LL;
const int N=1e6+5; int nxt[N],q[N],tmp[N],tmp2[N],id[N];
bool vis[N];
char IN[MAXIN],*SS=IN,*TT=IN; inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-48,c=gc());
return now;
} int main()
{
const int n=read();
for(int i=1; i<=n; ++i) nxt[i]=read();
for(int s=1; s<=n; ++s)
if(!vis[s])
{
int cnt=0,x=s;
do
{
tmp[++cnt]=x, vis[x]=1, x=nxt[x];
}while(x!=s);
if(cnt&1)
{
for(int i=1,now=0; i<=cnt; ++i,(now+=2)>=cnt&&(now-=cnt)) tmp2[now]=tmp[i];
for(int i=0; i<cnt; ++i) q[tmp2[i]]=tmp2[i+1];
q[tmp2[cnt-1]]=tmp2[0];
}
else
{
if(!id[cnt]) id[cnt]=s;
else
{
int y=id[cnt],x=y,t=0;
do
{
tmp2[++t]=x, x=nxt[x];
}while(x!=y);
for(int t1=1,t2=1,las=tmp[1],i=t<<1; i; --i)
las=q[las]=i&1?tmp[++t1]:tmp2[t2++];
q[tmp2[t]]=tmp[1], id[t]=0;
}
}
}
for(int i=2; i<=n; i+=2) if(id[i]) return puts("-1"),0;
for(int i=1; i<=n; ++i) printf("%d ",q[i]); return 0;
}

Codeforces.612E.Square Root of Permutation(构造)的更多相关文章

  1. 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 ...

  2. [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, ...

  3. 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, ...

  4. Square Root of Permutation - CF612E

    Description A permutation of length n is an array containing each integer from 1 to n exactly once. ...

  5. CF612E Square Root of Permutation

    题目分析 我们首先模拟一下题意 假设有一个 \(q _1\) \(p\) \(a_1\) \(a_x\) \(a_{a_1}\) \(a_{a_x}\) \(q\) \(x\) \(a_1\) \(a ...

  6. 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 ...

  7. 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 ...

  8. Codeforces Round #372 (Div. 1) A. Plus and Square Root 数学题

    A. Plus and Square Root 题目连接: http://codeforces.com/contest/715/problem/A Description ZS the Coder i ...

  9. Codeforces 417E Square Table(随机算法)

    题目链接:Codeforces 417E Square Table 题目大意:给出n和m.要求给出一个矩阵,要求每一列每一行的元素的平方总和是一个平方数. 解题思路:构造.依照 a a a b a a ...

随机推荐

  1. kali linux 信息收集(Kismet)

    1.kismet工具,是一个无线扫描工具,该工具通过测量周围的无线信号,可以扫描到周围附近所用可用的Ap,以及信道等信息.同时还可以捕获网络中的数据包到一个文件中.这样可以方便分析数据包.下面我将详细 ...

  2. Visual Studio UML类图

    1.unified Modeling Language(UML)称为同一建模语言或者标准建语言, 用例图:对系统的使用方式的分类.类图:显示类和他们的相互关系.  对象图:只显示对象及他们的相互关系. ...

  3. Python老男孩

    1.可以自己编写模块,但注意:如果想要调用该模块,需要将该模块放到site-packages目录下,或将该模块放在执行程序的路径下. 2.pyc文件是什么? 集合: set 集合可以去重:做交集.并集 ...

  4. easyui之自定义字体图标(鼠标覆盖时切换颜色)

    项目要求是自定义字体图标,使用easyui框架结构,众所周知easyui强功能弱样式,字体图标其实就是一张图片.要达到切换图标颜色的效果,要么就是有两套图,使用js控制.但是我这个人比较懒,不喜欢做复 ...

  5. java函数式编程之Supplier

    原创 2016年12月25日 10:02:52 标签: 函数式编程 / java 3250 描述:Supplier< T>接口没有入参,返回一个T类型的对象,类似工厂方法. 源码: pub ...

  6. 论文阅读笔记七:Structure Inference Network:Object Detection Using Scene-Level Context and Instance-Level Relationships(CVPR2018)

    结构推理网络:基于场景级与实例级目标检测 原文链接:https://arxiv.org/abs/1807.00119 代码链接:https://github.com/choasup/SIN Yong ...

  7. mysql集群7.4.1

    一:mysql集群原理: 1 mysql集群分为三个节点: 1.1 控制节点:本身不提供服务只是控制整个集群的开启与关闭 1.2 数据节点:真正提供数据库的存储,并和其他数据节点关联用 1.3 sql ...

  8. constructor与prototype

    在学习JS的面向对象过程中,一直对constructor与prototype感到很迷惑,看了一些博客与书籍,觉得自己弄明白了,现在记录如下: 我们都知道,在JS中有一个function的东西.一般人们 ...

  9. [转] Javascript 原型链

    1. 类 在C或者Java里,int a;定义了一个int类型的变量a.其中int是类型的名字,a是具体的变量. Javascript 模仿自 Java, 有一部分面向对象编程的部分.在面向对象的编程 ...

  10. 分布式一致性算法——paxos

    一.什么是paxos算法 Paxos 算法是分布式一致性算法用来解决一个分布式系统如何就某个值(决议)达成一致的问题. 人们在理解paxos算法是会遇到一些困境,那么接下来,我们带着以下几个问题来学习 ...