题目链接:http://codeforces.com/contest/742/problem/C
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 - 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 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.
题解:
错误的做法:
本以为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找环的更多相关文章
-
Codeforces Round #383 (Div. 2)C. Arpa&#39;s loud Owf and Mehrdad&#39;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&#39;s loud Owf and Mehrdad&#39;s evil plan(dfs+数学思想)
题目链接:http://codeforces.com/contest/742/problem/C 题意:题目比较难理解,起码我是理解了好久,就是给你n个位置每个位置标着一个数表示这个位置下一步能到哪个 ...
-
C. Arpa&#39;s loud Owf and Mehrdad&#39;s evil plan DFS + LCM
http://codeforces.com/contest/742/problem/C 首先把图建起来. 对于每个a[i],那么就在i --- a[i]建一条边,单向的. 如果有一个点的入度是0或者是 ...
-
code forces 383 Arpa&#39;s loud Owf and Mehrdad&#39;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&#39;s loud Owf and Mehrdad&#39;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&#39;s loud Owf and Mehrdad&#39;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) D. Arpa&#39;s weak amphitheater and Mehrdad&#39;s valuable Hoses —— DP(01背包)
题目链接:http://codeforces.com/contest/742/problem/D D. Arpa's weak amphitheater and Mehrdad's valuable ...
-
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 ...
-
Codeforces Round #383 (Div. 2) D. Arpa&#39;s weak amphitheater and Mehrdad&#39;s valuable Hoses(分组背包+dsu)
D. Arpa's weak amphitheater and Mehrdad's valuable Hoses Problem Description: Mehrdad wants to invit ...
随机推荐
-
Linux字符设备驱动结构(一)--cdev结构体、设备号相关知识机械【转】
本文转载自:http://blog.csdn.net/zqixiao_09/article/details/50839042 一.字符设备基础知识 1.设备驱动分类 linux系统将设备分为3类:字符 ...
-
微信公众平台自定义菜单PHP开发
微信公众平台自定义菜单PHP开发,微信公众平台自定义菜单是如何实现的呢?其实很简单,首先在微信公众平台升级为服务号,获取appid和appsecret,然后根据这2个参数获取access_token, ...
-
jquery easyui datagrid使用参考
jquery easyui datagrid使用参考 创建datagrid 在页面上添加一个div或table标签,然后用jquery获取这个标签,并初始化一个datagrid.代码如下: 页面上 ...
-
Angular之作用域与事件(转)
学习Angular,首先要理解其作用域机制. Angular应用是分层的,主要有三个层面:视图,模型,视图模型.其中,视图很好理解,就是直接可见的界面,模型就是数据,那么视图模型是什么呢?是一种把数据 ...
-
discuz函数dgmdate
function dgmdate($timestamp, $format = 'dt', $timeoffset = '9999', $uformat = '') { global $_G; $for ...
-
立贴读 《CLR》
弱弱的说,我要开始读<CLR>这本书了,怕自己不能坚持下来,特立贴监督自己,本来是大牛们涉及的区域,现在好朋友的鼓励下,勇敢的踏入,如有错误,还请各位指正.
-
Memcache存储机制与指令汇总
1.memcache基本简介 memcached是高性能的分布式内存缓存服务器.一般的使用目的是,通过缓存数据库查询结果,减少数据库访问次数,以提高动态Web应用的速度.提高可扩展性. Memcach ...
-
swift闭包中解决循环引用的问题
swift中可以通过三种方法解决循环引用的问题 利用类似oc方法解决循环引用weak var weakSelf = self weak var weakSelf = self loadData = { ...
-
poj 1696 极角排序求最长逆时针螺旋线
Space Ant Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4970 Accepted: 3100 Descrip ...
-
Solr数据迁移
单机Solr部署在linux /opt目录下,运行一段时间后发现该目录分配的空间不足,而Solr的索引数据量较大,必须更改相关core下面的data目录,以改变索引存放的目录. 找到相应的solrco ...