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.

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 repeatedt times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called theJoon-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 yx 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 xis 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.

分析:转移到自己时如果经过偶数次,则可以算一半贡献;

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=(int)m;i<=(int)n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
#define sys system("pause")
const int maxn=1e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,a[maxn];
ll ans,cnt;
set<int>pq;
bool flag;
void gao(int p,int q)
{
cnt++;
if(pq.find(p)!=pq.end())
{
flag=false;
return;
}
else if(p==q)return;
else pq.insert(p),gao(a[p],q);
}
int main()
{
int i,j;
ans=;
flag=true;
scanf("%d",&n);
rep(i,,n)scanf("%d",&a[i]);
rep(i,,n)
{
pq.clear();
cnt=;
gao(a[i],i);
if(cnt%==)cnt>>=;
ans=ans*cnt/gcd(ans,cnt);
}
if(flag)printf("%lld\n",ans);
else puts("-1");
//system("Pause");
return ;
}

Arpa's loud Owf and Mehrdad's evil plan的更多相关文章

  1. code forces 383 Arpa's loud Owf and Mehrdad's evil plan(有向图最小环)

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

  2. Codeforces Round #383 (Div. 2)C. Arpa's loud Owf and Mehrdad'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 ...

  3. Codeforces Round #383 (Div. 2) C. Arpa's loud Owf and Mehrdad's evil plan —— DFS找环

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

  4. C. Arpa's loud Owf and Mehrdad'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 ...

  5. 【codeforces 742C】Arpa's loud Owf and Mehrdad's evil plan

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  6. Codeforces 741A:Arpa's loud Owf and Mehrdad's evil plan(LCM+思维)

    http://codeforces.com/problemset/problem/741/A 题意:有N个人,第 i 个人有一个 a[i],意味着第 i 个人可以打电话给第 a[i] 个人,所以如果第 ...

  7. C. Arpa's loud Owf and Mehrdad's evil plan DFS + LCM

    http://codeforces.com/contest/742/problem/C 首先把图建起来. 对于每个a[i],那么就在i --- a[i]建一条边,单向的. 如果有一个点的入度是0或者是 ...

  8. Codeforces Round #383 (Div. 2) C. Arpa's loud Owf and Mehrdad's evil plan(dfs+数学思想)

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

  9. Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses(分组背包+dsu)

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

随机推荐

  1. 利用GeneratedKeyHolder获得新增数据主键值

    Spring利用GeneratedKeyHolder,提供了一个可以返回新增记录所对应的主键值的方法: int update(PreparedStatementCreator psc, KeyHold ...

  2. NodeJs+Express实现简单的Web增删改查

    前一段时间,公司组织了一次NodeJs的技术分享,自己有幸去听了听,第一次接触NodeJs,后来经过自己学习和探索,完成了一个很简单的Web演示项目,在这里和初学者做以分享,开发工具:WebStorm ...

  3. Xamarin原生跨平台概述(精简概述,命中要害。PS:无图)

    Xamarin原生跨平台:原生界面.原生性能.原生API(与H5比较): 1.C#可以访问Andrid.IOS原生API,也可以调用C#系统类型,如Syetem,System.IO;2.原理:基于Mo ...

  4. Picasso解决 TextView加载html图片异步显示

    项目中有这样一个需求: textview加载一段 html标签 其中包含 "<Img url= " 图片异步展示 而且 根据图片的比例 宽度满屏展示. 思路: 重写textv ...

  5. 从ASP.NET Web API 2 (C#)开始说起

    [译]Getting Started with ASP.NET Web API 2 (C#) 单击此处查看原文 HTTP不是单单为网页服务的,它更是一种用于构建API(用于暴露服务和数据)的强大平台. ...

  6. 驱动07.USB驱动程序

    1 了解USB识别的过程 eg:在Windows系统下的一个现象:把手机的USB设备接到PC 1. 右下角弹出"发现android phone" 2. 跳出一个对话框,提示你安装驱 ...

  7. Symfony3 更改生成CRUD目录步骤

    ---恢复内容开始--- 今天是2017-01-16号,项目零零散散的进行着: 由于Symfony3生成的crud在app目录需要转移到相应的Budle下面,记录以下过程: 我使用的是Symfony3 ...

  8. Python3 操作Excel--openpyxl

    操作Excel文件建议使用openpyxl 兼容office2016 打开excel文件,获取工作表 import openpyxl wb=openpyxl.load_workbook('ttt.xl ...

  9. Gpt转mbr

    1)Shift + F10 2)diskpart 3)list disk 4)select dist 0 5)clean 6)convert mbr [注]mbr常用于windows操作系统,而gpt ...

  10. leetcode 024

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...