根据那两个式子

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. dbcp基本配置和重连配置

    转载自:http://agapple.iteye.com/blog/772507 最近在看一些dbcp的相关内容,顺便做一下记录,免得自己给忘记了. 1. 引入dbcp (选择1.4) Java代码  ...

  2. Java节假日算法

    类:Vacation package test; import java.io.Serializable; import java.util.Date; public class Vacation i ...

  3. jquery和ajax,json写法的说明

    一: 在ajax中,如果没有用jquery,则如xmlHttpRequest.open("POST", "AjaxServlet", true); (1)如果用 ...

  4. bzoj1036: [ZJOI2008]树的统计Count link-cut-tree版

    题目传送门 这 算是link-cut-tree裸题啊 不过以前好像没有写过单点修改.............. #include<cstdio> #include<cstring&g ...

  5. hdu 1175 连连看 (深搜)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1175 题目大意:如果某两个相同的棋子,可以通过一条线连起来(这条线不能经过其它棋子)这样的两个棋子可以 ...

  6. Opencv 中透视变换函数对IplImage图像变换时出现的问题?

    最近一直在做视频稳像的项目,为了简化部分实现,使用了部分Opencv的函数,其中包括Opencv中对IplImage进行同时变换的函数cvWarpPerspective(src, dst,...) 发 ...

  7. linux C 中的volatile使用【转】

    转自:http://blog.csdn.net/sukhoi27smk/article/details/38020583 一个定义为volatile的变量是说这变量可能会被意想不到地改变,这样,编译器 ...

  8. BZOJ 1800

    1800: [Ahoi2009]fly 飞行棋 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1622  Solved: 1293[Submit][St ...

  9. webpack-dev-server坑

    转载人家滴 https://segmentfault.com/q/1010000007561947/a-1020000007596130 需要webpack开发服务(webpack-dev-serve ...

  10. WIN32通用控件之打开对话框获取文件路径

    OPENFILENAME ofn; char FileName[MAX_PATH]; memset(&ofn,,sizeof(OPENFILENAME)); memset(FileName,, ...