Codeforces.612E.Square Root of Permutation(构造)
\(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(构造)的更多相关文章
- 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, ...
- 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, ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces 417E Square Table(随机算法)
题目链接:Codeforces 417E Square Table 题目大意:给出n和m.要求给出一个矩阵,要求每一列每一行的元素的平方总和是一个平方数. 解题思路:构造.依照 a a a b a a ...
随机推荐
- java多线程机制中的Thread和Runnable()区别
1.java语言使用Thread类及其子类对象来表示线程,新建的一个线程声明周期中经历 新建.(声明一个线程,此时他已经有了相应的内存空间和其他资源),运行(线程创建之久就据用了运行的条件,一旦轮到使 ...
- python(9): GUI
实例1: 对输入的所有数字求和, 最后以. 结束输入 def fun(): list1=[] print('input a number:') while True: num=input() if n ...
- git无法pull仓库refusing to merge unrelated histories (拒绝合并不相关仓库)
原文地址 https://blog.csdn.net/lindexi_gd/article/details/52554159 本文讲的是把git在最新2.9.2,合并pull两个不同的项目,出现的问题 ...
- Java charAt() 方法
charAt() 方法用于返回指定索引处的字符.索引范围为从 0 到 length() - 1. 参数 index -- 字符的索引. 返回值 返回指定索引处的字符. 实例 public class ...
- python 给对象绑定属性和方法和__slots__的使用
# 以c语言为主是静态语言,运行之前先编译,在运行的过程中不允许编辑代码# 在运行的过程中,可以改变,可以添加属性,就是属于动态语言(python) # python动态的添加属性以及方法class ...
- org.apache.thrift.transport.TTransportException: Could not create ServerSocket on address 0.0.0.0/0.0.0.0:9083.
1.启动hive的过程中,[hadoop@slaver1 soft]$ hive --service metastore &错误如下所示: 原因:之前启动hive失败了,但是进程以及启动起来, ...
- 微信小程序 View:flex 布局
微信小程序 View 支持两种布局方式:Block 和 Flex 所有 View 默认都是 block 要使用 flex 布局的话需要显式的声明: display:flex; 下面就来介绍下微信小程序 ...
- cookie 详解
cookie概览 cookie是Web浏览器存储的少量命名数据,它与某个特定的网页或网站关联在一起.cookie是用来给Web浏览器提供内存,以便脚本和服务端程序可以在一个页面使用另一个页面的输入数据 ...
- Nessus忘记密码的解决
进入到Nessus安装目录下
- 一次BurpSuite无法抓https包定位