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

数组嵌套法

最新文章

  1. Coursera台大机器学习课程笔记7 -- Noise and Error
  2. Android--Retrofit的简单使用(一)
  3. iOS中的多线程及GCD
  4. 函数xdes_find_bit
  5. bzoj1231: [Usaco2008 Nov]mixup2 混乱的奶牛
  6. Python爬虫实战(1):爬取Drupal论坛帖子列表
  7. nginx-gridfs 的安装配置和使用
  8. 《探索未知种族之osg类生物》目录
  9. 小问题填坑,关于obj.x和obj[&quot;x&quot;]
  10. WPA简介
  11. UUChart的使用
  12. mongodb批量操作, bulk_write,
  13. 【PyQt5-Qt Designer】按钮系列
  14. Java IO中转换流的作用
  15. Swift语言精要 - 属性
  16. The YubiKey NEO
  17. Maximum Likelihood Method最大似然法
  18. PyalgoTrade 打印收盘价(二)
  19. Js里头的对象字面量
  20. ***七牛跨域上传图片JS SDK

热门文章

  1. Fiddler用AutoResponder实现app升级异步更新
  2. 读书笔记jvm探秘之二: 对象创建
  3. Springmvc 重定向参数传递方式
  4. 继承Thread类使用多线程
  5. SSH进阶之路
  6. getshell不用英文数字 或者不用下划线
  7. Oracle连接查询小结
  8. BZOJ-3190 [JLOI2013]赛车
  9. bzoj 4292: [PA2015]R&#243;wnanie
  10. this.$router.push() 在新窗口怎么打开