CF612E Square Root of Permutation
题目分析
我们首先模拟一下题意
假设有一个 \(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的更多相关文章
- 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 ...
- Square Root of Permutation - CF612E
Description A permutation of length n is an array containing each integer from 1 to n exactly once. ...
- 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, ...
- [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\).无解输出\( ...
- 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 ...
随机推荐
- vue-cli4脚手架搭建三
组件传值 <script> import LunBo from "./LunBo"; export default { name: 'Home', components ...
- oracle(数据文件)
--创建数据文件 create tablespace--创建表空间同时创建数据文件 create temporary tablespace --创建临时表空间的同时创建临时数据文件 alter tab ...
- logstash 正则表达式
正则表达式 3. 使用给定好的符号去表示某个含义 4. 例如.代表任意字符 5. 正则符号当普通符号使用需要加反斜杠 正则的发展 6. 普通正则表达式 7. 扩展正则表达式 普通正则表达式 . 任意一 ...
- 关于synchronize与lock的区别
参考文献:https://www.cnblogs.com/cloudblogs/p/6440160.html 一.synchronize修饰不同代码都是锁住了什么? 大家都知道synchronize可 ...
- 筛选Table.SelectRows-日期与时间(Power Query 之 M 语言)
数据源: 包含日期与时间的任意数据 目标: 对日期与时间进行筛选 M公式: = Table.SelectRows( 表,筛选条件) 筛选条件: 等于:each [日期列] = #date(年,月,日) ...
- 工作组规划器(Project)
<Project2016 企业项目管理实践>张会斌 董方好 编著 好像前面每分配一次任务,都要打开一个对话框,有木有简单粗暴点的法子啊? 必须有啊! 视图里有一种[工作组规划器],想要粗暴 ...
- CF760A Petr and a calendar 题解
Content 输入两个数 \(m,d\),请输出 \(2017\) 年 \(m\) 月的日历[其中第一天是星期 \(d\)(如果 \(d=7\) 就是星期天)]需要印的列数. 格式见题目所述. 数据 ...
- CentOS系统 python3+python2 & Ipython安装
https://www.cnblogs.com/albertrui/p/8093384.html 一.安装依赖环境 输入命令: yum -y install zlib-devel bzip2-deve ...
- Error: Not found: 'package:json_annotation/json_annotation.dart'
问题原因 json_annotation版本不对 修改json_annotation版本号 当前可用版本号 json_annotation: ^2.2.0
- springboot发送邮件(含附件)
引入maven <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...