http://codeforces.com/problemset/problem/741/A

题意:有N个人,第 i 个人有一个 a[i],意味着第 i 个人可以打电话给第 a[i] 个人,所以如果第 i 个人打电话出去,那么序列是 a[i], a[a[i]], a[a[a[i]]]……,打了 t 次电话后终点为y,那么从 y 也要打 t 次电话之后终点为 i,问最少要打多少次电话才能让所有人满足这样的条件。不存在输出 -1.

思路:这样的一个个序列就是一个环,因为要让所有人满足这个条件,所以 x * m = t , x 是每一个环节自身的长度, m 是一个整数, t 是最后的答案,那么求出所有环的最小公倍数,就是最后的答案了。如果环的长度为偶数,那么存在着一个 y 可以使得 x 能打电话给 y 的同时, y 也能打电话给 x,所以这样长度可以除以2。判断存不存在的话,只有每一个点的入度都不为 0,它才有可能形成环,否则会不能形成环。

  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <cstring>
  5. #include <string>
  6. #include <cmath>
  7. #include <queue>
  8. #include <vector>
  9. using namespace std;
  10. #define INF 0x3f3f3f3f
  11. #define N 100010
  12. typedef long long LL;
  13. int deg[];
  14. int a[];
  15.  
  16. LL solve(int n) {
  17. LL ans = ;
  18. memset(deg, , sizeof(deg));
  19. for(int i = ; i <= n; i++) {
  20. if(deg[i] == ) { // 经过的点的路径形成一个环
  21. LL tmp = ;
  22. int x = a[i];
  23. deg[i] = ;
  24. while(!deg[x]) {
  25. deg[x] = ;
  26. x = a[x];
  27. tmp++;
  28. }
  29. ans = ans / __gcd(ans, tmp) * tmp;
  30. }
  31. }
  32. if(!(ans & )) ans /= ;
  33. return ans;
  34. }
  35.  
  36. int main()
  37. {
  38. int n;
  39. scanf("%d", &n);
  40. for(int i = ; i <= n; i++) {
  41. scanf("%d", a+i);
  42. deg[a[i]]++;
  43. }
  44. bool flag = true;
  45. for(int i = ; i <= n; i++) {
  46. if(!deg[i]) {
  47. flag = false;
  48. break;
  49. }
  50. }
  51. if(!flag) puts("-1");
  52. else printf("%I64d\n", solve(n));
  53. return ;
  54. }

Codeforces 741A:Arpa's loud Owf and Mehrdad's evil plan(LCM+思维)的更多相关文章

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

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

  6. 【codeforces 742C】Arpa's loud Owf and Mehrdad's evil plan

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

  7. Codeforces Round #383 (Div. 2) C. Arpa's loud Owf and Mehrdad's evil plan(dfs+数学思想)

    题目链接:http://codeforces.com/contest/742/problem/C 题意:题目比较难理解,起码我是理解了好久,就是给你n个位置每个位置标着一个数表示这个位置下一步能到哪个 ...

  8. 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或者是 ...

  9. codeforces 741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(启发式合并)

    codeforces 741D Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths 题意 给出一棵树,每条边上有一个字符,字符集大小只 ...

随机推荐

  1. SocketServer model_use

    #!/usr/bin/env python #-*- coding:utf- -*- import SocketServer class MySocker(SocketServer.BaseReque ...

  2. JavaScript实现,判断一个点是否在多边形内

    //定义点的结构体 function point(){ this.x=0; this.y=0; } //计算一个点是否在多边形里,参数:点,多边形数组 function PointInPoly(pt, ...

  3. strip和stripe

  4. C# 6.0的新变化

    自动属性初始化 (Initializers for auto-properties) 以前我们是这么写的 为一个默认值加一个后台字段是不是很不爽,现在我们可以这样写 只读属性的初始化(Getter-o ...

  5. jdk安装与环境变量配置(一劳永逸)

    换了一个项目组做新的项目,新的机器,又得重新打环境,懒得去百度下,这里做个备份,下回直接看就行,如下 进行java开发,首先要安装jdk,安装了jdk后还要进行环境变量配置: 1.下载jdk(http ...

  6. Java基础之写文件——在通道写入过程中的缓冲区状态(BufferStateTrace)

    控制台程序,在Junk目录中将字符串“Garbage in, garbage out\n”写入到名为charData.txt的文件中. import static java.nio.file.Stan ...

  7. HashedWheelTimer

    HashedWheelTimer 是根据 Hashed and Hierarchical Timing Wheels: Data Structuresfor the Efficient Impleme ...

  8. GTA项目 一, 包装外部WebService

    外部WebService返回的是xml太重了. 而JSON是web的新标准.所以要包装一下. 使用NewtonSoft.JSON的dll里面的JsonConvert.SerializeXmlNode方 ...

  9. oracle和sql server的区别(1)

    A.instance和database 1.从oracle的角度来说,每个instance对应一个database.有时候多个instance对应一个database(比如rac环境).有自己的Sys ...

  10. [转] java编程规范

    原文链接: 资料推荐--Google Java编码规范 之前已经推荐过Google的Java编码规范英文版了: http://google-styleguide.googlecode.com/svn/ ...