Codeforces Round #464 (Div. 2) A. Love Triangle[判断是否存在三角恋]
1 second
256 megabytes
standard input
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.
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 «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.
5
2 4 5 1 3
YES
5
5 5 5 5 1
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.
[题意]:判断是否存在A喜欢B,B喜欢C,C喜欢A.(即三角恋)
[分析]:a[a[a[i]]]==i可以判断存在三角恋
[代码]:
#include<bits/stdc++.h> using namespace std;
const int maxn = +; int main()
{
int n,a[maxn],ans,j,k;
while(cin>>n)
{
ans=;
for(int i=;i<=n;i++)
{
cin>>a[i];
} for(int i=;i<=n;i++)
{
j=a[i];
k=a[j];
if(a[k]==i && i!=j && j!=k && i!=k) ans++;
}
printf("%s\n",ans?"YES":"NO");
}
}
交换变量法
#include<bits/stdc++.h> using namespace std;
const int maxn = +; int main()
{
int n,a[maxn],f,ans;
while(cin>>n)
{
for(int i=;i<=n;i++)
cin>>a[i];
f=;
for(int i=;i<=n;i++)
{
if(a[a[a[i]]]==i) f=;
}
printf("%s\n",f?"YES":"NO");
}
}
数组嵌套法
Codeforces Round #464 (Div. 2) A. Love Triangle[判断是否存在三角恋]的更多相关文章
- Codeforces Round #464 (Div. 2) E. Maximize!
题目链接:http://codeforces.com/contest/939/problem/E E. Maximize! time limit per test3 seconds memory li ...
- Codeforces Round #464 (Div. 2) A Determined Cleanup
A. Love Triangle time limit per test1 second memory limit per test256 megabytes Problem Description ...
- Codeforces Round #464 (Div. 2)
A. Love Triangle time limit per test: 1 second memory limit per test: 256 megabytes input: standard ...
- Codeforces Round #464 (Div. 2) D. Love Rescue
D. Love Rescue time limit per test2 seconds memory limit per test256 megabytes Problem Description V ...
- Codeforces Round #464 (Div. 2) C. Convenient For Everybody
C. Convenient For Everybody time limit per test2 seconds memory limit per test256 megabytes Problem ...
- Codeforces Round #464 (Div. 2) B. Hamster Farm
B. Hamster Farm time limit per test2 seconds memory limit per test256 megabytes Problem Description ...
- 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 ...
- 【Codeforces Round #239 (Div. 1) A】Triangle
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 最后的直角三角形可以通过平移,将直角顶点移动到坐标原点. 然后我们只要枚举另外两个点其中一个点的坐标就好了. x坐标的范围是[1.. ...
- Codeforces Round #464 (Div. 2) D题【最小生成树】
Valya and Tolya are an ideal pair, but they quarrel sometimes. Recently, Valya took offense at her b ...
随机推荐
- 第5模块闯关CSS练习题
1.列举你知道的css选择器? 说道css选择器,大家都知道有许多种,但是真要你去掰着手指头数一数的话,你可能需要数几分钟.其实这么多选择器,完全可以分为两类: 标签选择器(*是特殊情况),可但标签, ...
- 第2章c++简单程序设计
第2章c++简单程序设计 知识梳理 以下是我遗忘以及认为重要的知识整理: 1.标识符的构成规则: 以大写字母.小写字母或下划线 _ 开始 由大写字母.小写字母.下划线 _ 或数字(0~9)组成 大写字 ...
- IOS开发---菜鸟学习之路--(八)-实现新闻页面
本章将具体讲述如何结合前两张的内容最终实现一个新闻页面的雏形 之所以称之为雏形,是因为本章实现的内容只是实现了最基础的效果 还有很多其他诸如下拉刷新 页面导航等效果都需要投入一些时间进行研究 好了直接 ...
- 【Best Time to Buy and Sell Stock II】cpp
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- leetcode 【 Reverse Nodes in k-Group 】 python 实现
原题: Given a linked list, reverse the nodes of a linked list k at a time and return its modified list ...
- Python+Selenium练习篇之19-多窗口之间切换
本文来介绍如何处理driver在多窗口之间切换,想一下这样的场景,在页面A点击一个连接,会触发在新Tab或者新窗口打开页面B,由于之前的driver实例对象在页面A,但是你接下来的脚本是操作页面B的元 ...
- c++11特性使用
#include <list> #include <iostream> int main(){ list<int> lst; for(list<int> ...
- 学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用利用条件运算符的嵌套来完成此题:C表示。
# -*- coding: utf8 -*- # Author:wxq #python 2.7 #题目:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用利用条件运算符 ...
- Android数据储存之SharedPreferences
Android中SharedPreferences通常与Editor连用 接口SharedPreferences常用方法: boolean contains(String str):判断SharedP ...
- cookie和session机制区别
会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端 ...