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 ...
随机推荐
- android 开发-文件存储之读写sdcard
android提供对可移除的外部存储进行文件存储.在对外部sdcard进行调用的时候首先要调用Environment.getExternalStorageState()检查sdcard的可用状态.通过 ...
- maven相关说明,以及使用Testng相关
配置Apache Maven Apache Maven使用本身的配置和建立的项目位于许多地方: MAVEN_OPTS环境变量: 该变量包含用于启动运行Maven的JVM的参数,可用于向Maven提供其 ...
- java使用线程请求访问每次间隔10分钟连续5次,之后停止请求
java使用线程请求访问每次间隔10分钟连续5次,收到相应的时候停止请求 package com.qlwb.business.util; /** * * * @类编号: * @类名称:RequestT ...
- ElasticSearch java API-使用More like this实现基于内容的推荐
ElasticSearch java API-使用More like this实现基于内容的推荐 基于内容的推荐通常是给定一篇文档信息,然后给用户推荐与该文档相识的文档.Lucene的api中有实现查 ...
- Vmware 可用的激活码
4F207-8HJ1M-WZCP8-000N0-92Q6G 0A6Z5-8H1EJ-WZCL1-PK072-23DJG 0F0Q9-8F38L-RZXT9-4U054-ACW5F JA02E-09H4 ...
- 学习《CSS选择器Level-4》不完全版
1 概述 1.1 前言 选择器是CSS的核心组件.本文依据W3C的Selectors Level 4规范,概括总结了Level1-Level4中绝大多数的选择器,并做了简单的语法说明及示例演示.希望对 ...
- 菜鸟 学注册机编写之 “MD5”
测试环境 系统: xp sp3 调试器 :od 1.10 sc_office_2003_pro 高手不要见笑,仅供小菜玩乐,有不对或不足的地方还请多多指教,不胜感激! 一:定位关键CALL 1. 因 ...
- C++ 宏定义的简单使用
1.定义常量 #define ARRMAX 50 int arr[ARRMAX]; (这种做法不如直接用const来直接定义常量.) 2.代替模板函数或者内联函数,将函数定义成宏.执行效率很快 #de ...
- 【Quartus警告】created implicit net for XXX.
[警告内容]Warning (10236): Verilog HDL Implicit Net warning at forward_replace.v(16): created implicit n ...
- python3基础05(有关日期的使用1)
#!/usr/bin/env python# -*- coding:utf-8 -*- import timefrom datetime import datetime,timedelta,timez ...