【codeforces 742C】Arpa's loud Owf and Mehrdad's evil plan
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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).
Input
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.
Output
If there is no t satisfying the condition, print -1. Otherwise print such smallest t.
Examples
input
4
2 3 1 4
output
3
input
4
4 4 4 4
output
-1
input
4
2 1 4 3
output
1
Note
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.
【题目链接】:http://codeforces.com/contest/742/problem/C
【题解】
先用个while 搞出所有的环的长度;
需要注意的就是;
环的长度如果小于等于2;则t为什么值都是可以的;所以不用管这种环;
然后啊长度大于2的环搞出来就好;
当然对于长度大于2的环还有特殊的情况;
就是这个长度为偶数;
那么这个人可以从最左边到中间,然后再到最右边(然后又回到1);这种情况我一开始漏掉了;真的是蠢死了;
显然这样可以让t更小一点;
所以对于长度大于2的偶数直接除2;
然后对上面需要处理的长度;求他们的最小公倍数就可以了;
每次都差一点,大概就是我的缺陷吧。坚持不下去?
【完整代码】
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
void rel(LL &r)
{
r = 0;
char t = getchar();
while (!isdigit(t) && t!='-') t = getchar();
LL sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
}
void rei(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)&&t!='-') t = getchar();
int sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
}
const int MAXN = 100+30;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
int n;
int a[MAXN];
bool flag[MAXN];
vector <int> g;
map <int,int> dic;
int main()
{
//freopen("F:\\rush.txt","r",stdin);
scanf("%d",&n);
rep1(i,1,n)
cin>>a[i];
memset(flag,false,sizeof(flag));
rep1(i,1,n)
if (!flag[i])
{
flag[i] = true;
int s = i,t = a[i],dd = 1;
while (!flag[t])
{
flag[t] = true;
t = a[t];
dd++;
}
if (t!=s)
{
puts("-1");
return 0;
}
if (dd>2)
{
if ((dd&1)==0)
dd/=2;
g.pb(dd);
}
}
LL ans;
int len = g.size();
if (len==0)
ans = 1;
else
{
LL temp = g[0];
rep1(i,1,len-1)
{
LL temp1 = g[i];
LL gg = __gcd(temp,temp1);
temp = (temp*temp1)/gg;
}
ans = temp;
}
cout << ans << endl;
return 0;
}
【codeforces 742C】Arpa's loud Owf and Mehrdad's evil plan的更多相关文章
- 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] 个人,所以如果第 ...
- 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 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 742B】Arpa’s obvious problem and Mehrdad’s terrible solution
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
随机推荐
- Spring源码分析专题 —— 阅读指引
阅读源码的意义 更深入理解框架原理,印象更深刻 学习优秀的编程风格.编程技巧.设计思想 解决实际问题,如修复框架中的bug,或是参考框架源码,结合实际业务需求编写一个独有的框架 阅读源码的方法 首先是 ...
- Codeforces Round #194 (Div. 2) 部分题解
http://codeforces.com/contest/334 A题意:1-n^2 平均分成 n 份,每份n个数,且和相同 解法 : 构造矩阵1-n^2的矩阵即可 ][]; int main() ...
- 将二级目录下的文件合并成一个文件的Python小脚本
这个小程序的目的是将二级目录下的文件全部合并成一个文件(其实几级目录都可以,只要做少许改动) #coding:utf8 import sys, os def process(path): new_fi ...
- 一起talk C栗子吧(第九十 三回:C语言实例--进程间通信之临界资源)
各位看官们.大家好,前面章回中咱们说的是使用信号和管道进行进程间通信的样例.这一回咱们说的样例是:进程间通信之临界资源.闲话休提,言归正转.让我们一起talk C栗子吧! 我们首先介绍一下,什么是临界 ...
- Myeclipse的默认工作区间怎么恢复提示框?
好久一直使用默认工作空间.现在,回过头来想让那个提示框回来. 该如何做呢? 1.找到我们的myeclipse安装目录下的 2.false是关闭. 3.改成true 4.同时,新增新的工作区间和之前旧的 ...
- 将Maven的Java Project转换或修改为Web Project
将Maven的Java Project转换为Web Project关键是需要了解Eclipse和MyEclipse的工程中如下文件.classpath..project. .mymetadata和s ...
- Day1:字符编码
一.ASCII码 ASCII(American Standard Code for Information Interchange,美国标准信息交换代码),8位,一个字节,最多只能表示255个符号. ...
- WinPcap 简介
WinPcap(windows packet capture) 它包括一个核心态的包过滤器NPF,一个底层的动态链接库(packet.dll)和一个高层的不依赖于系统的库(wpcap.dll). [w ...
- 让自己的软件实现拖拽打开文件(覆盖WM_DROPFILES,使用DragQueryFile,DragFinish API函数)
作者: 帅宏军 //声明 protected procedure WMDROPFILES(var Msg : TMessage); message WM_DROPFILES; --------- ...
- POJ 2362 Square DFS
传送门:http://poj.org/problem?id=2362 题目大意: 给一些不同长度的棍棒,问是否可能组成正方形. 学习了写得很好的dfs 赶紧去玩博饼了.....晚上三个地方有约.... ...