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

阅读: 评论:0

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

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

题目链接:


 

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.

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: &#wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: &#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 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 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.

 

 



题解:


错误的做法:

本以为t最大不会超过n,所以就用p[i][j]记录,记录距离结点i,j个距离的是哪个顶点。然后再依次枚举j,找到合适的t。

后来发现:t可以大于n,所以此方法失败。


正确的做法:

t为所有环的最小公倍数。(当环长为奇数时,直接取环长;当环长为偶数时,取环长的一半,因为可以刚好走到正对面)




错误做法:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const double eps = 1e-6;
 5 const int INF = 2e9;
 6 const LL LNF = 9e18;
 7 const int mod = 1e9+7;
 8 const int maxn = 100+10;
 9 
10 int n, a[maxn], p[maxn][maxn];
11 
12 void dfs(int f, int u, int k)
13 {
14     if(k>n) return;
15     p[f][k] = u;
16     dfs(f,a[u], k+1);
17 }
18 
19 int main()
20 {
21     cin>>n;
22     for(int i = 1; i<=n; i++)
23         cin>>a[i];
24     for(int i = 1; i<=n; i++)
25         dfs(i,a[i],1);
26 
27     int ans = -1;
28     for(int t = 1; t<=n; t++)
29     {
30         int i;
31         for(i = 1; i<=n; i++)
32         {
33             int v = p[i][t];
34             if(p[v][t]!=i)
35                 break;
36         }
37         if(i==n+1)
38         {
39             ans = t;
40             break;
41         }
42     }
43     cout<<ans<<endl;
44 }
View Code

 

正确做法:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const double eps = 1e-6;
 5 const int INF = 2e9;
 6 const LL LNF = 9e18;
 7 const int mod = 1e9+7;
 8 const int maxn = 100+10;
 9 
10 int n, a[maxn],vis[maxn];
11 
12 int gcd(int a, int b) { return b==0?a:gcd(b,a%b); }
13 
14 int dfs(int f, int i, int k)
15 {
16     if(vis[i]) return (i==f)?k:-1;
17     vis[i] = 1;
18     return dfs(f, a[i], k+1);
19 }
20 
21 int main()
22 {
23     cin>>n;
24     for(int i = 1; i<=n; i++)
25         cin>>a[i];
26 
27     int ans = 1;
28     for(int i = 1; i<=n; i++)
29     {
30         if(vis[i]) continue;
31         int x = dfs(i,i,0);
32         if(x==-1)
33         {
34             ans = -1;
35             break;
36         }
37         if(!(x&1)) x >>= 1;
38         ans = (ans*x)/gcd(ans,x);
39     }
40     cout<<ans<<endl;
41 }
View Code

 

转载于:.html

本文发布于:2024-02-04 21:39:17,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170717031259856.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:Arpa   loud   Div   Codeforces   plan
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23