根据那两个式子

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. CentOS ninimal 安装后没有桌面-yellowcong

    昨天,安装Centos后,发现没有桌面,主要是没有安装桌面环境导致 的这个问题,我们需要做的第一步是,安装一个桌面(GNOME Desktop,命令:yum groupinstall -y " ...

  2. PropertiesConfiguration的用法

    PropertiesConfiguration是一个配置文件的加载工具类,封装了从配置文件里获取值并转化为基本数据类型的方法. 使用org.apache.commons.configuration2中 ...

  3. Death Note

    注:本文系作者原创,但可随意转载. ********************************************************************************** ...

  4. maven项目在eclipse tomcat正常运行

    转摘自:http://binary.duapp.com/2013/10/1577.html 配置下部署路径即可.

  5. Spring学习--集合属性

    Spring 中可以通过一组内置的 xml 标签(例如: <list> , <set> 或 <map>) 来配置集合属性. 配置java.util.Set 需要使用 ...

  6. MVC前台获取ViewData的数组中的值

    查了一上午资料,找到了一种比较有效的方法 后台控制器:public ActionResult Index() { List<string> colors = new List<str ...

  7. 【UVALive4685-Succession】树形DP

    http://acm.hust.edu.cn/vjudge/problem/14338 题意:给定一棵树,每个点有一个值,让你选择k个点,并且这k个点是连在一起的(从任意一个点出发,可以遍历完所有选择 ...

  8. 【BZOJ3450】Easy [期望DP]

    Easy Time Limit: 10 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description 某一天WJMZBMR在打osu~~ ...

  9. bzoj 5028: 小Z的加油店——带修改的区间gcd

    Description 小Z经营一家加油店.小Z加油的方式非常奇怪.他有一排瓶子,每个瓶子有一个容量vi.每次别人来加油,他会让 别人选连续一段的瓶子.他可以用这些瓶子装汽油,但他只有三种操作: 1. ...

  10. BZOJ1037 DP

    2013-11-15 21:51 原题传送门http://www.lydsy.com/JudgeOnline/problem.php?id=1037 拿到这道题想到了DP,后来发现三维无法确定的表示状 ...