题意:

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

要点:

一看就是图找环,找到所有独立的环对应的节点数,对所有节点数求最小公倍数就是结果。有个地方要注意就是,当节点数是偶数时,比如1->2->3->4->1,这种情况要将节点数/2计算,因为比如1到达3要2次,3到达1也是两次,肯定比从1绕一圈再到1要小,奇数没有办法。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<map>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 105;
int a[N], vis[N];
ll b[N];int dfs(int u, int s, int l)//有向图找环
{if (u == s) {l = l + 1;return l;}if (vis[u] && u != s) {return -1;}vis[u] = 1;if (a[u] == s)return dfs(a[u], s, l);elsereturn dfs(a[u], s, l + 1);
}
ll gcd(ll a, ll b)
{if (b == 0) return a;return gcd(b, a%b);
}int main()
{int n;scanf("%d", &n);for (int i = 1; i <= n; i++)scanf("%d", &a[i]);memset(vis, 0, sizeof(vis));int cnt = 0;bool flag = true;for (int i = 1; i <= n; i++){if (vis[i])continue;int temp = dfs(a[i], i, 1);if (temp != -1){if (temp % 2 == 0)temp /= 2;b[cnt++] = temp;}else{flag = false;break;}}if (!flag)printf("-1\n");else{ll lcm, c = b[0], e;for (int i = 0; i < cnt; i++)//计算lcm{e = gcd(c, b[i]);lcm = c*b[i] / e;c = lcm;}printf("%I64d\n", lcm);}return 0;
}

转载于:https://www.cnblogs.com/seasonal/p/10343651.html

codefores741A Arpa's loud Owf and Mehrdad's evil plan(图找环)相关推荐

  1. Codeforces 742 C Arpa's loud Owf and Mehrdad's evil plan

    题目地址:http://codeforces.com/contest/742/problem/C 题意:我看了好多别人的博客的对于题意的理解,结合他们的代码我认为这道题的意思应该是每个数要都能经过t步 ...

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

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

  3. CF741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths

    CF741D Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths 好像这个题只能Dsu On Tree? 有根树点分治 统计子树过x的 ...

  4. 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 思路: 树上启发式合并 从根节点出发到每个位置的每个字符的奇偶性记为每个位 ...

  5. 【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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

最新文章

  1. Python入门(一)数据类型、循环语句
  2. 共享数字经济之光!世界互联网大会重磅发布“30位新生代数字经济人才”
  3. Sql server 获得某一部门下的所有子部门。根据子部门获得它的上级部门。
  4. SDUT_2121数据结构实验之链表六:有序链表的建立 (对建立的单项链表结构进行排序)
  5. ESP8266/ESP32 NVS 基本操作
  6. ORACLE如何一次性修改用户连接数?
  7. 关于JVM的几个垃圾收集算法思想
  8. fedora 安装google浏览器失败,报错
  9. python分布式多进程框架 Ray
  10. [译] A Neural Algorithm of Artistic Style--图片风格化
  11. android+解锁工具,安卓手机解锁助手 (A Unlock Tool)
  12. 更好用计算机怎么打,电脑输入法哪个好用(最受欢迎的7大输入法对比详情)...
  13. 港股通会成资金外流黑洞?伪逻辑不值一驳
  14. 直观说明Hadoop是什么?有什么作用?
  15. 使用g++创建动态库和静态库及其相关探索
  16. 消息通知系统详解2---后端设计
  17. Java实现 LeetCode 420 强密码检验器
  18. Snail OS 0.03的thread目录是内核线程
  19. 2022年计算机四级考试强化训练题及答案(多选题)
  20. 【转】PIC单片机的命名规则

热门文章

  1. GDCM:gdcm::DataSet的测试程序
  2. 基于Boost::beast模块的异步WebSocket服务器
  3. VTK:Shaders之BozoShaderDemo
  4. QDoc通用配置变量
  5. vector和deque的区别?
  6. QT的QVBoxLayout类的使用
  7. 经典C语言程序100例之二一
  8. C语言求最大公约数与最小公倍数
  9. php不支持定时器么,PHP没有定时器?
  10. 细粒度图像分类_支付宝AI大幅提升细粒度图像分类识别精度:一眼看穿万物细微差异...