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. 利用Java程序将字符串进行排序与拼接

    1.初始生成字符串的代码程序: package com.map.test; import java.util.ArrayList; import java.util.Collections; impo ...

  2. 最简实例演示asp.net5中用户认证和授权(3)

    上接: 最简实例演示asp.net5中用户认证和授权(2) 在实现了角色的各种管理接口后,下一步就是实现对用户的管理,对用户管理的接口相对多一些,必须要实现的有如下三个: 1 public inter ...

  3. 使用jQuery实现文本框input定位到文字最后(兼容所有浏览器)

    $.fn.setCursorPosition = function(position){ if(this.lengh == 0) return this; return $(this).setSele ...

  4. 零基础逆向工程30_Win32_04_资源文件_消息断点

    1 资源文件,创建对话框 详细步骤: 1.创建一个空的Win32应用程序 2.在VC6中新增资源 File -> New -> Resource Script 创建成功后会新增2个文件:x ...

  5. 配置海康IPC或大华IPC通过路由器公网访问

    设备:路由器DLink-DIR-600M,海康IPC:DS-2CD864FWD-E 海康默认端口为8000,HTTP访问为80,RTSP访问端口为554. 配置分成两步,分别为配置IPC相关网络参数和 ...

  6. 画报表框架——Echarts.js

    官网:http://echarts.baidu.com/index.html ————————————————————————————————— 先看看我做的第一个柱状图形报表 ——————————— ...

  7. Neo4j-3.0.3 (Debian 8)

    平台: Ubuntu 类型: 虚拟机镜像 软件包: neo4j-3.0.3 basic software database graph database infrastructure neo4j op ...

  8. Yii2 Working with Relational Data at ActiveDataProvider

    Yii2 Working with Relational Data at ActiveDataProvider namespace common\models; use Yii; use yii\ba ...

  9. 手机端@media screen布局自适应

    @media only screen and (min-width: 310px) and (max-width: 360px) { }@media only screen and (min-widt ...

  10. 更新KB915597补丁后导致“您的windows副本不是正版”的解决方案

    更新KB915597补丁后导致“您的windows副本不是正版”的解决方案 解决方法: 运行cw.exe(https://pan.lanzou.com/i05ya8h),直至提示成功: 重新启动操作系 ...