题目链接: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. 创建软RAID5

    一  创建4块硬盘组软  RAID5  新增四块20G的新硬盘,将四块硬盘分别分区,全部大小都分为一个区,并改  id  为 “ fd ”      1创建阵列mdadm -Cv /dev/md5 - ...

  2. 01-Spring Security框架学习

    目录 01-Spring Security框架学习 简介 Spring Security 是什么 Spring Security 解决那些问题 Spring Security 的优点 历史背景 Spr ...

  3. CodeForces 29D Ant on the Tree

    洛谷题目页面传送门 & CodeForces题目页面传送门 题意见洛谷里的翻译. 这题有\(\bm3\)种解法,但只有一种是正解(这不是废话嘛). 方法\(\bm1\):最近公共祖先LCA(正 ...

  4. 8.9 day30 并发编程 进程理论 进程方法 守护进程 互斥锁

    多道技术 1.空间上的复用 多个程序共用一套计算机硬件 多道技术原理 2.时间上的复用 ​ 切换+保存状态 ​ 1.当一个程序遇到IO操作 操作系统会剥夺该程序的CPU执行权限( 提高了CPU的利用率 ...

  5. Kubernetes-保障集群内节点和网络安全

    13.1.在pod中使用宿主节点的Linux命名空间 13.1.1.在pod中使用宿主节点的网络命名空间 在pod的yaml文件中就设置spec.hostNetwork: true 这个时候pod使用 ...

  6. jupyter iPython web sit use 1

    I want Jupyter to print all the interactive output without resorting to print, not only the last res ...

  7. windows下搭建syslog服务器及基本配置

    一.环境 windows7 64位+ kiwi_syslog_server_9.5.0 kiwi_syslog百度云下载地址: 链接: https://pan.baidu.com/s/1EpPBNsL ...

  8. 搭建nuget 服务器

    前言 搭建nuget服务器,这是上家公司进行类库管理的方式,其实优点很明显, 1.代码保密 2.代码重复利用效率高,这样不管任何项目只要知道nuget服务器地址就能直接调用 3.可进行版本任意切换提高 ...

  9. Laravel框架内实现api文档:markdown转为html

    前后端分离的工作模式于今是非常流行了,前后端工作的对接,就离开不了API文档的辅助. 根据自己以往的工作经历,以及了解的一些资讯,API文档的建立,无非以下几种方式: 1. word文档模板 2. 第 ...

  10. Scrapy框架解读

    1. Scrapy组件a. 主体部分i. 引擎(Scrapy):处理整个系统的数据流处理,触发事务(框架核心)ii. 调度器(Scheduler):1) 用来接受引擎发过来的请求, 压入队列中, 并在 ...