Codeforces Round #464 (Div. 2) A Determined Cleanup
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,这样直接枚举每个点,回溯两次看是否可以回溯到自身就行了。刚开始没有注意到每个点的出度只能为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的更多相关文章
- 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) 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 #464 (Div. 2) A. Love Triangle[判断是否存在三角恋]
A. Love Triangle time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- 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题【最小生成树】
Valya and Tolya are an ideal pair, but they quarrel sometimes. Recently, Valya took offense at her b ...
- 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 ...
随机推荐
- mysql必知必会学习笔记(1)
chapter 13 13.1 数据分组 如果要将数据分为多个逻辑组怎么办?? 要用数据分组 13.2 创建分组 使用group by子句.group by会指示对mysql进行分组,然后对每个组进行 ...
- 转---JS 获取鼠标左右键
原文:http://blog.csdn.net/mine3333/article/details/7291557 function test() { alert(event.x+" &quo ...
- C 碎片三 运算符与表达式
一.算术运算符 算术运算符:+. -. *. /. %等 加:+ 减: - 乘: * 除: / 除数不能为0 模:% 参与模运算的数据不能为小数 二.赋值运算符 赋值运算符:= 作用: ...
- fstab 解析
某些时候当Linux系统下划分了新的分区后,需要将这些分区设置为开机自动挂载,否则,Linux是无法使用新建的分区的. /etc/fstab 文件负责配置Linux开机时自动挂载的分区. Window ...
- 《译》准备做一些 AR/增强现实的 翻译
中文这方面资料实在少之又少. 准备做一些这方面翻译,关注于Vuforia, Unity3d, Hololens等方面. 如有问题.建议,随时联系.Fell free ton contact me.
- 一个普通 iOS 码农的几个小项目相关知识点总结
题记:在开发的路途上,有的人走的很深很远,而对于停留在初级阶段的我来说,还要学的.经历的还有很多... list sqlite 数据库中,当把表里的数据都清空时,下次插入的数据的 id 主键不会从 0 ...
- Android(java)学习笔记98:如何让你的GridView不再滚动
1. 如何让你的GridView不再滚动: GridView显示不完整的原因是因为,他的外层也套用了一个滑动的控件,这个解决办法是:重写GridView,是控制GridView不能滚动,就是写一个类继 ...
- gearmand 编译 Unable to find libevent
如果出现configure: error: Unable to find libevent,则输入命令:yum -y install libevent libevent-devel然后重新config ...
- Java读取properties配置文件工具类
1. PropertyUtils.java package javax.utils; import java.io.InputStream; import java.util.Properties ...
- vs code vim
很多初学者启动vim后,不知道怎么输入字符:按了半天字母,结果屏幕还是空的. vim和记事本或WORD不一样,不是一打开后就可以输入文字,此时它处于正常模式. vim一共有4个模式: 正常模式 (No ...