题目分析

我们首先模拟一下题意

假设有一个 \(q _1\)

\(p\) \(a_1\) \(a_x\) \(a_{a_1}\) \(a_{a_x}\)
\(q\) \(x\) \(a_1\) \(a_x\) \(a_{a_1}\)
\(pos\) 1 \(x\) \(a_1\) \(a_x\)

对这个表格分析,发现,当前节点的后继为前驱的对应

如果我们对 \(i \to p_i\) 建边,模拟一下,发现

偶环是会有两个环组成,而奇环会形成一个类似于五角星的图形

于是,我们可以找出所有环,对于奇环模拟即可,而偶环,拆分后再访问

#include<bits/stdc++.h>
using namespace std;
int n;
int a[1000005];
int vis[1000005];
struct node{
vector<int>vs;
}scc[1000005];
int cnt_block;
void dfs(int x,int key)
{
if(vis[x])
{
return;
}
vis[x]=1;
scc[key].vs.push_back(x);
dfs(a[x],key);
}
bool cmp(node x,node y)
{
return x.vs.size()<y.vs.size();
}
vector<int>temp;
int ans[1000005];
int pcd[1000005];
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(int i=1;i<=n;i++)
{
if(!vis[i])
{
++cnt_block;
dfs(i,cnt_block);
}
}
//
sort(scc+1,scc+1+cnt_block,cmp);
for(int i=1;i<=cnt_block;i++)
{
// printf("%d\n",scc[i].vs.size());
temp.clear();
if(scc[i].vs.size()&1)
{
int mid = (scc[i].vs.size() + 1)/2;
for (int j =1;j <= mid; j++)
{
pcd[(j*2)-1] = scc[i].vs[j-1];
} for (int j = mid + 1;j <= scc[i].vs.size(); j++)
{
pcd[(j - mid)*2] = scc[i].vs[j-1];
}
for(int j=1;j<=scc[i].vs.size();j++)
{
ans[pcd[j]]=pcd[j+1];
}
ans[pcd[scc[i].vs.size()]]=pcd[1];
}
else
{
if(i==cnt_block)
{
printf("-1");
return 0;
}
if(scc[i].vs.size()^scc[i+1].vs.size())
{
printf("-1");
return 0;
}
for(int j=0;j<scc[i].vs.size();j++)
{
temp.push_back(scc[i].vs[j]);
temp.push_back(scc[i+1].vs[j]);
}
for(int j=0;j<temp.size()-1;j++)
{
ans[temp[j]]=temp[j+1];
} ans[temp[temp.size()-1]]=temp[0];
i++;
}
}
for(int i=1;i<=n;i++)
{
printf("%d ",ans[i]);
}
}

CF612E 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. Square Root of Permutation - CF612E

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

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

  5. Codeforces.612E.Square Root of Permutation(构造)

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

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

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

  9. (Problem 57)Square root convergents

    It is possible to show that the square root of two can be expressed as an infinite continued fractio ...

随机推荐

  1. js将数字转为千分位/清除千分位

    /** * 千分位格式化数字 * * @param s * 传入需要转换的数字 * @returns {String} */ function formatNumber(s) { if (!isNaN ...

  2. layui 弹窗中 分页展示table

    1. 需求:点击查看更多,展示该类别 所有数据,并分页 2. 参考文档: (1)https://www.jianshu.com/p/40da11ebae66 (2) https://blog.csdn ...

  3. MVC+Servlet+mysql+jsp读取数据库信息

    首先有以下几个包: 1.controller 控制层,对用户的请求进行响应 2.dao 数据层接口标准 3.daoimpl 数据层实现层 4.model 实体类层 5.service 业务层接口标准 ...

  4. java 多线程的状态迁移 常用线程方法分析

    一.线程的各个状态 图中的线程状态(Thread.Stat 中定义的Enum 名)NEW.RUNNABLE .TERMINATED.WAITING.TIMED_WAITING 和BLOCKED 都能够 ...

  5. Nginx编译添加新模块

    目录 一.简介与思路 一.简介与思路 当前适用于nginx已经在安装过了,如果没安装过,直接在编译时候添加模块即可. Nginx主要程序就是nginx这个二进制脚本,只要在编译一个nginx脚本替换掉 ...

  6. python模块(三)

    hashilib模块 hashilib模块的主要作用是加密,可以将明文数据通过一系列算法转化为秘闻数据. 目的是为了数据的安全. 加密算法包括md系列,sha系列,base系列,hmac系列. 基本使 ...

  7. SpringCloud微服务实战——搭建企业级开发框架(三十三):整合Skywalking实现链路追踪

      Skywalking是由国内开源爱好者吴晟(原OneAPM工程师)开源并提交到Apache孵化器的产品,它同时吸收了Zipkin/Pinpoint/CAT的设计思路,支持非侵入式埋点.是一款基于分 ...

  8. [BUUCTF]PWN——wustctf2020_closed

    wustctf2020_closed 附件 步骤: 例行检查,64位程序,开启了nx保护 本地试运行一下看看大概的情况 64位ida载入,首先是检索程序里的字符串,找到了后门 main函数里的关键函数 ...

  9. [BUUCFT]PWN——pwn2_sctf_2016

    pwn2_sctf_2016[整数溢出+泄露libc] 题目附件 步骤: 例行检查,32位,开启了nx保护 试运行一下程序,看看大概的执行情况 32位ida载入,shift+f12检索程序里的字符串, ...

  10. 记录一次成功CICD完整亲身实践从此踏进入Devops大门

    Devops概念 DevOps 强调通过一系列手段来实现既快又稳的工作流程,使每个想法(比如一个新的软件功能,一个功能增强请求或者一个 bug 修复)在从开发到生产环境部署的整个流程中,都能不断地为用 ...