首页 > 其他分享 >Codeforces Round #383 (Div. 2)-C. Arpa's loud Owf and Mehrdad's evil plan

Codeforces Round #383 (Div. 2)-C. Arpa's loud Owf and Mehrdad's evil plan

时间:2023-06-12 17:33:23浏览次数:51  
标签:return int Mehrdad ll Owf Arpa person round Joon


原题链接


C. Arpa's loud Owf and Mehrdad's evil plan



time limit per test



memory limit per test



input



output


As you have noticed, there are lovely girls in Arpa’s land.

1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi.



Codeforces Round #383 (Div. 2)-C. Arpa


Owf

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

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 yx would become the Joon-Joon of the round. Find such t

crushi = i).


Input



n (1 ≤ n ≤ 100) — the number of people in Arpa's land.

n integers, i-th of them is crushi (1 ≤ crushi ≤ n) — the number of i-th person's crush.


Output



t satisfying the condition, print -1. Otherwise print such smallest t.


Examples



input



4 2 3 1 4



output



3



input



4 4 4 4 4



output



-1



input



4 2 1 4 3



output



1


遍历每个人i, 用深搜找到是否存在从第i个人出发,经过次数t回到第i个人的情况,若不存在无解,若存在判断t是否为偶数,若为偶数则除以2,求出所有t的最小公倍数就是答案

#include <bits/stdc++.h>
#define MOD 1000000007
#define maxn 100005
using namespace std;
typedef long long ll;

int num[105];
int vis[105], t[105], cnt, k;
bool dfs(int j){
	
	if(num[j] == k){
		return true;
	}
	int h = num[j];
    if(vis[h])
	 return false;
	vis[h] = 1;
	cnt++;
	if(dfs(h))
	 return true;
	return false; 
}
ll gcd(ll a, ll b){
	return b ? gcd(b, a % b) : a;
}
int main(){
//	freopen("in.txt", "r", stdin);
	int n;
	scanf("%d", &n);
	for(int i = 1; i <= n; i++)
	 scanf("%d", num+i);
	for(int i = 1; i <= n; i++){
		memset(vis, 0, sizeof(vis));
		cnt = 1;
		vis[i] = 1;
		k = i;
		if(dfs(i) == false){
			puts("-1");
			return 0;
		}
		if(cnt&1)
		 t[i] = cnt;
		else
		 t[i] = cnt / 2;
	}
	ll ans = t[1];
	for(int i = 2; i <= n; i++){
		ans = ans * t[i] / gcd((ll)ans, (ll)t[i]);
	}
	printf("%I64d\n", ans);
	return 0;
}




标签:return,int,Mehrdad,ll,Owf,Arpa,person,round,Joon
From: https://blog.51cto.com/u_16158872/6464280

相关文章

  • AVA数据集以及SlowFast对该数据集的处理方法
    本文内容全部摘自:知乎AVAActionsDataset详解 。推荐看原文。1.1.基本情况数据集类别:Spatio-TemporalActionDetection,即时空行为检测。举个例子,就是检测出视频中所有人的位置以及对应的行为类别。数据集形式(这里是简单介绍,后面会有更详细的说明):要标记的内容包括人......
  • Unique Snowflakes uva11572
    找最长的,没有相同元素的区间 双指针#include<iostream>#include<set>usingnamespacestd;constintN=1e6+2;intn,a[N];voidsolve(){ intx=1,y=1,ans=0; set<int>st; while(y<=n){ while(y<=n&&!st.count(a[y]))s......
  • .Net Standard-Missing compiler member error Microsoft.CSharp.RuntimeBinder.CShar
     最近在玩dynamic的时候出现无法生成的情况."missingcompilermembererrorMicrosoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create"   解决方案:缺少Nuget包: Microsoft.CSharp ......
  • 【go】snowflake和snoyflake雪花算法学习与go实现
    预备知识:MonotonicClocks,即单调时间,所谓单调,就是只会不停的往前增长,不受校时操作的影响,这个时间是自进程启动以来的秒数参考文章:https://www.simpleapples.com/2018/10/......
  • 雪花算法(SnowFlake)
    简介现在的服务基本是分布式、微服务形式的,而且大数据量也导致分库分表的产生,对于水平分表就需要保证表中id的全局唯一性。对于MySQL而言,一个表中的主键id一般使用......
  • 分布式ID生成-雪花算法(Snowflake)
    1描述使用原生Java方式生成雪花算法,雪花算法是推特公司开源的生成唯一ID的算法,性能更高,可以避免对第三方依赖的使用,减少耦合  1)能满足高并发分布式系统环境下I......
  • SnowFlake 高可用的ID 生产方案
    SnowFlakeTwitter的雪花算法SnowFlake,使用Java语言实现。SnowFlake算法用来生成64位的ID,刚好可以用long整型存储,能够用于分布式系统中生产唯一的ID,并且生成的ID有大致的......
  • 机器学习(Tensowflow) 环境搭建(Anacoda的lab 使用)
    说明:本篇文章用于tensorflow学习,学习地址在bilibi,我会将自己做的笔记写成博客,最后会写成目录的,放上链接,方便大家阅读.1.创建的名字随便取一个就行2.确保有Tens......
  • D. Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths (cf741D)
    D.Arpa’sletter-markedtreeandMehrdad’sDokhtar-koshpaths(cf741D)tag:dsuontree,dp题目链接题意:给你一棵树,每一条边的权值是'a'到'v'的字母,求在每一个点......
  • SnowFlake 雪花算法详解与实现 & MP中的应用
    BackGround现在的服务基本是分布式,微服务形式的,而且大数据量也导致分库分表的产生,对于水平分表就需要保证表中id的全局唯一性。对于MySQL而言,一个表中的主键id一般......