A. Love Triangle

time limit per test1 second

memory limit per test256 megabytes

Problem Description

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

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 4 likes plane 1, plane 1 likes plane 2 and that is a love triangle.

In second example there are no love triangles.


解题心得:

  1. 其实题意很简单,就是问你给出的连线中是否能够形成三角形。
  2. 其实很简单,每个点的出度只能为1,这样直接枚举每个点,回溯两次看是否可以回溯到自身就行了。刚开始没有注意到每个点的出度只能为1,写了一个比较复杂的dfs。

代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 5010; int n,to[maxn]; int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&to[i]); bool flag = false; for(int i=1;i<=n;i++)
if(to[to[to[i]]] == i)
flag = true; if(flag)
printf("YES");
else
printf("NO");
return 0;
}

写得很繁琐的智障代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 5010; int n;
bool flag,vis[maxn];
vector <int> ve[maxn]; void dfs(int pos,int pre,int cnt){
vis[pos] = true;
if(flag)
return ;
for(int i=0;i<ve[pos].size();i++) {
int v = ve[pos][i];
if(cnt == 3 && vis[v] && v != pre)
flag = true;
if(cnt == 3 && (!vis[v] || v == pre))
continue;
dfs(v,pos,cnt+1);
}
return ;
} void check(){
for(int i=1;i<=n;i++){
memset(vis,0,sizeof(vis));
dfs(i,-1,1);
}
} int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
int temp;
scanf("%d",&temp);
ve[i].push_back(temp);
} check(); if(flag)
printf("YES");
else
printf("NO");
return 0;
}

Codeforces Round #464 (Div. 2) A Determined Cleanup的更多相关文章

  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) D. Love Rescue

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

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

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

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

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

  5. 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 ...

  6. Codeforces Round #464 (Div. 2) A. Love Triangle[判断是否存在三角恋]

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

  7. Codeforces Round #464 (Div. 2)

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

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

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

  9. Codeforces Round #346 (Div. 2)---E. New Reform--- 并查集(或连通图)

    Codeforces Round #346 (Div. 2)---E. New Reform E. New Reform time limit per test 1 second memory lim ...

随机推荐

  1. SpringBoot | 第十八章:web应用开发之WebJars使用

    前言 前面一章节我们主要讲解了关于文件上传的两种方式.本章节继续web开发的相关知识点.通常对于web开发而言,像js.css.images等静态资源版本管理是比较混乱的,比如Jquery.Boots ...

  2. Docker | 第六章:构建私有仓库

    前言 上一章节,讲解了利用Dockerfile和commit进行自定义镜像的构建.大部分时候,公司运维或者实施部门在构建了符合公司业务的镜像环境后,一般上不会上传到公共资源库的.这就需要自己搭建一个私 ...

  3. JS文本框输入限制

    1上面的文本框只能输入数字代码(小数点也不能输入): CODE: <input onkeyup="this.value=this.value.replace(/\D/g,'')&quo ...

  4. ServiceStack.Redis 使用

    Redis官网提供了很多开源的C#客户端.例如,Nhiredis ,ServiceStack.Redis ,StackExchange.Redis等.其中ServiceStack.Redis应该算是比 ...

  5. 关于wav文件fft处理后x,y轴坐标数据的问题

    1.关于横坐标的频率的最大值是采样频率,那么每个点对应的频率值就很好算了:f(n) = [Fs/(N/2)]*n  (Fs是采样频率,常见的是44.1KHz(44100),N是采样点数,k表是第k个点 ...

  6. Windows服务器高并发处理IOCP(完成端口)详细说明

    一. 完成端口的优点 1. 我想只要是写过或者想要写C/S模式网络服务器端的朋友,都应该或多或少的听过完成端口的大名吧,完成端口会充分利用Windows内核来进行I/O的调度,是用于C/S通信模式中性 ...

  7. zabbix文档3.4

    zabbix 文档 3.4 5.zabbix 快速入门 1 登录和配置用户 用户名 : Admin 密 码 : zabbix 防止暴力袭击 在连续五次失败登录尝试的情况下,Zabbix界面将暂停30秒 ...

  8. ArcGIS10.1的安装问题

    注:必须用3个带0的文件夹里面的东西安装 1.先装Pre-release_license_manager   ,然后停掉. 2.然后安装0Desktop/ArcGIS_Desktop, 3.打开0Ke ...

  9. 500 Days Of Summer

    <和莎莫的500天>,一部爱情片. Summer和Tom两人不同的爱情观走在了一起,或许真的就是爱情观不同,或许是Summer爱Tom爱的不够深,最终的结局是那么不尽人意. 有人愿意把秘密 ...

  10. js中(break,continue,return)的区别

    break 一般用于跳出整个循环(for,while) continue  跳出本次循环,进入下一次循环 return 只能出现在函数体内,一旦执行return,后面的代码将不会执行,经常用retur ...