code forces 383 Arpa's loud Owf and Mehrdad's evil plan(有向图最小环)
Arpa's loud Owf and Mehrdad's evil plan
1 second
256 megabytes
standard input
standard output
As you have noticed, there are lovely girls in Arpa’s land.
People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi.

Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows.
The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time.
Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t ≥ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible.
Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i).
The first line of input contains integer n (1 ≤ n ≤ 100) — the number of people in Arpa's land.
The second line contains n integers, i-th of them is crushi (1 ≤ crushi ≤ n) — the number of i-th person's crush.
If there is no t satisfying the condition, print -1. Otherwise print such smallest t.
4
2 3 1 4
3
4
4 4 4 4
-1
4
2 1 4 3
1
In the first sample suppose t = 3.
If the first person starts some round:
The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1.
The process is similar for the second and the third person.
If the fourth person starts some round:
The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4.
In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa.
【分析】题目扯了一大堆,主要是这个意思。n个人,每个人有一个打电话的对象(可以是自己),设t为打电话的总次数,当一个人打电话时,他只打给自己的对象,然后他的对象接着打给自己的对象。。。问最小的t,使得从任意的x开始打电话,总共打了t次后到达y,这一轮结束,然后y接着打,总共打了t次后又回到x,(x与y可等)。
说白了,就是一个有向图,找遍所有的环,若环为偶数,则/2,求所有的最小公倍数。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 10000000000000
#define met(a,b) memset(a,b,sizeof a)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long ll;
using namespace std;
const int N = 1e2+;
const int M = 4e5+;
int dp[N][];
int n,sum[N],m=,p,k;
int Tree[N];
ll w[N][N],vis[N];
void Floyd(){
for(int k=;k<=n;k++){
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(w[i][k]!=inf&&w[k][j]!=inf&&w[i][j]>w[i][k]+w[k][j]){
w[i][j]=w[i][k]+w[k][j];
}
}
}
}return;
}
int GCD(int minn,int maxn){
if(maxn%minn==)return minn;
else return GCD(min(minn,maxn%minn),max(minn,maxn%minn));
}
int main()
{
for(int i=;i<N;i++)for(int j=;j<N;j++)w[i][j]=inf;
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&k);
w[i][k]=;
}
Floyd();
ll ans=inf;
bool flag=false;
for(int i=;i<=n;i++){
if(w[i][i]<inf){
if(w[i][i]&){
if(!flag)ans=w[i][i],flag=true;
else ans=(ll)ans*(w[i][i]/GCD(min(w[i][i],ans),max(w[i][i],ans)));
}
else {
w[i][i]/=;
if(!flag)ans=w[i][i],flag=true;
else ans=(ll)ans*(w[i][i]/GCD(min(w[i][i],ans),max(w[i][i],ans)));
}
}
else if(w[i][i]==inf){
ans=inf;
break;
}
}
if(ans==inf)puts("-1");
else printf("%lld\n",ans);
return ;
}
code forces 383 Arpa's loud Owf and Mehrdad's evil plan(有向图最小环)的更多相关文章
- 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 ...
- 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 ...
- 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个位置每个位置标着一个数表示这个位置下一步能到哪个 ...
- 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] 个人,所以如果第 ...
- 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或者是 ...
- 思维题--code forces round# 551 div.2
思维题--code forces round# 551 div.2 题目 D. Serval and Rooted Tree time limit per test 2 seconds memory ...
随机推荐
- Windows 7下硬盘安装Ubuntu 14.04图文教程
http://www.linuxidc.com/Linux/2014-04/100369.htm
- linux环境下安装tomcat并配置tomcat日志分割
1.直接解压apache-tomcat-7.0.69.tar.gz 存放在/home目录下 根据需要自定义tomcat名称 mv apache-tomcat-7.0.69 Tomcat7 2.解压cr ...
- SQL简单语法
(1)select SELECT 列名称 FROM 表名称 (2)distinct SELECT DISTINCT 列名称 FROM 表名称 SELECT * FROM 表名称 (3)where SE ...
- Android 图片浏览器 从原来位置放大至全屏显示
android 图片浏览器 特点: 1.从网络加载图片,只需要传图片地址数组即可 2.点击图片,从原来位置放大至全屏 3.支持手势操作 4.完全自定义布局 项目源码请到GitHub下载:https:/ ...
- elasticsearch,python包pyes进行的处理
elasticsearch:高性能搜索引擎,官网:https://www.elastic.co/products/elasticsearch/ 对于它相信大家都不陌生,es的使用已经广泛存在 各大网站 ...
- [转载]盒模型display:-webkit-box;的使用
http://blog.sina.com.cn/s/blog_62161a4d01019b7g.html box-flex是css3新添加的盒子模型属性,它的出现可以解决我们通过N多结构.css实现的 ...
- Java并发编程学习笔记(一)——线程安全性
主要概念:线程安全性.原子性.原子变量.原子操作.竟态条件.复合操作.加锁机制.重入.活跃性与性能. 1.当多个线程访问某个状态变量并且其中有一个线程执行写入操作时,必须采用同步机制来协同这些线程对变 ...
- 将NSString转换成UTF8编码的NSString
在使用网络地址时,一般要先将url进行encode成UTF8格式的编码,否则在使用时可能报告网址不存在的错误,这时就需要进行转换 下面就是转换函数: NSString *urlString= [NSS ...
- js 正则表达式 转至(七郎's Blog)
//匹配帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线 var re =new RegExp("^[a-zA-Z][a-zA-Z0-9_]{5,19}$"); if( ...
- 整数中1出现的次数(从1到n整数中1出现的次数)
题目:求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了.AC ...