L - Love Triangle

Posted mzchuan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 

 

以上是关于L - Love Triangle的主要内容,如果未能解决你的问题,请参考以下文章

Love2d从青铜到王者第四篇:Love2d之LÖVE与移动矩形

HDU6964 I love counting (字典树+莫队)

爱的英文缩写

HDU 6599 I Love Palindrome String (回文树+hash)

I love you 的数学代码

HARRYWINSTON高级珠宝 with love爱情