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,它才有可能形成环,否则会不能形成环。

 #include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
#define N 100010
typedef long long LL;
int deg[];
int a[]; LL solve(int n) {
LL ans = ;
memset(deg, , sizeof(deg));
for(int i = ; i <= n; i++) {
if(deg[i] == ) { // 经过的点的路径形成一个环
LL tmp = ;
int x = a[i];
deg[i] = ;
while(!deg[x]) {
deg[x] = ;
x = a[x];
tmp++;
}
ans = ans / __gcd(ans, tmp) * tmp;
}
}
if(!(ans & )) ans /= ;
return ans;
} int main()
{
int n;
scanf("%d", &n);
for(int i = ; i <= n; i++) {
scanf("%d", a+i);
deg[a[i]]++;
}
bool flag = true;
for(int i = ; i <= n; i++) {
if(!deg[i]) {
flag = false;
break;
}
}
if(!flag) puts("-1");
else printf("%I64d\n", solve(n));
return ;
}

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. Android --账户注销

    参考博客:android如何实现注销功能 Intent logoutIntent=new Intent(SettingActivity.this,LoginActivity.class); //在执行 ...

  2. JavaScript权威指南(第六版)--JavaScript概述 DEMO

    //所有在双斜线之后的内容都属于注释 //仔细阅读这里的注释:它们对JavaScript代码做了解释 //变量是表示值的一个符号名字 //变量是通过var关键字声明的 var x; //声明一个变量x ...

  3. CNContact对通讯录的基本使用(第二篇)

    /** * 注意:iOS9才有能使用 * 首先在工程里导入ContactsUI.framework和Contacts.framework两个框架 * * * 源代码的链接地址 * 链接: http:/ ...

  4. PAT 解题报告 1004. Counting Leaves (30)

    1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...

  5. Leetcode: Trapping Rain Water II

    Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...

  6. Summary: Merge Sort of Array && 求逆序对

    常用算法(后面有inplace版本): package ArrayMergeSort; import java.util.Arrays; public class Solution { public ...

  7. struts2 标签为简单标签

    <s:form method="post" action="" theme="simple"> <s:textfield ...

  8. 树形dp Anniversary party(HDU1520)

    题意:给出一棵树,(上下级关系)每个节点都有一个权值,要求选出一些节点满足这些节点任意连个点都不是直接的上下级关系,可以得到的最大权值是多少? 分析:对于每个点有两个状态选或者不选,用状态数组dp[u ...

  9. Python学习总结9:目录操作方法汇总

    os.getcwd():得到当前工作目录,即当前Python脚本工作的目录路径os.listdir():返回指定目录下的所有文件和目录名os.remove():函数用来删除一个文件os.removed ...

  10. LED流水灯(二)

    记住看汇编的时候是红在上面 黑色在下面 startup.s 程序 ; MDK跑马灯实验; PRESERVE8               // 字节对齐关键词 ,汇编有8位对齐的要求,要添加 AREA ...