Codeforces Round #383 (Div. 2)C. Arpa's loud Owf and Mehrdad's evil plan

时间:2022-09-17 14:23:47
C. Arpa's loud Owf and Mehrdad's evil plan
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

As you have noticed, there are lovely girls in Arpa’s land.

People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi.

Codeforces Round #383 (Div. 2)C. Arpa's loud Owf and Mehrdad's evil plan

Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows.

The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time.

Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t ≥ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible.

Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i).

Input

The first line of input contains integer n (1 ≤ n ≤ 100) — the number of people in Arpa's land.

The second line contains n integers, i-th of them is crushi (1 ≤ crushi ≤ n) — the number of i-th person's crush.

Output

If there is no t satisfying the condition, print -1. Otherwise print such smallest t.

Examples
Input
4
2 3 1 4
Output
3
Input
4
4 4 4 4
Output
-1
Input
4
2 1 4 3
Output
1
Note

In the first sample suppose t = 3.

If the first person starts some round:

The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1.

The process is similar for the second and the third person.

If the fourth person starts some round:

The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4.

In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa.

/*
并查集瞎搞,唯一一组爆int的数据:
如果环的长度为:2 3 5 7 11 13 17 19 23,那么最小公倍数就是363322674就会爆int
*/
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int bin[];
int vis[];
int vis2[];
int n,a[];
ll cur=;
ll gcd(ll a,ll b)
{
return a==?b:gcd(b%a,a);
}
ll lcm(ll a,ll b)
{
return a*b/gcd(a,b);
}
int findx(int x,int start)//查找
{
while(bin[x]!=x)
{
x=bin[x];
if(x==start)
break;
}
return x==start;
}
void op(int x,int start)//查找
{
vis[x]=;
int flag=;//表示是不是第一次
while(bin[x]!=x)
{ vis[x]=;
x=bin[x];
if(x==start)
{
if(cur!=)
cur++;
break;
}
cur++;
}
if(cur==)
cur=;
}
int main()
{
//freopen("C:\\Users\\acer\\Desktop\\in.txt","r",stdin);
scanf("%d",&n);
for(int i=;i<=n;i++)
{
vis[i]=;
vis2[i]=;
bin[i]=i;
}
int ans=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
if(vis2[a[i]]==)
ans++;
bin[i]=a[i];
vis2[a[i]]=;
}
if(ans!=n)
{
puts("-1");
return ;
}
int f=;//是不是每个节点都有环
long long maxn=;
for(int i=;i<=n;i++)
{
cur=;
//cout<<"findx(i,i)="<<findx(i,i)<<endl;
if(vis[i]==)//没有访问过
{
if(findx(i,i))//有环
{
op(i,i);//去更新操作
//cout<<"cur="<<cur<<endl;
if(cur%==)
cur/=;
maxn=maxn/__gcd(maxn,cur)*cur;
}
else
{
f=;
break;
}
}
}
if(f)
puts("-1");
else
printf("%lld\n",maxn);
return ;
}

