zoukankan      html  css  js  c++  java
  • 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 megabytes
    input
    standard input
    output
    standard output

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

    People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi.

    Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows.

    The game consists of rounds. Assume person 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 - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time.

    Mehrdad has an evil plan to make the game more funny, he wants to find smallest 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 y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible.

    Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i).

    Input

    The first line of input contains integer n (1 ≤ n ≤ 100) — the number of people in Arpa's land.

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

    Output

    If there is no 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
    Note

    In the first sample suppose t = 3.

    If the first person starts some round:

    The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1.

    The process is similar for the second and the third person.

    If the fourth person starts some round:

    The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4.

    In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa.

    /*
    并查集瞎搞,唯一一组爆int的数据:
    如果环的长度为:2 3 5 7 11 13 17 19 23,那么最小公倍数就是363322674就会爆int
    */
    #include<bits/stdc++.h>
    #define ll long long
    using namespace std;
    int bin[150];
    int vis[150];
    int vis2[150];
    int n,a[150];
    ll cur=0;
    ll gcd(ll a,ll b)
    {
        return a==0?b:gcd(b%a,a);
    }
    ll lcm(ll a,ll b)
    {
        return a*b/gcd(a,b);
    }
    int findx(int x,int start)//查找
    {
        while(bin[x]!=x)
        {
            x=bin[x];
            if(x==start)
                break;
        }
        return x==start;
    }
    void op(int x,int start)//查找
    {
        vis[x]=1;
        int flag=0;//表示是不是第一次
        while(bin[x]!=x)
        {
    
            vis[x]=1;
            x=bin[x];
            if(x==start)
            {
                if(cur!=1)
                    cur++;
                break;
            }
            cur++;
        }
        if(cur==0)
            cur=1;
    }
    int main()
    {
        //freopen("C:\Users\acer\Desktop\in.txt","r",stdin);
        scanf("%d",&n);
        for(int i=0;i<=n;i++)
        {
            vis[i]=0;
            vis2[i]=0;
            bin[i]=i;
        }
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            if(vis2[a[i]]==0)
                ans++;
            bin[i]=a[i];
            vis2[a[i]]=1;
        }
        if(ans!=n)
        {
            puts("-1");
            return 0;
        }
        int f=0;//是不是每个节点都有环
        long long maxn=1;
        for(int i=1;i<=n;i++)
        {
            cur=0;
            //cout<<"findx(i,i)="<<findx(i,i)<<endl;
            if(vis[i]==0)//没有访问过
            {
                if(findx(i,i))//有环
                {
                    op(i,i);//去更新操作
                    //cout<<"cur="<<cur<<endl;
                    if(cur%2==0)
                        cur/=2;
                    maxn=maxn/__gcd(maxn,cur)*cur;
                }
                else
                {
                    f=1;
                    break;
                }
            }
        }
        if(f)
            puts("-1");
        else
            printf("%lld
    ",maxn);
        return 0;
    }
  • 相关阅读:
    ES6相关概念及新增语法
    正则表达式
    递归
    高阶函数和闭包
    严格模式
    this指向
    递归
    严格模式
    函数内部的this指向
    函数的定义和调用
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/6140578.html
Copyright © 2011-2022 走看看