Codeforces 741A:Arpa's loud Owf and Mehrdad's evil plan(LCM+思维)
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+思维)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 【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 ...
- Codeforces Round #383 (Div. 2) C. Arpa's loud Owf and Mehrdad's evil plan(dfs+数学思想)
题目链接:http://codeforces.com/contest/742/problem/C 题意:题目比较难理解,起码我是理解了好久,就是给你n个位置每个位置标着一个数表示这个位置下一步能到哪个 ...
- 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或者是 ...
- 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 题意 给出一棵树,每条边上有一个字符,字符集大小只 ...
随机推荐
- SocketServer model_use
#!/usr/bin/env python #-*- coding:utf- -*- import SocketServer class MySocker(SocketServer.BaseReque ...
- JavaScript实现,判断一个点是否在多边形内
//定义点的结构体 function point(){ this.x=0; this.y=0; } //计算一个点是否在多边形里,参数:点,多边形数组 function PointInPoly(pt, ...
- strip和stripe
- C# 6.0的新变化
自动属性初始化 (Initializers for auto-properties) 以前我们是这么写的 为一个默认值加一个后台字段是不是很不爽,现在我们可以这样写 只读属性的初始化(Getter-o ...
- jdk安装与环境变量配置(一劳永逸)
换了一个项目组做新的项目,新的机器,又得重新打环境,懒得去百度下,这里做个备份,下回直接看就行,如下 进行java开发,首先要安装jdk,安装了jdk后还要进行环境变量配置: 1.下载jdk(http ...
- Java基础之写文件——在通道写入过程中的缓冲区状态(BufferStateTrace)
控制台程序,在Junk目录中将字符串“Garbage in, garbage out\n”写入到名为charData.txt的文件中. import static java.nio.file.Stan ...
- HashedWheelTimer
HashedWheelTimer 是根据 Hashed and Hierarchical Timing Wheels: Data Structuresfor the Efficient Impleme ...
- GTA项目 一, 包装外部WebService
外部WebService返回的是xml太重了. 而JSON是web的新标准.所以要包装一下. 使用NewtonSoft.JSON的dll里面的JsonConvert.SerializeXmlNode方 ...
- oracle和sql server的区别(1)
A.instance和database 1.从oracle的角度来说,每个instance对应一个database.有时候多个instance对应一个database(比如rac环境).有自己的Sys ...
- [转] java编程规范
原文链接: 资料推荐--Google Java编码规范 之前已经推荐过Google的Java编码规范英文版了: http://google-styleguide.googlecode.com/svn/ ...