A. Love Triangle
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 i likes the plane with number fi, where 1 ≤ fi ≤ n and 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
Input

Copy
5
2 4 5 1 3
Output
YES
Input

Copy
5
5 5 5 5 1
Output
NO
Note

In first example plane 2 likes plane 4, plane 4 likes plane 1, plane 1 likes plane 2 and that is a love triangle.

In second example there are no love triangles.

[题意]:判断是否存在A喜欢B,B喜欢C,C喜欢A.(即三角恋)

[分析]:a[a[a[i]]]==i可以判断存在三角恋

[代码]:

#include<bits/stdc++.h>

using namespace std;
const int maxn = +; int main()
{
int n,a[maxn],ans,j,k;
while(cin>>n)
{
ans=;
for(int i=;i<=n;i++)
{
cin>>a[i];
} for(int i=;i<=n;i++)
{
j=a[i];
k=a[j];
if(a[k]==i && i!=j && j!=k && i!=k) ans++;
}
printf("%s\n",ans?"YES":"NO");
}
}

交换变量法

#include<bits/stdc++.h>

using namespace std;
const int maxn = +; int main()
{
int n,a[maxn],f,ans;
while(cin>>n)
{
for(int i=;i<=n;i++)
cin>>a[i];
f=;
for(int i=;i<=n;i++)
{
if(a[a[a[i]]]==i) f=;
}
printf("%s\n",f?"YES":"NO");
}
}

数组嵌套法

Codeforces Round #464 (Div. 2) A. Love Triangle[判断是否存在三角恋]的更多相关文章

  1. Codeforces Round #464 (Div. 2) E. Maximize!

    题目链接:http://codeforces.com/contest/939/problem/E E. Maximize! time limit per test3 seconds memory li ...

  2. Codeforces Round #464 (Div. 2) A Determined Cleanup

    A. Love Triangle time limit per test1 second memory limit per test256 megabytes Problem Description ...

  3. Codeforces Round #464 (Div. 2)

    A. Love Triangle time limit per test: 1 second memory limit per test: 256 megabytes input: standard ...

  4. Codeforces Round #464 (Div. 2) D. Love Rescue

    D. Love Rescue time limit per test2 seconds memory limit per test256 megabytes Problem Description V ...

  5. Codeforces Round #464 (Div. 2) C. Convenient For Everybody

    C. Convenient For Everybody time limit per test2 seconds memory limit per test256 megabytes Problem ...

  6. Codeforces Round #464 (Div. 2) B. Hamster Farm

    B. Hamster Farm time limit per test2 seconds memory limit per test256 megabytes Problem Description ...

  7. Codeforces Round #464 (Div. 2) B. Hamster Farm[盒子装仓鼠/余数]

    B. Hamster Farm time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  8. 【Codeforces Round #239 (Div. 1) A】Triangle

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 最后的直角三角形可以通过平移,将直角顶点移动到坐标原点. 然后我们只要枚举另外两个点其中一个点的坐标就好了. x坐标的范围是[1.. ...

  9. Codeforces Round #464 (Div. 2) D题【最小生成树】

    Valya and Tolya are an ideal pair, but they quarrel sometimes. Recently, Valya took offense at her b ...

随机推荐

  1. 2017 ACM-ICPC EC-Final ShangHai(思维乱搞赛)

    感觉全是思维乱搞题. Gym - 101775J Straight Master 给你n种扑克,你每次可以出连续的3 ~ 5 张,问你能否出完. Sample Input 2 13 1 2 2 1 0 ...

  2. [BZOJ1588]营业额统计(Splay)

    Description 题意:给定 n个数,每给定一个数,在之前的数里找一个与当前数相差最小的数,求相差之和(第一个数为它本身) 如:5 1 2 5 4 6 Ans=5+|1-5|+|2-1|+|5- ...

  3. vue --子父组件传值

    1.父组件可以使用 props 把数据传给子组件. 2.子组件可以使用 $emit 触发父组件的自定义事件. vm.$emit( event, arg ) //触发当前实例上的事件 vm.$on( e ...

  4. 手机端sticker布局,底部按钮在屏幕底部

    <template> <div class="product-detail-container"> <div class="detail&q ...

  5. day40--mysql step4 SQLAlchemy

    1.unique = True 表示启动唯一索 2.有add 必须有commit这样数据才会提交 3.ORM功能 #!/usr/bin/env python # -*- coding:utf-8 -* ...

  6. laravel5.2总结--序列化

    序列化 构建Json格式的API接口时,经常需要转换 '模型' 和 '关联关系' 为数组或者JSON. 1>转换模型为数组:   $user = App\User::with('roles')- ...

  7. Hyper-V动态迁移中?小心性能损失

    动态迁移是虚拟化技术的一个标志,它允许虚拟机在服务器间进行动态迁移.调节负载平衡.性能管理.备灾管理和数据中心维护.Windows Server 2012 R2中的Hyper-V动态迁移默认功能具备相 ...

  8. Oracle 学习笔记(十)

    合并查询 在实际项目开发中经常遇到要合并结果集的情况,可以使用集合操作符:union,union all,intersect,minus.这次笔记学习这几个操作符. [union] 该操作符用于取得两 ...

  9. CentOS 7使用dnf安装Memcached以及启动、停止、开机启动等设置

    1.安装Memcached dnf install memcached 根据提示完成安装 2.启动Memcached 输入以下命令: service memcached start 输出以下内容: R ...

  10. 精通CSS高级Web标准解决方案(3-1 背景图像与图像替换)

    3.1背景图像基础 3.2图像替换 使用文本的图像并保留文本的方法.