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 ...
随机推荐
- VScode 使用 CMake 入门
参考 CMake 入门实战 在 linux 平台下使用 CMake 生成 Makefile 并编译的流程如下: 编写 CMake 配置文件 CMakeLists.txt . 执行命令 cmake PA ...
- 3.0 go mod之远程仓库搭建-代码示例
注意事项 所谓的远程仓库指的是github,个人首次使用go mod在其他云仓库上尝试,并未成功,这浪费了我近2小时的时间: 如果你是初次尝试,那么除了github的地址换一下之外,其他的都按照示例操 ...
- VueAPI 2 (生命周期钩子函数)
所有的生命周期钩子自动绑定 this 上下文到实例中,因此你可以访问数据,对属性和方法进行运算.这意味着你不能使用箭头函数来定义一个生命周期方法. beforeCreate 在实例初始化之后,此时还不 ...
- 微软开源的Web测试和自动化神器 Playwright
Playwright 是微软开源的一个用于 Web 测试和自动化的框架, 提供了可靠的端到端测试, 功能非常强大, 可以在测试, 爬虫,自动化场景中使用. 跨浏览器 Playwright 支持所有现代 ...
- input type="file"多图片上传
单个的input type="file"表单也是可以实现多图片上传的 代码如下: <form action="manypic.php" method=&q ...
- Markdown 语法粗学
Markdown 语法粗学 Typora下载 Typora官网 下拉点击右上角 选择下载即可 里面选择自己想要的系统下载即可 如果下载缓慢,推荐使用各自的下载工具或者使用软件管家等 亲测迅雷速度尚可 ...
- MySQL 创建定时任务 详解
自 MySQL5.1.6起,增加了一个非常有特色的功能–事件调度器(Event Scheduler),可以用做定时执行某些特定任务,来取代原先只能由操作系统的计划任务来执行的工作.事件调度器有时也可称 ...
- Nacos——注册中心
目录 1.什么是nacos 2.使用--依赖+配置文件 3.Nacos服务分级存储模型 4.服务跨集群调用问题 5.服务集群属性--配置服务集群 6. Nacos-NacosRule负载均衡 7.根据 ...
- java 图形化小工具Abstract Window Toolit 菜单项
AWT 中的菜单由如下几个类组合而成 MenuBar: 菜单条,菜单的容器. Menu: 菜单组件,菜单项的容器,它也是Menultem的子类,所以可作为菜单项使用. PopupMenu: 上下文菜单 ...
- response.setHeader("xxx","大侠")如果赋值中文,那么将不会在页面出值,
response.setHeader("xxx","大侠")如果赋值中文,那么将不会在页面出值,而非中文就可以在页面出值