zoukankan      html  css  js  c++  java
  • 【CodeForces

    Love Triangle

    Descriptions:

    正如你所知道的,没有男性飞机也没有女性飞机。然而,地球上的每一个平面都喜欢另一个平面。地球上有n个平面,编号从1到n,编号i的平面喜欢编号fi的平面,其中1≤fi≤n且fi≠i。

    我们把平面A喜欢平面B,平面B喜欢平面C,平面C喜欢平面a的情况称为“三角恋”。

    Input

    第一行包含一个整数n(2≤n≤5000)——面数。

    第二行包含n个整数f1 f2…, fn(1≤fi≤n, fi≠i),表示第i个平面喜欢第fi个平面。

    Output

    输出"YES"如果地球上有一个由平面组成的三角恋。否则,输出"NO"。

    Examples
    Input
    5
    2 4 5 1 3
    Output
    YES
    Input
    5
    5 5 5 5 1
    题目链接
     
    水题 模拟吧
     
    AC代码
    #include <iostream>
    #include <cstdio>
    #include <fstream>
    #include <algorithm>
    #include <cmath>
    #include <deque>
    #include <vector>
    #include <queue>
    #include <string>
    #include <cstring>
    #include <map>
    #include <stack>
    #include <set>
    #include <sstream>
    #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
    #define Mod 1000000007
    #define eps 1e-6
    #define ll long long
    #define INF 0x3f3f3f3f
    #define MEM(x,y) memset(x,y,sizeof(x))
    #define Maxn 1100000
    using namespace std;
    int n;
    int a[Maxn];
    int main()
    {
        cin>>n;
        for(int i=1;i<=n;i++)
            cin>>a[i];
        int f=0;
        for(int i=1;i<=n;i++)
        {
            int cnt=1;//三角恋 当前次数为1
            int init=i;
            int x=i;
            int y;
            while(cnt<=3&&x<=n)
            {
                y=a[x];
                x=y;
                cnt++;
            }
            if(x==init)
            {
                f=1;
                cout<<"YES"<<endl;
                break;
            }
        }
        if(f==0)
            cout<<"NO"<<endl;
    
    }
  • 相关阅读:
    js中 var let const 区别
    img标签src引用网络图片,响应403的解决方法
    统计开发push数据
    gp日志查看
    node之path模块
    算法相关问题
    常用的 curl 发送 http 请求 命令
    Python与Go列表切片越界的对比
    golang时间与时区相关操作总结
    go语言结构体转map的方法
  • 原文地址:https://www.cnblogs.com/sky-stars/p/11221876.html
Copyright © 2011-2022 走看看