• L


    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
    ");
    34             return 0;
    35         }
    36     }
    37     printf("NO
    ");
    38 }
  • 相关阅读:
    Leetcode题解(4):L216/Combination Sum III
    零基础学python-3.1 python基本规则和语句
    3D打印技术之切片引擎(4)
    自己定义struts2中action类型转换器
    Android开发之实现锁屏功能
    用户上传本地磁盘中的一个图片文件作为头像
    微博达人硅谷之歌:Testin云測移动搜索性能測试非常是让人信服
    读《疯狂Java讲义》笔记总结三
    这才是爱情最好的样子
    数据挖掘 与 Web开发何去何从
  • 原文地址:https://www.cnblogs.com/mzchuan/p/11222721.html
Copyright © 2020-2023  润新知