根据那两个式子

g(h(x))=x

h(g(x))=f(x)

可以推出来两个新的式子

g(f(x))=g(x)

h(x)=f(h(x))

于是,我们先找到f(x)的所有不动点,有几个不动点,m就是多少,将它们依次赋值给h(1),h(2),...,h(m),对应的g(h(i))赋值成1,2,..,m

然后如果对于某个i,g(f(i))不存在的话,则无解,否则有解,将g和h依次输出即可。

#include<cstdio>
using namespace std;
int n,f[100010],g[100010],h[1000010],m;
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;++i)
scanf("%d",&f[i]);
for(int i=1;i<=n;++i)
if(f[i]==i)
{
h[++m]=i;
g[i]=m;
}
if(!m)
{
puts("-1");
return 0;
}
for(int i=1;i<=n;++i)
if(!g[f[i]])
{
puts("-1");
return 0;
}
printf("%d\n",m);
for(int i=1;i<n;++i)
printf("%d ",g[f[i]]);
printf("%d\n",g[f[n]]);
for(int i=1;i<m;++i)
printf("%d ",h[i]);
printf("%d\n",h[m]);
return 0;
}

【构造】Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) D. Artsem and Saunders的更多相关文章

  1. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) D. Artsem and Saunders 数学 构造

    D. Artsem and Saunders 题目连接: http://codeforces.com/contest/765/problem/D Description Artsem has a fr ...

  2. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) A B C D 水 模拟 构造

    A. Neverending competitions time limit per test 2 seconds memory limit per test 512 megabytes input ...

  3. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) D. Artsem and Saunders

    地址:http://codeforces.com/contest/765/problem/D 题目: D. Artsem and Saunders time limit per test 2 seco ...

  4. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) F. Souvenirs 线段树套set

    F. Souvenirs 题目连接: http://codeforces.com/contest/765/problem/F Description Artsem is on vacation and ...

  5. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) E. Tree Folding 拓扑排序

    E. Tree Folding 题目连接: http://codeforces.com/contest/765/problem/E Description Vanya wants to minimiz ...

  6. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) C. Table Tennis Game 2 水题

    C. Table Tennis Game 2 题目连接: http://codeforces.com/contest/765/problem/C Description Misha and Vanya ...

  7. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) B. Code obfuscation 水题

    B. Code obfuscation 题目连接: http://codeforces.com/contest/765/problem/B Description Kostya likes Codef ...

  8. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) A. Neverending competitions 水题

    A. Neverending competitions 题目连接: http://codeforces.com/contest/765/problem/A Description There are ...

  9. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) E. Tree Folding

    地址:http://codeforces.com/contest/765/problem/E 题目: E. Tree Folding time limit per test 2 seconds mem ...

随机推荐

  1. springboot中 后端跨域的实现配置

    在springboot的启动文件中,添加此内容,可以允许跨域

  2. bzoj 5094 [Lydsy1711月赛]硬盘检测 概率dp

    [Lydsy1711月赛]硬盘检测 Time Limit: 1 Sec  Memory Limit: 256 MBSubmit: 273  Solved: 75[Submit][Status][Dis ...

  3. a标签的download属性简介

    最近在工作中需要一个前端直接下载静态文件的需求,之前有粗略的了解过a标签的download属性,通过download和href属性可以实现文件的下载. 简介 HTML <a> 元素 (或锚 ...

  4. Android 网络编程--上传文件及相应的参数到服务器

    之前一直在做SiteCheck的项目,所用到的知识大部分都涉及到网络编程方面,所以现在有时间先把它的使用方法及一些注意事项记录下来.在这里我用两种例子让大家了解它的使用方法: (1)上传图片及相应参数 ...

  5. java中的构造块、静态块等说明

    一:这篇博客写的时候我在学校已经一个星期了,为什么又会想到写这le,因为这几天又在重新学下有关spring.myBatis的知识,其中在实例化sessionFactory的时候用到了静态块,虽然在学习 ...

  6. 图论的复习/(ㄒoㄒ)/

    图论基本概念 完全图: 每对顶点之间有边并且只有唯一的一条边. 强连通分量:有向图中任意2点都联通的最大子图. 图的储存 邻接矩阵:也就是一个二维数组,a[i][j]的值代表是否相连. 适用范围: 1 ...

  7. ShadowBroker公开的SMB远程命令执行漏洞修复

    有人不知道如何获得MS对应的补丁KB编号,可以看这篇文章了~ 漏洞编号为ms17-010,如何查看对应MS号的补丁已经安装: 下载微软官方的补丁信息列表(Microsoft Security Bull ...

  8. [vue-router] Failed to resolve async component default: Error: Loading chunk 0 failed.

    在整合laravel5.4 和vue2.1的时候遇到一个奇怪的问题 Uncaught SyntaxError: Unexpected token < Error: Loading chunk 0 ...

  9. django原生sql

    参考:http://www.jb51.net/article/128257.htm coding=utf-8 from django.core.paginator import Paginator d ...

  10. 进一步认识golang中的并发

    如果你成天与编程为伍,那么并发这个名词对你而言一定特别耳熟.需要并发的场景太多了,例如一个聊天程序,如果你想让这个聊天程序能够同时接收信息和发送信息,就一定会用到并发,无论那是什么样的并发. 并发的意 ...