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

首先把图建起来。

对于每个a[i],那么就在i --- a[i]建一条边,单向的。

如果有一个点的入度是0或者是>= 2,那么就不行了。直接-1

然后就是把图分成若干个圈了。

对于每一个圈,只需要找一个点,dfs,算出它回到自己的时候需要多少步数。

如果步数是偶数,那么只需要取中点,x -- mid    然后mid -- x步数是一样,那么最小的t就是长度/2

如果是奇数,那没办法了。

然后把所有的圈的步数来一个lcm,就是答案。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 1e2 + ;
int e[maxn][maxn];
int in[maxn];
bool vis[maxn];
int tans, ans;
int n;
int lcm(int a, int b) {
return a / __gcd(a, b) * b;
}
void dfs(int cur, int haha) {
for (int i = ; i <= n; ++i) {
if (e[cur][i] == && vis[i] == false) {
vis[i] = true;
tans++;
dfs(i, haha);
}
}
}
void work() {
ans = ;
memset(e, 0x3f, sizeof e);
IOS;
cin >> n;
for (int i = ; i <= n; ++i) {
int x;
cin >> x;
e[i][x] = ;
in[x]++;
}
for (int i = ; i <= n; ++i) {
if (in[i] == || in[i] >= ) {
cout << - << endl;
return;
}
}
for (int i = ; i <= n; ++i) {
if (vis[i]) continue;
tans = ;
dfs(i, i);
if (tans % == ) tans /= ;
ans = lcm(ans, tans);
}
cout << ans << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}

C. Arpa's loud Owf and Mehrdad's evil plan DFS + LCM的更多相关文章

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

  2. Codeforces 741A:Arpa's loud Owf and Mehrdad's evil plan(LCM+思维)

    http://codeforces.com/problemset/problem/741/A 题意:有N个人,第 i 个人有一个 a[i],意味着第 i 个人可以打电话给第 a[i] 个人,所以如果第 ...

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

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

  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 742C】Arpa's loud Owf and Mehrdad's evil plan

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

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

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

  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. C++11 条件变量

    C++11中的条件变量提供了用户等待的同步机制,在同步队列的应用中有很大的便利. 简单同步队列代码如下(SimpleSyncQueue.h): #ifndef SIMPLESYNCQUEUE_H #d ...

  2. 关于在PHP中当一个请求未完成时,再发起另一个请求被阻塞的问题

    最近做项目的时候遇到个问题,就是做阿里云oss大文件上传进度条显示,因为要实时查询上传分片进度,所以在上传的同时必须要再发起查询的请求,但是一直都是所有分片上传完成后查询的请求才执行,刚开始以为是阿里 ...

  3. Yii2 behaviors中verbs access的一些理解

    public function behaviors() { return ArrayHelper::merge(parent::behaviors(), [ 'verbs' => [ 'clas ...

  4. Fibonacci数列(找规律)

    题目描述 Fibonacci数列是这样定义的:F[0] = 0F[1] = 1for each i ≥ 2: F[i] = F[i-1] + F[i-2]因此,Fibonacci数列就形如:0, 1, ...

  5. 设置label的字体

    label.font = [UIFont fontWithName:@"Arial-BoldItalicMT" size:24]; 字体名如下: Font Family: Amer ...

  6. 使用Pycharm官方统计代码行插件统计代码总行数

    最近有需求,需要统计项目代码的总行数,首先想到了使用Pycharm官方的统计行数插件,发现效果还不错. 官方代码统计插件指导:https://plugins.jetbrains.com/plugin/ ...

  7. CKD 实现

    主要功能: 1.新物料(部品号)的入库管理 部品号的验证.描述.品名.重量.单价等 2.部品号-供应商的核对 校验部品号/供应商的对应情况.入库.移除等 3.BOM清单的导入 基础清单的导入 4.订单 ...

  8. 【202】ThinkPad手势&快捷键

    快捷键: Ctrl + Alt + ↑:正常屏幕Ctrl + Alt + ↓:翻转屏幕Ctrl + Alt + →:向左侧翻转90°Ctrl + Alt + ←:向右侧翻转90° 首先看下 Esc 键 ...

  9. B. Mishka and trip

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  10. linux中用无名管道进行文件的读写

    1管道是什么: 水管子大家知道,有两端,在此一端用来读一端用来写,其中一端的输出作为另外一端的输入. 2 函数原型 int pipe(int pipefd[2]);//参数中分别代表的两端 3 例子: ...