因为自建服务器又搞了很多很多所以 csdn 的博客荒废了有一年多,看着一个个到期的域名都不高兴续费想了想还是把东西记在 csdn 上吧...
#383 也没有打不知道上个礼拜在做什么...
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 repeated ttimes) 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 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).
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.
If there is no t satisfying the condition, print -1. Otherwise print such smallest t.
4 2 3 1 4
3
4 4 4 4 4
-1
4 2 1 4 3
1
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.
-------------
大概的意思就是:很多个人,第 i 个人可以给第 a[i] 个人打电话,让你求一个最小的 t,使得对于任意第 x 个人,可以通过正好 t 次传到 y,y 再 t 次传回来。
有向图找环,很简单的想法,如果图中存在一个点不在某一个环上(包括自环)那 t 一定不存在。否则的话找到所有的环,如果环的长度是奇数就直接算环长,如果是偶数就除以二,最后求这些数的最小公倍数。
code:
/* coding=utf8
Author: kyeremal
Email: Kyeremalprime@gmail.com
File Name: data.cpp
Problem: codeforces 383 div1 A
Description: 有向图最小环
--------------------------------*/
#include <bits/stdc++.h>
using namespace std;
#define rep(i, l, r) for (int (i) = (l); (i) <= (r); (i)++)
#define REP(i, l, r) for (int (i) = (l); (i) >= (r); (i)--)
#define LL long long
#define INF (1<<28)
#define MAXN 1010
int n, a[MAXN], ans;
LL lcm;
bool vis[MAXN];
vector<int> v;
inline void setIO() {
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
}
inline void dfs(int s, int x, int k) {
vis[x] = 1;
if (vis[a[x]]) {
if (a[x] == s) ans = ((k+1) % 2 == 0) ? (k+1)/2 : (k+1);
return;
}
dfs(s, a[x], k+1);
}
inline LL gcd(LL a, LL b) {
return (!b) ? a : gcd(b, a % b);
}
inline LL get_lcm(LL a, LL b) {
return (a * b) / gcd(a, b);
}
inline LL vector_lcm() {
LL ret = 1;
rep(i, 0, int(v.size())-1) {
ret = get_lcm(ret, (LL)v[i]);
}
return ret;
}
int main() {
// setIO();
scanf("%d", &n);
rep (i, 1, n) { // readin
scanf("%d", &a[i]);
}
memset(vis, 0, sizeof(vis)); // initialize array
rep (i, 1, n) {
if (!vis[i]) {
ans = -INF;
dfs(i, i, 0);
if (ans == -INF) break;
}
v.push_back(ans);
}
if (ans == -INF) puts("-1");
else printf("%d", vector_lcm());
return 0;
}