题目地址:http://codeforces.com/contest/742/problem/C
题意:我看了好多别人的博客的对于题意的理解,结合他们的代码我认为这道题的意思应该是每个数要都能经过t步以后到达一个点,再经过t步到达起点。
思路:看这些数能否构成一个或多个环(包括自环),如果存在不能成环的数就输出-1,否则一定存在一个最小的t。

如果当前环中元素的个数为奇数,则 x->x->x 当前的t=元素个数
如果当前环中元素的个数为偶数,则 x->y->x (y为中间的数)当前的t=元素个数/2
然后再把每个环的 t 求出他们的最小公倍数就是最小的 t,对于每个环都满足的t。

#include <iostream>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <cstdio>
#include <algorithm>
#define LL long long
#define N 110
#define M 50010
#define inf 0x3f3f3f3f
using namespace std;
const LL mod = 1e9 + 7;
const double eps = 1e-9;
LL num[N], pre[N];
LL gcd(LL a, LL b) {return b == 0 ? a : gcd(b, a%b);
}
LL lcm(LL a, LL b) {return a*b / gcd(a, b);
}
int main() {cin.sync_with_stdio(false);LL n, flag, ans, cnt, sum;while (cin >> n) {memset(pre, 0, sizeof(pre));for (int i = 1; i <= n; i++) {cin >> num[i];}flag = 1;sum = 0;for (int i = 1; i <= n&&flag; i++) {if (!pre[i]) {ans = i;cnt = 0;while (pre[num[ans]] == 0) {pre[num[ans]] = ans;ans = num[ans];cnt++;}if (pre[ans] == 0 || ans != i) {flag = 0;}else {if (cnt % 2) {if (sum == 0) {sum = cnt;}else {sum = lcm(sum, cnt);}}else {if (sum == 0) {sum = cnt / 2;}else {sum = lcm(sum, cnt / 2);}}}}}if (flag) {cout << sum << endl;}else {cout << -1 << endl;}}return 0;
}

Codeforces 742 C Arpa's loud Owf and Mehrdad's evil plan相关推荐

  1. codefores741A Arpa's loud Owf and Mehrdad's evil plan(图找环)

    题意: 对于给定的一个数列a[n],如果现在点i,那么下一个点就在a[i],现在问给定n个点,找到一个最小的t,使得对于从任意一个点x出发,经过t次之后,到达的点y, 使得点y经过t次之后会回到点x. ...

  2. Codeforces-741A-Arpa's loud Owf and Mehrdad's evil plan(找有向图环及最大公倍数计算)

    转载于:https://www.cnblogs.com/GrowingJlx/p/6642661.html

  3. Codeforces 741 D - Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths

    D - Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths 思路: 树上启发式合并 从根节点出发到每个位置的每个字符的奇偶性记为每个位 ...

  4. 【Codeforces 741 B. Arpa's weak amphitheater and Mehrdad's 】+ 并查集 + 01背包

    B. Arpa's weak amphitheater and Mehrdad's valuable Hoses time limit per test 1 second memory limit p ...

  5. Codeforces 742B B. Arpa’s obvious problem and Mehrdad’s terrible solution

    B. Arpa's obvious problem and Mehrdad's terrible solution time limit per test 1 second memory limit ...

  6. 【codeforces 742A】Arpa’s hard exam and Mehrdad’s naive cheat

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  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 742B 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 solutio ...

  9. Codeforces 742A Arpa’s hard exam and Mehrdad’s naive cheat 打表+水题

    A. Arpa's hard exam and Mehrdad's naive cheat time limit per test 1 second memory limit per test 256 ...

最新文章

  1. TextView设置文字包含中英文时自动换行问题的终极解决方案
  2. IAP升级功能编写初期的一些困惑与疑问---完成功能后的总结
  3. 写文件函数 Linux C fwrite,C文件读写函数介绍(转)
  4. unity2019,打包APK时的gradle错误问题
  5. Java获取文件路径获取访问路径、域名、项目名、请求入参
  6. 关于python类的继承正确的说法是_2017美团点评的运维岗校招笔试题,测测你会几题?...
  7. Oracle入门(五)之基本命令操作
  8. jms java client mq_将Java客户端(JMS)连接到IBM MQ时出现问题
  9. 蓝桥杯 基础练习 时间转换
  10. GIS案例练习-----------第五天
  11. 数据定义约束-数据库习题
  12. TI单芯片毫米波雷达代码走读(十八)—— 多普勒维CA-CFAR检测之C代码实现
  13. Windows NT 架构
  14. gin html模板调用js函数
  15. 3GPP TS 29244-g30 中英文对照 | 6.2.6 PFCP Association Setup Procedure
  16. js[中英文排序-获取中文拼音]
  17. 【Unity3D】粒子系统ParticleSystem
  18. 【苹果推】imessage群发条件文档(推送证书)开发工具Pushnotification
  19. 市场运营:App 渠道追踪的5种方法以及渠道数据分析的两大思路
  20. FFT学习应用之Matlab程序(频率泄露实例)

热门文章

  1. ABTest样本量计算
  2. vue获取地理位置自动定位---腾讯地图
  3. vs2017/2019中pch.h和pch.cpp是个什么东西(附加,如何删除)
  4. MySQL学习笔记——如何建立本地连接及简单的MySQL语句的使用
  5. 遇上老赖,欠款不还,怎么办? 东莞的一家光电设备厂商向我单位永坤电机在四月份采购了五台微型齿轮减速电机200W的,货款一共才2980元,采购前协商好的收到货后便当天开支票寄来给我。原本单位有规定是要求
  6. 计算机没有外审的核心期刊,期刊外审太快是不是不好_核心期刊外审一般多长时间_论文外审是什么意思...
  7. [js] cloneNode
  8. 创建文件夹mkdir
  9. yum安装软件失败:rpmdb: BDB0113 Thread/process 8178/139732838348864 failed
  10. 定时任务Quart的三种实现方法