题目链接: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;
}

Codeforces Round #383 (Div. 2) C. Arpa's loud Owf and Mehrdad's evil plan —— DFS找环的更多相关文章

  1. 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 ...

  2. Codeforces Round #383 (Div. 2) C. Arpa's loud Owf and Mehrdad's evil plan(dfs+数学思想)

    题目链接:http://codeforces.com/contest/742/problem/C 题意:题目比较难理解,起码我是理解了好久,就是给你n个位置每个位置标着一个数表示这个位置下一步能到哪个 ...

  3. 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或者是 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses —— DP(01背包)

    题目链接:http://codeforces.com/contest/742/problem/D D. Arpa's weak amphitheater and Mehrdad's valuable ...

  8. Codeforces Round #383 (Div. 2) B. Arpa’s obvious problem and Mehrdad’s terrible solution —— 异或

    题目链接:http://codeforces.com/contest/742/problem/B B. Arpa's obvious problem and Mehrdad's terrible so ...

  9. Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses(分组背包+dsu)

    D. Arpa's weak amphitheater and Mehrdad's valuable Hoses Problem Description: Mehrdad wants to invit ...

随机推荐

  1. 洛谷——P2701 [USACO5.3]巨大的牛棚Big Barn

    P2701 [USACO5.3]巨大的牛棚Big Barn 题目背景 (USACO 5.3.4) 题目描述 农夫约翰想要在他的正方形农场上建造一座正方形大牛棚.他讨厌在他的农场中砍树,想找一个能够让他 ...

  2. spring-mvc 的一些使用技巧(转)

    APP 服务端的 Token 验证 通过拦截器对使用了@Authorization注解的方法进行请求拦截,从 http header 中取出 token 信息,验证其是否合法.非法直接返回 401 错 ...

  3. JDK1.8中的Lambda表达式和Stream

    1.lambda表达式 Java8最值得学习的特性就是Lambda表达式和Stream API,如果有python或者javascript的语言基础,对理解Lambda表达式有很大帮助,因为Java正 ...

  4. 使用UltraISO为U盘或内存卡制作系统安装工具

    此软件可以为U盘制作Windows安装版的启动工具,也可以为Linux制作启动工具,尤其是Ubuntu这些.提示:推荐购买一些读取速度快一些的U盘,运行起来可以节省很多时间. 如果是内存卡要实现此功能 ...

  5. [UIDevice currentDevice]

    获取iphone的系统信息使用[UIDevice currentDevice],信息例如以下: [[UIDevice currentDevice] systemName]:系统名称,如iPhone O ...

  6. 无插件,跨平台,基于WebGL的三维地球来了!!!

    用户通过浏览器即可递交数据到同一个地理信息系统中,操作简单,跨平台 ,无插件,可扩展,高效共享 ,完美匹配超大数据量发布! 近年来,随着计算机图形学.虚拟现实.卫星遥感.航空摄影.激光雷达等技术的迅猛 ...

  7. ios 清理缓存(EGO)

    一段清理缓存的代码例如以下: dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,) , ^{ NSSt ...

  8. sql导入数据库

    有问题可以尝试加上这句 DROP TABLE IF EXISTS `t_saler_login_log`;

  9. 如何下载合适自己系统环境的Xdebug

      访问https://xdebug.org     在浏览器输入http://localhost/?phpinfo=1 (前提已经安装了wamp环境) Ctrl+a 全选 ,复制 粘贴到刚才的网站的 ...

  10. js 节点操作

    添加和删除节点(HTML 元素). 创建新的 HTML 元素 如需向 HTML DOM 添加新元素,您必须首先创建该元素(元素节点),然后向一个已存在的元素追加该元素. 实例 <div id=& ...