L - Love Triangle

As you could know there are no male planes nor female planes. However, each plane on Earth likes some other plane. There are n planes on Earth, numbered from 1 to n, and the plane with number ilikes the plane with number fi, where 1 ≤ fi ≤ nand fi ≠ i.

We call a love triangle a situation in which plane A likes plane B, plane B likes plane C and plane C likes plane A. Find out if there is any love triangle on Earth.

Input

The first line contains a single integer n (2 ≤ n ≤ 5000) — the number of planes.

The second line contains n integers f1, f2, ..., fn(1 ≤ fi ≤ nfi ≠ i), meaning that the i-th plane likes the fi-th.

Output

Output «YES» if there is a love triangle consisting of planes on Earth. Otherwise, output «NO».

You can output any letter in lower case or in upper case.

Examples

Input
5
2 4 5 1 3
Output
YES
Input
5
5 5 5 5 1
Output
NO

Note

In first example plane 2 likes plane 4, plane 4likes plane 1, plane 1 likes plane 2 and that is a love triangle.

In second example there are no love triangles.

 

百度翻译:正如你所知道的,没有男性和女性的平面。然而,地球上的每一个平面都喜欢另一个平面。地球上有n个平面,编号从1到n,而编号为i的平面喜欢编号为fi的平面,其中1≤fi≤n和fi≠i。我们把一个三角叫做A面喜欢B面,B面喜欢C面,C面喜欢A面,看看地球上有没有三角。

 

思路:因为是第i个喜欢fi,所以当sz[sz[sz[i]]]等于i时,表示有三角关系。注意第i个的值不能等于i.

 

 1 #include <cstdio>
 2 #include <fstream>
 3 #include <algorithm>
 4 #include <cmath>
 5 #include <deque>
 6 #include <vector>
 7 #include <queue>
 8 #include <string>
 9 #include <cstring>
10 #include <map>
11 #include <stack>
12 #include <set>
13 #include <sstream>
14 #include <iostream>
15 #define mod 1000000007
16 #define eps 1e-6
17 #define ll long long
18 #define INF 0x3f3f3f3f
19 using namespace std;
20 
21 int sz[5005],n;
22 int main()
23 {
24     scanf("%d",&n);
25     for(int i=1;i<=n;i++)
26     {
27         scanf("%d",&sz[i]);
28     }
29     for(int i=1;i<=n;i++)
30     {
31         if(sz[sz[sz[i]]]==i&&sz[i]!=i)
32         {
33             printf("YES\n");
34             return 0;
35         }
36     }
37     printf("NO\n");
38 }

 

转载于:https://www.cnblogs.com/mzchuan/p/11222721.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值