zoukankan      html  css  js  c++  java
  • 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 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 repeatedt 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 theJoon-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 yx 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 xis 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.

    分析:转移到自己时如果经过偶数次,则可以算一半贡献;

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #define rep(i,m,n) for(i=(int)m;i<=(int)n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<int,int>
    #define Lson L, mid, ls[rt]
    #define Rson mid+1, R, rs[rt]
    #define sys system("pause")
    const int maxn=1e5+10;
    using namespace std;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    inline ll read()
    {
        ll x=0;int f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int n,m,k,t,a[maxn];
    ll ans,cnt;
    set<int>pq;
    bool flag;
    void gao(int p,int q)
    {
        cnt++;
        if(pq.find(p)!=pq.end())
        {
            flag=false;
            return;
        }
        else if(p==q)return;
        else pq.insert(p),gao(a[p],q);
    }
    int main()
    {
        int i,j;
        ans=1;
        flag=true;
        scanf("%d",&n);
        rep(i,1,n)scanf("%d",&a[i]);
        rep(i,1,n)
        {
            pq.clear();
            cnt=0;
            gao(a[i],i);
            if(cnt%2==0)cnt>>=1;
            ans=ans*cnt/gcd(ans,cnt);
        }
        if(flag)printf("%lld
    ",ans);
        else puts("-1");
        //system("Pause");
        return 0;
    }
  • 相关阅读:
    ue4 socket
    ue4动画蓝图
    localStorage 用法
    关于textarea中换行、回车、空格的识别与处理
    git忽略某些文件提交
    动态加载js文件
    H5 App页面 绝对定位 软键盘弹出时顶起底部按钮
    Android软键盘弹出时把布局顶上去的解决方法
    javascript 事件委托 和jQuery事件绑定on、off 和one
    escape()、encodeURI()、encodeURIComponent()区别详解
  • 原文地址:https://www.cnblogs.com/dyzll/p/6193918.html
Copyright © 2011-2022 走看看