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或者是>= 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的更多相关文章
- 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 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] 个人,所以如果第 ...
- 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 ...
- 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 ...
- 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 742C】Arpa's loud Owf and Mehrdad's evil plan
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 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. 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 ...
随机推荐
- top swap
显示交换空间(虚拟内存)的使用情况
- bash_action
https://stackoverflow.com/questions/12076326/how-to-install-maven2-on-redhat-linux #!/bin/bash # Tar ...
- OOalv 实现带出栏位描述
.类定义 CLASS lcl_event_handler DEFINITION. PUBLIC SECTION. METHODS: handle_data_changed_finished FOR E ...
- bzoj4105: [Thu Summer Camp 2015]平方运算
填坑 我不知道怎么算的,但是所有环的LCM数不会超过60 然后用线段树维护这个东西,每个节点记录子树内的循环节 没到循环节的暴力枚举 复杂度是nlogn再乘以循环节长度 #include<cst ...
- android之View坐标系(view获取自身坐标的方法和点击事件中坐标的获取)
在做一个view背景特效的时候被坐标的各个获取方法搞晕了,几篇抄来抄去的博客也没弄很清楚. 现在把整个总结一下. 其实只要把下面这张图看明白就没问题了. 涉及到的方法一共有下面几个: view获取自身 ...
- codeforces 436A. Feed with Candy 解题报告
题目链接:http://codeforces.com/contest/436/problem/A 题目意思:给出 n 颗只有两种类型:fruit 和 caramel的candies,这些candies ...
- ssl原理及应用
今天学习网络通信,看到使用ssl(Secure Sockets Layer)进行加密,由于对ssl只是有些概念上的了解,对于具体应用原理.过程和如何使用不慎了解,于是学习了一番,总结如下: 1. 为什 ...
- javascript之递归得DOM文本
var tag=document.getElementsByTagName('body')[0]; function findChild(tag){ var child=tag.childNodes ...
- Appium+python自动化
名称 链接地址 Appium+python自动化8-Appium Python API(上) http://mp.weixin.qq.com/s/WvpT5oRrYY22avI95FuypQ Appi ...
- jsp实现文件上传(一)用jspSmartUpload组件实现文件上传
java类(ImageUtil.java) import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import ...