当前位置: 首页 > news >正文

练习cf939A. Love Triangle

题目如下
A. Love Triangle
time limit per test1 second
memory limit per test256 megabytes
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.

题目大意
现有n个飞机,编号从1到n,数组元素的值代表了每个飞机喜欢的飞机编号,如果A->B,B->C, C->A,那么称之为三角恋,请问上述飞机中是否存在这种情况。

题目分析
嵌套即可,确认B喜欢的飞机C喜欢的飞机是不是A即可。

点击查看代码
#include <iostream>
using namespace std;int main(){int n;cin >> n;int like[5005];for(int i = 1; i <= n; i++){cin >> like[i];}bool tri = false;for(int i = 1; i < n; i++){if(like[like[like[i]]] == i){tri = true;break;}}if(tri){printf("YES\n");}else{printf("NO\n");}return 0;
}
http://www.wuyegushi.com/news/65.html

相关文章:

  • CVE-2023-46604 Apache ActiveMQ 远程代码执行漏洞 (复现)
  • Day26
  • 假期学习
  • 第二十四天
  • 在python虚拟环境中遇到 ​​No module named pip​​ 错误解决方案
  • 从零开始的web前端学习-React
  • tinymce富文本编辑器使用
  • 微软C语言编译器‘strcpy‘: This function or variable may be unsafe. Consider using strcpy_s instead
  • Java学习Day26
  • 线性基(个人学习笔记)
  • 花菖蒲 2025.7.26 模拟赛题解
  • 信任的意外反射:深入解析LLVM循环向量化器中的罕见编译错误
  • P1429 平面最近点对(加强版)[骗分解法]
  • 7.26 - GENGAR
  • P4565 [CTSC2018] 暴力写挂 题解
  • 第十二篇
  • 计算机网络——应用层 - 浪矢
  • 《第一节:跟着符映维学C语言---配置c语言开发环境》
  • 再见,大连
  • 影视软件集合分享
  • 7.26总结
  • geogebra 2 进阶
  • 20250726GF模拟赛
  • java学习
  • 深入解析Passkeys背后的密码学原理
  • CCF中学生计算机程序设计-基础篇-第一章-函数练习答案
  • 第二次总结——关系中的魔法语言
  • 2025.7 Solar应急响应-
  • 【计算几何】Largest Quadrilateral
  • 2025暑假qbxtNOIP实战梳理营Day1题目