题目链接:http://codeforces.com/contest/742/problem/C

C. Arpa's loud Owf and Mehrdad's evil plan
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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 - 1times)
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 yx 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.

题解:

错误的做法:

本以为t最大不会超过n,所以就用p[i][j]记录,记录距离结点i,j个距离的是哪个顶点。然后再依次枚举j,找到合适的t。

后来发现:t可以大于n,所以此方法失败。

正确的做法:

t为所有环的最小公倍数。(当环长为奇数时,直接取环长;当环长为偶数时,取环长的一半,因为可以刚好走到正对面)

错误做法:

 #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = +; int n, a[maxn], p[maxn][maxn]; void dfs(int f, int u, int k)
{
if(k>n) return;
p[f][k] = u;
dfs(f,a[u], k+);
} int main()
{
cin>>n;
for(int i = ; i<=n; i++)
cin>>a[i];
for(int i = ; i<=n; i++)
dfs(i,a[i],); int ans = -;
for(int t = ; t<=n; t++)
{
int i;
for(i = ; i<=n; i++)
{
int v = p[i][t];
if(p[v][t]!=i)
break;
}
if(i==n+)
{
ans = t;
break;
}
}
cout<<ans<<endl;
}

正确做法:

 #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = +; int n, a[maxn],vis[maxn]; int gcd(int a, int b) { return b==?a:gcd(b,a%b); } int dfs(int f, int i, int k)
{
if(vis[i]) return (i==f)?k:-;
vis[i] = ;
return dfs(f, a[i], k+);
} int main()
{
cin>>n;
for(int i = ; i<=n; i++)
cin>>a[i]; int ans = ;
for(int i = ; i<=n; i++)
{
if(vis[i]) continue;
int x = dfs(i,i,);
if(x==-)
{
ans = -;
break;
}
if(!(x&)) x >>= ;
ans = (ans*x)/gcd(ans,x);
}
cout<<ans<<endl;
}

最新文章

  1. Winform开发:在ProgressBar显示百分比数字
  2. LeetCode 解题报告索引
  3. ZipArchive 打包下载压缩包
  4. C++变量对比java变量所占内存
  5. hdu1272 小希的迷宫
  6. C++ string 类的 find 方法实例详解
  7. Java:多线程之生产者与消费者
  8. MSSQL索引优化
  9. easyui combobox赋值
  10. jQuery 图表
  11. 《Flask Web开发——基于Python的Web应用开发实践》一字一句上机实践(上)
  12. Welcome to Django!
  13. Activity简单几步支持向右滑动返回
  14. Ocelot中文文档-服务发现
  15. 洛谷P1880 石子合并(区间DP)(环形DP)
  16. linux下保护视力、定时强制锁定软件: Workrave
  17. #AOS应用基础平台# 实现了在用户权限范围内自己定义的快捷菜单的导航展示
  18. Python3中 sys.argv的用法
  19. OpenGL.ProjectiveTextureMapping
  20. 解决Redis/Codis Connection with master lost(复制超时)问题

热门文章

  1. ios- nil NULL 和 NSNull
  2. 【matlab】:matlab中不断的出现计算过程怎么办
  3. electron 缓存目录 禁用缓存
  4. 最新ssh2构架
  5. php实现双色球算法
  6. Allegro16.6和17.0和17.2中将板框导出DXF文件
  7. fping - send ICMP ECHO_REQUEST packets to network hosts
  8. idea常用的快捷命令
  9. EC2的维护更新
  10. tree related problems (update continuously)