dfs + 最小公倍数 Codeforces Round #383 (Div. 2)
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)的更多相关文章
- 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 题解 直接 ...
- DFS/BFS Codeforces Round #301 (Div. 2) C. Ice Cave
题目传送门 /* 题意:告诉起点终点,踩一次, '.'变成'X',再踩一次,冰块破碎,问是否能使终点冰破碎 DFS:如题解所说,分三种情况:1. 如果两点重合,只要往外走一步再走回来就行了:2. 若两 ...
- 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 ...
- Codeforces Codeforces Round #383 (Div. 2) E (DFS染色)
题目链接:http://codeforces.com/contest/742/problem/E 题意: 有一个环形的桌子,一共有n对情侣,2n个人,一共有两种菜. 现在让你输出一种方案,满足以下要求 ...
- Codeforces Round #383 (Div. 2) C. Arpa's loud Owf and Mehrdad's evil plan(dfs+数学思想)
题目链接:http://codeforces.com/contest/742/problem/C 题意:题目比较难理解,起码我是理解了好久,就是给你n个位置每个位置标着一个数表示这个位置下一步能到哪个 ...
- Codeforces Round #383 (Div. 1)
A: 题目大意:给出一个有向图(n<=100),每个点的出度都为1,求最小的t,使得任意两点x,y,如果x走t步后能到y,那么y走t步后到x. 题解: 首先每个点应该都在一个环上,否则无解. 对 ...
- Codeforces Round #383 (Div. 2) 解题报告
本来是打算所有半夜进行的CF都不参加的,但看到这次比赛22:35就开始,还是没有忍住orz--晚上总是不够清醒,做题思维不如白天活跃,低级错误常常出现.出的比较早的C因为一个书写错误有点小bug,在比 ...
- Codeforces Round #383 Div 1题解
第一次打Div 1,感觉还是挺难的..把基础题打完就日常划水了.... [A. Arpa's loud Owf and Mehrdad's evil plan](http://codeforces.c ...
- 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 ...
随机推荐
- js生成缩略图后上传并利用canvas重绘
function drawCanvasImage(obj,width, callback){ var $canvas = $('<canvas></canvas>'), can ...
- ASP.NET里面,如果设置了form的 onsubmit="return false;"之后,就不能提交按钮了?
我的按钮是写成的服务器控件的形式<asp:Button ID="btnSubmitBR" runat="server" Text="提交&quo ...
- Java 内部类 this
内部类访问外部类的一个例子: public class Abc { private class Bc { public void print() { System.out.println(Abc.th ...
- [河南省ACM省赛-第三届] AMAZING AUCTION (nyoj 251)
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=251 规则: 1.若某竞标价唯一,则胜出 2.若不存在唯一竞标价,则投标次数最少竞标价中标 ...
- .Net中jQuery.ajax()调用asp.net后台方法 总结
利用JQuery的$.ajax()调用.Net后台方法有多种方式, 不多说了 直接上代码 前台代码 <script type="text/javascript"> $ ...
- 学习笔记:shell 中 [-eq] [-ne] [-gt] [-lt] [ge] [le]
-eq //等于 -ne //不等于 -gt //大于 (greater ) -lt //小于 (less) -g ...
- java中转换json方式(JSONArray,JSONObject),json解析
package com.yunos.tv.video.resource.controller.web; import java.util.ArrayList; import java.util.Has ...
- Little Puzzlers–List All Anagrams in a Word
The Solution A major hint is in the fact we are given a dictionary. Because we are given this dicti ...
- 一个初学者的辛酸路程-FTP-9
前言 今天,我要描述一个FTP的故事 主要内容 嗯,今天主要以阶梯性的形式来做一个FTP项目. 第一步: 我要实现这么一个功能,一个FTP客户端,1个FTP服务端,2端建立连接以后可以进行通讯. 服务 ...
- Date对象 识记
1.Date 对象创建 var myDate=new Date() 2.Date 对象属性 constructor pototype 3.Date 对象方法 Date() ...