Love Triangle
可乐喜欢雪碧,雪碧喜欢芬达,芬达喜欢可乐,这叫做三角恋
告诉你每个人喜欢谁,问你这堆人里面有没有三角恋存在
input
第一行N(2≤ Ñ ≤5000) -N个人。 第二行包含Ñ整数F 1, F 2,..., F Ñ(1≤ Fi ≤ Ñ,Fi ≠ i),这意味着第i个人喜欢第Fi个人。
output
如果有三角恋关系输出YES,没有输出NO
Examples
Input
5
2 4 5 1 3
Output
YES
Input
5
5 5 5 5 1
Output
NO```
```cpp
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=5000+10;
/*
2 4 5 1 3
a[0] = 1;
a[1] = 3;
a[2] = 4;
a[3] = 0;
a[4] = 2;
*/
int main()
{
int n;
cin>>n;
int a[maxn];
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
int flag=0;
for(int i=0;i<n;i++)
{
int j=a[i]-1;
int count=1;
int temp;
while(j!=i&&count<=n)
{
temp = a[j]-1;
j=temp;
count++;
}
if(count==3)
{
flag=1;
printf("YES");
break;
}
}
if(flag==0)
{
cout<<"NO";
}
}
Love Triangle
最新推荐文章于 2021-08-11 23:49:49 发布