Codeforces Round #383 (Div. 2) C. Arpa's loud Owf and Mehrdad's evil plan —— DFS找环
题目链接: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'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或者是 ...
- 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) 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 ...
- 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'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 ...
随机推荐
- ELK之收集haproxy日志
由于HAProxy的运行信息不写入日志文件,但它依赖于标准的系统日志协议将日志发送到远程服务器(通常位于同一系统上),所以需要借助rsyslog来收集haproxy的日志.haproxy代理nginx ...
- session转载
sessionid是一个会话的key,浏览器第一次访问服务器会在服务器端生成一个session,有一个sessionid和它对应.tomcat生成的sessionid叫做jsessionid. ses ...
- FreeBSD 8
FreeBSD 8.0的安装过程和7.2区别不大.先在FreeBSD官方网站上下载安装镜像,我一般都下载DVD的ISO,也有人爱好下最小的安装包,然后通过FTP或HTTP方式从网上下载各个程序包. 这 ...
- 百科知识 华为手机P7如何更换电池
参考下面 教程 https://item.jd.com/3265516.html
- [3 Jun 2015 ~ 9 Jun 2015] Deep Learning in arxiv
arXiv is an e-print service in the fields of physics, mathematics, computer science, quantitative bi ...
- android客户端向服务器端验证登陆方法的实现1
遇到的问题:一个条件查询与多个条件查询,所用到的方式不一样 参考文档: http://www.oschina.net/question/1160609_133366 mybatis多条件查询的一 ...
- SpringMVC:走通一个SpringMVC
我们现在使用SpringMVC来做一个小的用户管理系统,由于重点在学习SpringMVC,这里我们就不用数据库了. 该小系统实现的功能是:1.登录,不做用户名密码的正确性判断,任何用户名+密码都可以成 ...
- HTML5开发移动web应用——Sencha Touch篇(7)
Sencha Touch中的Ext.DomHelper组件能够方便的实现对元素的追加或重写操作 演示样例: launch:function(){ function appendDom(){ Ext.D ...
- Tomcat服务器改主页 & jeesite框架改首页
Tomcat服务器改主页: 方法一: 把原来的 ROOT 目录清空: 发布你自己的项目到 ROOT 目录下: 发布程序 /webapps/ROOT/WEB-INF/web.xml 中需要有默认首页定义 ...
- strupr和strlwr字符串函数的使用
strupr 功能:将小写字母转换为大写字母 strlwr 功能:将大写字母转换为小写字母 在VS2013里面使用的时候要这样的格式 _strlwr_s _strupr_s #include<s ...