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 ≤ n, fi ≠ 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
5
2 4 5 1 3
YES
5
5 5 5 5 1
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.
Baidu translation: As you know, there is no plane for men and women. However, each plane on the planet like the other plane. N on earth planes, numbered from 1 to n, i is the number of flat planar like number fi, and wherein 1≤fi≤n fi ≠ i. We like a triangle called the B side A side, B side surface like C, C A face like the face, to see if there triangle on the planet.
Idea: because love is the i-th fi, so when sz [sz [sz [i]]] is equal to i, expressed triangle. Note that the i-th value is not equal to 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 }