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 ...
随机推荐
- pat1035. Password (20)
1035. Password (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue To prepare f ...
- [LeetCode]10. Regular Expression Matching正则表达式匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- CF1182E Product Oriented Recurrence
思路: fn = can * f1xn * f2yn * f3zn, 首先dp计算指数部分an = an-1 + an-2 + an-3 + 2 * n - 6, 而an-1 = an-2 + an- ...
- 编译出freeswitch的java调用的 jar和so
假设freeswitch 源码路径为 /usr/local/src/freeswitch 1. cd /usr/local/src/freeswitch(源代码的根目录) 执行./configure, ...
- 跨平台移动开发phonegap/cordova 3.3全系列教程-目录
目录(更新完成后会附上源码供参考) 第一章 android平台开发 phonegap/cordova简介 1.开发环境搭建 2.helloworld 3.启动画面 4.结合asp.net/jqmboi ...
- Java栈,PC寄存器,本地方法栈,堆,方法区(静态区)和运行常量池
详情参考:https://my.oschina.net/wangsifangyuan/blog/711329 前言:当要判断一个变量存在什么空间上哪儿时,先分析它是哪一种(是实例变量还是局部变量),实 ...
- bzoj3887: [Usaco2015 Jan]Grass Cownoisseur
题意: 给一个有向图,然后选一条路径起点终点都为1的路径出来,有一次机会可以沿某条边逆方向走,问最多有多少个点可以被经过?(一个点在路径中无论出现多少正整数次对答案的贡献均为1) =>有向图我们 ...
- 【BZOJ4458】GTY的OJ(树上超级钢琴)
点此看题面 大致题意: 给你一棵树,让你求出每一个节点向上的长度在\([l,r]\)范围内的路径权值和最大的\(m\)条路径的权值总和. 关于此题的数列版本 此题的数列版本,就是比较著名的[BZOJ2 ...
- NBU基本常用命令
Veritas常用命令: 1. 查看当有运行的任务 bpdbjobs –report | grep Active 2. 停止任务 bpdbjobs –cancel PID (包括主任务和子任务) 3. ...
- sessionStorage 和 localStorage
html5 中的 web Storage 包括了两种存储方式:sessionStorage 和 localStorage. sessionStorage 用于本地存储一个会话(session)中的数据 ...