CF939A.Love Triangle

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 f__i, where 1 ≤ f__i ≤ n and f__i ≠ 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.

众所周知,飞机没有性别之分。然而,地球上的每架飞机都喜欢另一架飞机。地球上共有 nn 架飞机,编号从 11 到 nn,其中编号为 ii 的飞机喜欢编号为 fif_i 的飞机,其中 1≤fi≤n1 \leq f_i \leq n 且 fi≠if_i \neq i。

我们称“爱情三角形”为这样一种情形:飞机 AA 喜欢飞机 BB,飞机 BB 喜欢飞机 CC,且飞机 CC 喜欢飞机 AA。请判断地球上是否存在任何爱情三角形。

输入格式

The first line contains a single integer n (2 ≤ n ≤ 5000) — the number of planes.

The second line contains n integers _f_1, _f_2, ..., f__n (1 ≤ f__i ≤ n, f__i ≠ i), meaning that the i-th plane likes the f__i-th.

第一行包含一个整数 nn(2≤n≤50002 \leq n \leq 5000)—— 表示飞机的数量。

第二行包含 nn 个整数 f1, f2, …, fnf_1,\ f_2,\ \dots,\ f_n(1≤fi≤n1 \leq f_i \leq n,且 fi≠if_i \neq i),表示第 ii 架飞机喜欢第 fif_i 架飞机。

输出格式

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.

如果地球上存在由飞机组成的爱情三角形,则输出 «YES»;否则输出 «NO»。

您可以以小写或大写字母输出任意字符。

输入输出样例

  • 输入#1

    5
    2 4 5 1 3

    输出#1

    YES
  • 输入#2

    5
    5 5 5 5 1

    输出#2

    NO

说明/提示

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.

在第一个例子中,飞机 2 喜欢飞机 4,飞机 4 喜欢飞机 1,飞机 1 喜欢飞机 2,这就构成了一个“爱情三角形”。

在第二个例子中,不存在“爱情三角形”。

输入解题思路,AI测评打分。不知道怎么写?

首页