http://codeforces.com/contest/742/problem/C

题目大意:从x出发,从x->f[x] - > f[f[x]] -> f[f[f[x]]] -> ..... -> y的步数需要t步,然后再从y出发回到x的步数也需要t步。问需要找到一个最小的t,使得任何一个x经过t步可以到达某一个y,且y也可以经过t步走到x

思路:

先判断是否全都是自环。然后如果不是全都是自环的话。再dfs找环,并且记录环中的结点数。如果是偶环,那么往vector里面push_back(结点数/2),奇环就往vector里面push_back(结点数)。然后求最小公倍数就好啦。

当然,要判断是否有不存在环的情况

//看看会不会爆int!数组会不会少了一维!
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
const int maxn = + ;
int crush[maxn], vis[maxn];
int n;
vector<int> v; int gcd(int a, int b){
return b == ? a : gcd(b, a % b);
} bool dfs(int u, int &cnt, int beg){
vis[u] = true;
int v = crush[u];
if (v == beg) return false;
if (vis[v]) return true;
cnt += ;
return dfs(v, cnt, beg);
} int main(){
cin >> n;
int t = ;
for (int i = ; i<= n; i++){
scanf("%d", crush + i);
if (i == crush[i]) t++;
}
if (t == n) {
printf("1\n"); return ;
}
bool flag = false;
for (int i = ; i <= n; i++){
if (i == crush[i]) continue;
if (!vis[i]) {
int cnt = ;
flag = dfs(i, cnt, i);
if(flag) break;
v.push_back(cnt % == ? cnt/ : cnt);
}
}
if (flag) printf("-1\n");
else {
int ans = v[];
for (int i = ; i < v.size(); i++){
ans = ans * v[i] / gcd(ans, v[i]);
}
printf("%d\n", ans);
}
return ;
}

dfs + 最小公倍数 Codeforces Round #383 (Div. 2)的更多相关文章

  1. Codeforces Round #383 (Div. 2) 题解【ABCDE】

    Codeforces Round #383 (Div. 2) A. Arpa's hard exam and Mehrdad's naive cheat 题意 求1378^n mod 10 题解 直接 ...

  2. DFS/BFS Codeforces Round #301 (Div. 2) C. Ice Cave

    题目传送门 /* 题意:告诉起点终点,踩一次, '.'变成'X',再踩一次,冰块破碎,问是否能使终点冰破碎 DFS:如题解所说,分三种情况:1. 如果两点重合,只要往外走一步再走回来就行了:2. 若两 ...

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

  4. Codeforces Codeforces Round #383 (Div. 2) E (DFS染色)

    题目链接:http://codeforces.com/contest/742/problem/E 题意: 有一个环形的桌子,一共有n对情侣,2n个人,一共有两种菜. 现在让你输出一种方案,满足以下要求 ...

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

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

  6. Codeforces Round #383 (Div. 1)

    A: 题目大意:给出一个有向图(n<=100),每个点的出度都为1,求最小的t,使得任意两点x,y,如果x走t步后能到y,那么y走t步后到x. 题解: 首先每个点应该都在一个环上,否则无解. 对 ...

  7. Codeforces Round #383 (Div. 2) 解题报告

    本来是打算所有半夜进行的CF都不参加的,但看到这次比赛22:35就开始,还是没有忍住orz--晚上总是不够清醒,做题思维不如白天活跃,低级错误常常出现.出的比较早的C因为一个书写错误有点小bug,在比 ...

  8. Codeforces Round #383 Div 1题解

    第一次打Div 1,感觉还是挺难的..把基础题打完就日常划水了.... [A. Arpa's loud Owf and Mehrdad's evil plan](http://codeforces.c ...

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

随机推荐

  1. @ResponseBody注解与JSON

    MappingJacksonHttpMessageConverter 调用了 objectMapper.writeValue(OutputStream stream, Object)方法,使用@Res ...

  2. Nopi Excel导入

    http://download.csdn.net/detail/diaodiaop/7611721 using System.Collections.Generic; using System.Dat ...

  3. JPA 系列教程 异常 集锦

    异常1.hibernate升级到3.5版本 异常信息摘要: Associations marked as mappedBy must not define database mappings like ...

  4. 转: Executor类

    Executor框架是指java 5中引入的一系列并发库中与executor相关的一些功能类,其中包括线程池,Executor,Executors,ExecutorService,Completion ...

  5. Sub Lime Text

    Sub Lime Text License -– BEGIN LICENSE -– Andrew Weber Single User License EA7E-855605 813A03DD 5E4A ...

  6. 读取Zip文件内容

    第一步,上次文件并保存到服务器目录下 /// <summary> /// 上传压缩文件 /// </summary> protected void UploadZip() { ...

  7. MVC学习笔记--IEnumerable的用法

    IEnumerable的用法 IEnumerable和IEnumerable<T>接口在.NET中是非常重要的接口,它允许开发人员定义foreach语句功能的实现 并支持非泛型方法的简单的 ...

  8. LYF电子书制作工具(CHM格式)

    可以制作CHM电子书 可以制作电子相册 下载地址:http://files.cnblogs.com/files/blogLYF/LYF电子书制作安装包.zip http://files.cnblogs ...

  9. 湖南多校对抗赛(2015.05.03)Problem A: Twenty-four point

    给四个数 问能不能算出24点...我的方法比较烂...920ms 差点TLE.应该有更好的方法. #include<stdio.h> #include<string.h> #i ...

  10. @InitBinde的作用

    @InitBinder用于在@Controller中标注于方法,表示为当前控制器注册一个属性编辑器或者其他,只对当前的Controller有效