题目链接:http://codeforces.com/contest/742/problem/C

题意:题目比较难理解,起码我是理解了好久,就是给你n个位置每个位置标着一个数表示这个位置下一步能到哪个位置,然后要求

求一个数t,保证经过t步x能到达y,而且y经过t步能到x,而且是所有点都要满足。

这题只有一种情况是无法得到结果的,那就是有两个以上的点通向同一个位置。其实题目中也略微有点提示。

然后剩下的就差不多靠暴力解决,但还是有点技巧的。

注意要满足题目中要求其实就是找单向联通块,如果联通数为偶数那么就可以折半,否则就为循环到自己。

然后求各个联通块的gcd即可。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int M = 110;
int gcd(int a , int b) {
return b ? gcd(b , a % b) : a;
}
int vis[M] , Next[M] , have[M];
int dfs(int pos) {
if(vis[Next[pos]])
return 0;
vis[Next[pos]] = 1;
return 1 + dfs(Next[pos]);
}
int main() {
int n;
scanf("%d" , &n);
int flag = 0;
for(int i = 1 ; i <= n ; i++) {
scanf("%d" , &Next[i]);
have[Next[i]]++;
if(have[Next[i]] > 1)
flag = 1;
}
if(flag == 1) {
printf("-1\n");
}
else {
int ans = 1;
for(int i = 1 ; i <= n ; i++)
vis[i] = 0;
for(int i = 1 ; i <= n ; i++) {
if(vis[i])
continue;
int l = dfs(i);
if(l % 2 == 0)
l /= 2;
ans = (ans * l) / gcd(ans , l);
}
printf("%d\n" , ans);
}
return 0;
}

Codeforces Round #383 (Div. 2) C. Arpa's loud Owf and Mehrdad's evil plan(dfs+数学思想)的更多相关文章

  1. Codeforces Round #383 (Div. 2) C. Arpa's loud Owf and Mehrdad's evil plan —— DFS找环

    题目链接:http://codeforces.com/contest/742/problem/C C. Arpa's loud Owf and Mehrdad's evil plan time lim ...

  2. Codeforces Round #383 (Div. 2)C. Arpa's loud Owf and Mehrdad's evil plan

    C. Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 me ...

  3. C. Arpa's loud Owf and Mehrdad's evil plan DFS + LCM

    http://codeforces.com/contest/742/problem/C 首先把图建起来. 对于每个a[i],那么就在i --- a[i]建一条边,单向的. 如果有一个点的入度是0或者是 ...

  4. code forces 383 Arpa's loud Owf and Mehrdad's evil plan(有向图最小环)

    Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 megab ...

  5. Arpa's loud Owf and Mehrdad's evil plan

    Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 megab ...

  6. C. Arpa's loud Owf and Mehrdad's evil plan

    C. Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 me ...

  7. Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses —— DP(01背包)

    题目链接:http://codeforces.com/contest/742/problem/D D. Arpa's weak amphitheater and Mehrdad's valuable ...

  8. Codeforces Round #383 (Div. 2) B. Arpa’s obvious problem and Mehrdad’s terrible solution —— 异或

    题目链接:http://codeforces.com/contest/742/problem/B B. Arpa's obvious problem and Mehrdad's terrible so ...

  9. Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses(分组背包+dsu)

    D. Arpa's weak amphitheater and Mehrdad's valuable Hoses Problem Description: Mehrdad wants to invit ...

随机推荐

  1. RabbitMQ(四):使用Docker构建RabbitMQ高可用负载均衡集群

    本文使用Docker搭建RabbitMQ集群,然后使用HAProxy做负载均衡,最后使用KeepAlived实现集群高可用,从而搭建起来一个完成了RabbitMQ高可用负载均衡集群.受限于自身条件,本 ...

  2. MySQL 之 Explain 输出分析

    ​MySQL 之 Explain 输出分析 背景 前面的文章写过 MySQL 的事务和锁,这篇文章我们来聊聊 MySQL 的 Explain,估计大家在工作或者面试中多多少少都会接触过这个.可能工作中 ...

  3. ipad pro 为什么不行

    TalkingData公布的数据显示,iPad Pro在中国发行首月的销量仅为49 300台,而此前iPad Air 2发行首月后销量曾高达55.7万台.那么到底是什么原因,让这个被寄予厚望的iPad ...

  4. Tomcat+MySQL常见调优参数

    一.Tomcat 调优 (一).Tomcat内存优化 参数一: vim /tomcat/bin/catalina.sh CATALINA_OPTS="-server -Xms128m -Xm ...

  5. 记我的一次 Java 服务性能优化

    背景 前段时间我们的服务遇到了性能瓶颈,由于前期需求太急没有注意这方面的优化,到了要还技术债的时候就非常痛苦了. 在很低的 QPS 压力下服务器 load 就能达到 10-20,CPU 使用率 60% ...

  6. iview自定义实现多级表头

    最近更新: 2018-07-19 注意:最新版iview已经提供多级表头功能 参考 原理:利用多个Table组件通过显示和隐藏thead和tbody来拼接表格(较粗暴) html <div st ...

  7. C++学习想法

    今天是周一,今天做早操的时候舍友说准备买一本C++基础的书.我觉得这样的想法很好,突然想到自己最近几天因为自己私人原因事情很忙,蛋这不能成为我不学C++的理由.所以我在这规划了我这一周的学习进程.首先 ...

  8. Flink 源码解析 —— 深度解析 Flink 序列化机制

    Flink 序列化机制 https://t.zsxq.com/JaQfeMf 博客 1.Flink 从0到1学习 -- Apache Flink 介绍 2.Flink 从0到1学习 -- Mac 上搭 ...

  9. C# 发送邮件 并自定义邮件格式

    话不多说,直接上代码 //邮件提醒 string Body = @"Dear " + list.Rows[i]["people"] + ":<b ...

  10. 100天搞定机器学习|day39 Tensorflow Keras手写数字识别

    提示:建议先看day36-38的内容 TensorFlow™ 是一个采用数据流图(data flow graphs),用于数值计算的开源软件库.节点(Nodes)在图中表示数学操作,图中的线(edge ...