cf 464 A. Love Triangle

本文介绍了一个关于判断是否存在飞机间的爱情三角的算法问题。输入为若干飞机及其喜欢的对象编号,输出是否存在爱情三角的情况。
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.


#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
using namespace std;
#define INF 0x7f
const int maxn =10005; 
typedef long long ll ;
#define f(i,l,r) for(int i=l;i<=r;++i)
#define g(i,l,r) for(int i=l;i>=r;--i)
int n ;
int a[maxn];
bool flag=false;
int main()
{
	cin>>n;
	f(i,1,n)
		cin>>a[i];
	f(i,1,n)
	{
		if(a[a[a[i]]]==i)
			flag=true;
	}
	 if(flag)cout<<"YES"<<endl;
	 else cout<<"NO"<<endl;

	return 0;

}
未来的我一定会感谢现在正在成长的我




在Python中,`.format()` 是字符串的一个方法,用于格式化字符串。它允许将变量的值插入到字符串中的占位符位置。 ### `.format()` 方法基本使用 ```python triangle = "right-angled triangle" message = "This is a {}.".format(triangle) print(message) ``` 在上述代码中,`{}` 是占位符,`.format(triangle)` 会将 `triangle` 变量的值插入到占位符的位置,最终输出 `"This is a right-angled triangle."`。 ### 引用中的相关联系 参考引用[2]中提到了 `.format()` 方法,这里 `fn_explainer.format(function_name=get_source_code)` 是 `FunctionExplainerPromptTemplate` 类的 `format` 方法的调用,它接受关键字参数 `function_name`,将其值格式化到对应的模板中。该方法灵活且解耦,调用者能按需传入任意多个参数,方法内部通过 `kwargs` 访问所需参数,不受外部参数变化的影响 [^2]。 ### 常见使用场景及示例 #### 多个参数 ```python side1 = 3 side2 = 4 side3 = 5 triangle_info = "A triangle with sides {}, {} and {} can be a right-angled triangle.".format(side1, side2, side3) print(triangle_info) ``` #### 指定参数顺序 ```python side1 = 3 side2 = 4 side3 = 5 triangle_info = "A triangle with sides {2}, {1} and {0} can be a right-angled triangle.".format(side1, side2, side3) print(triangle_info) ``` #### 关键字参数 ```python triangle = { "side1": 3, "side2": 4, "side3": 5 } triangle_info = "A triangle with sides {side1}, {side2} and {side3} can be a right-angled triangle.".format(**triangle) print(triangle_info) ``` ### 相关问题 1. `.format()` 方法和 f-string 有什么区别? 2. `.format()` 方法在处理复杂数据类型(如列表、字典)时的使用技巧有哪些? 3. 如果 `.format()` 方法的参数数量和占位符数量不匹配会出现什么问题? 4. 在 Python 3.6 之后,`.format()` 方法是否还有使用的必要? 5. 如何在 `.format()` 方法中对插入的值进行格式化(如保留小数位数)?
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值