Love Triangle

 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";
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值