Codeforces Round #383 (Div. 2)C. Arpa's loud Owf and Mehrdad's evil plan的更多相关文章

  1. Codeforces Round &num;383 &lpar;Div&period; 2&rpar; C&period; Arpa&&num;39&semi;s loud Owf and Mehrdad&&num;39&semi;s evil plan —— DFS找环

    题目链接:http://codeforces.com/contest/742/problem/C C. Arpa's loud Owf and Mehrdad's evil plan time lim ...

  2. Codeforces Round &num;383 &lpar;Div&period; 2&rpar; C&period; Arpa&&num;39&semi;s loud Owf and Mehrdad&&num;39&semi;s evil plan(dfs&plus;数学思想)

    题目链接:http://codeforces.com/contest/742/problem/C 题意:题目比较难理解,起码我是理解了好久,就是给你n个位置每个位置标着一个数表示这个位置下一步能到哪个 ...

  3. code forces 383 Arpa&&num;39&semi;s loud Owf and Mehrdad&&num;39&semi;s evil plan&lpar;有向图最小环&rpar;

    Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 megab ...

  4. Arpa&&num;39&semi;s loud Owf and Mehrdad&&num;39&semi;s evil plan

    Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 megab ...

  5. C&period; Arpa&&num;39&semi;s loud Owf and Mehrdad&&num;39&semi;s evil plan

    C. Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 me ...

  6. Codeforces Round &num;383 &lpar;Div&period; 2&rpar; D&period; Arpa&&num;39&semi;s weak amphitheater and Mehrdad&&num;39&semi;s valuable Hoses —— DP(01背包)

    题目链接:http://codeforces.com/contest/742/problem/D D. Arpa's weak amphitheater and Mehrdad's valuable ...

  7. Codeforces Round &num;383 &lpar;Div&period; 2&rpar; B&period; Arpa’s obvious problem and Mehrdad’s terrible solution —— 异或

    题目链接:http://codeforces.com/contest/742/problem/B B. Arpa's obvious problem and Mehrdad's terrible so ...

  8. Codeforces Round &num;383 &lpar;Div&period; 2&rpar; D&period; Arpa&&num;39&semi;s weak amphitheater and Mehrdad&&num;39&semi;s valuable Hoses&lpar;分组背包&plus;dsu&rpar;

    D. Arpa's weak amphitheater and Mehrdad's valuable Hoses Problem Description: Mehrdad wants to invit ...

  9. Codeforces Round &num;383 &lpar;Div&period; 2&rpar; B&period; Arpa’s obvious problem and Mehrdad’s terrible solution

    B. Arpa’s obvious problem and Mehrdad’s terrible solution time limit per test 1 second memory limit ...

随机推荐

  1. WordPress基础:文章的自定义栏目的使用

    写文章只有标题和内容,那么我想加个加个怎么办呢?这时候就可以用到自定义栏目了,这个功能相当于增加了一个键值对 1.启动“自定义栏目” 2.定义键值对 3.调用自定义栏目值 <?php $pric ...

  2. PHP中VC6、VC9、TS、NTS版本的区别与用法详解

    Thread safe(线程安全)是运行在Apache上以模块的PHP上,如果你以CGI的模式运行PHP,请选择非线程安全模式(non-thread safe). 1. VC6与VC9的区别: VC6 ...

  3. Ajax、反向Ajax和WebSocket 概念

    Ajax 异步的JavaScript和XML(Asynchronous JavaScript and XML,Ajax),一种可通过JavaScript来访问的浏览器功能特性,其允许脚本向幕后的网站发 ...

  4. poj 2153 Rank List

    原题链接:http://poj.org/problem?id=2153 简单题,map,平衡树均可.. map: #include<algorithm> #include<iostr ...

  5. POJ 3865 - Database 字符串hash

    [题意] 给一个字符串组成的矩阵,规模为n*m(n<=10000,m<=10),如果某两列中存在两行完全相同,则输出NO和两行行号和两列列号,否则输出YES [题解] 因为m很小,所以对每 ...

  6. BZOJ 1087 互不侵犯king

    Description 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. Input 只有一行,包 ...

  7. 深入理解 JBoss 7&sol;WildFly Standalone 模式启动过程

    概述 JBoss 7/WildFly Standalone 模式启动过程大致例如以下: 启动脚本 standalone.sh 启动 JBoss Modules,JBoss Modules 启动 JBo ...

  8. Oracle 外键约束

    本文讨论一下Oracle中的外键约束问题,以及和DB2中的小不同. 首先创建测试环境. -- 创建测试主表. ID 是主键. CREATE TABLE test_main ( id INT, valu ...

  9. POI颜色设置

    package com.java.connect.poi; import java.io.FileOutputStream; import java.io.IOException; import or ...

  10. Android 如何将手机屏幕投影到 PC 屏幕上或者投影仪上做演示?

    Android 如何将手机屏幕投影到 PC 屏幕上或者投影仪上做演示? 公司开发款APP,要给领导演示,总不能用手机面对面演示吧.所以找了好久,找到一款体验超好的: Total Control-帮助你 ...