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 ...
随机推荐
- UIView点击事件。弹出视图,背景虚化。
@interface CountryViewController //背景 @property (strong, nonatomic) UIView *BackView; end //设置背景虚化 - ...
- HeadFirst 设计模式
一.设计原则 封装变化 多用组合,少用继承 针对接口编程,不针对实现编程 为交互对象之间的松紧耦合设计而努力 对扩展开放,都修稿关闭 依赖抽象,不要依赖具体类 最少知识原则:之和朋友交谈 好莱坞原则: ...
- 学习Find函数和select
Find函数其实就类似于在excel按下Ctrl+F出现的查找功能:在某个区域中查找你要找的字符,一旦找到就定位到第一个对应的单元格.所以Find函数的返回值是个单元格,也就是个range值.举例,s ...
- Spark MLlib 之 Naive Bayes
1.前言: Naive Bayes(朴素贝叶斯)是一个简单的多类分类算法,该算法的前提是假设各特征之间是相互独立的.Naive Bayes 训练主要是为每一个特征,在给定的标签的条件下,计算每个特征在 ...
- 支付宝即时到账API,网站收到回调页面,输出验证失败,log中responseTxt=错误的问题
看了网上无外乎这三种原因导致: 1.参数编码问题 2.参数多余问题 3.网络延时问题 以上三种可以参考这个网站给出的解决办法:http://blog.csdn.net/zj53hao/article/ ...
- 《C++primer》v5 第7章 类 读书笔记 习题答案
7.1.7.2.7.3 #include<iostream> #include<cstdio> #include<vector> #include<strin ...
- php大力力:技术排错过程中,关键点总结和心情历程(2015-10-19)
9:40 2015/10/19技术排错过程中,关键点总结和心情历程 有一个按照标题进行内容分类的函数似乎不起作用,这叫人沮丧. 在页面显示图片地址时候,在源系统和目标系统中,包含图片地址的页面代码格式 ...
- Kali安装小问题解决及一些设置
昨天按照完Kali linux之后,更改了镜像源, 然后运行了 apt-get update && apt-get dist-upgrade 发现要 等大约两个小时才能跟新完毕.. 于 ...
- thoughtworks编程题
微博看到vczh分享的thoughtworks的一道题目https://www.jinshuju.net/f/EGQL3D,代码写完之后才得知这个公司并不是我想的那样美好. 题目: FizzBuzzW ...
- Velocity快速入门教程-脚本语法详解(转)
1.变量 (1)变量的定义: #set($name = "hello") 说明:velocity中变量是弱类型的. 当使用#set 指令时,括在双引号中的字面字符串将解析 ...