题目链接


\(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. Web Penetration Testing

    1.国外使用的一款在线工具,对web的信息收集很有帮助 地址http://archive.org  , WayBack Machine 主界面如下:对百度存档的历史信息进行查询. 2.IP地址归属信息 ...

  2. linux安装siege

    siege安装笔记 本文介绍centos和ubuntu安装方法 centos安装 下载: [root@ siege-4.0.4]# wget http://download.joedog.org/si ...

  3. Microsoft Visual Studio Community 2017 修改新建项目的默认位置

    IDE: Microsoft Visual Studio Community 2017 15.5.2 通过修改默认的设置,在下一次新建项目时,就可以节省一些不必要的操作. 菜单:工具 > 选项, ...

  4. 基础运算符补充,流程控制之if判断/while循环

    常量 常量即指不变的量.在python中没有一个专门 的语法代表常量,程序员约定俗成地用变量名全部被大写代表常量. AGE_OF_OLDBOY = 56 基础运算符补充 1.算术运算 加减乘除+ - ...

  5. 微信小程序 Video默认横屏

    wxml文件 <video id='myvideo' src='你的视频文件路径'> </video> js文件 onLoad: function (options) { va ...

  6. Django 基模板布局设置

    Django 基模板布局设置 基模板 定义基础模板一般分为三块,css部分,body部分,js部分 将基础统一的部分写在基础模板中 差异部分直接 引用 {% block css %}{% endblo ...

  7. [ZJOI2006]皇帝的烦恼

    题解: 具有单调性的题目还是要多想想二分答案 不二分答案暴力dp是n^3的 非常不优秀 二分答案之后就比较好做 mx[i],nx[i]表示最多/最少几个与a[1]相同 代码: #include < ...

  8. centos6.5上安装redis3.2.1遇见的坑

    解决方法: 安装gcc即可解决 解决方法: make MALLOC=libc 原因分析: 说关于分配器allocator, 如果有MALLOC  这个 环境变量, 会有用这个环境变量的 去建立Redi ...

  9. docker 安装配置

    1. 安装docker 环境是ubuntu 14.04 参照:  https://help.aliyun.com/document_detail/60742.html # step 1: 安装必要的一 ...

  10. Ubuntu16.04中nginx除80之外其他端口不能访问

    不废话, 大多数都以为是ufw防火墙的问题. 但我的是因iptables防火墙, 坑死我了. 查了好多也没查到怎么在Ubuntu关闭iptables, 索性直接卸载 apt-get remove ip ...