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

    【分析】题目扯了一大堆,主要是这个意思。n个人,每个人有一个打电话的对象(可以是自己),设t为打电话的总次数,当一个人打电话时,他只打给自己的对象,然后他的对象接着打给自己的对象。。。问最小的t,使得从任意的x开始打电话,总共打了t次后到达y,这一轮结束,然后y接着打,总共打了t次后又回到x,(x与y可等)。

    说白了,就是一个有向图,找遍所有的环,若环为偶数,则/2,求所有的最小公倍数。

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <string>
    #include <map>
    #include <stack>
    #include <queue>
    #include <vector>
    #define inf 10000000000000
    #define met(a,b) memset(a,b,sizeof a)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    typedef long long ll;
    using namespace std;
    const int N = 1e2+5;
    const int M = 4e5+5;
    int dp[N][21];
    int n,sum[N],m=0,p,k;
    int Tree[N];
    ll w[N][N],vis[N];
    void Floyd(){
        for(int k=1;k<=n;k++){
            for(int i=1;i<=n;i++){
                for(int j=1;j<=n;j++){
                    if(w[i][k]!=inf&&w[k][j]!=inf&&w[i][j]>w[i][k]+w[k][j]){
                        w[i][j]=w[i][k]+w[k][j];
                    }
                }
            }
        }return;
    }
    int GCD(int minn,int maxn){
        if(maxn%minn==0)return minn;
        else return GCD(min(minn,maxn%minn),max(minn,maxn%minn));
    }
    int main()
    {
        for(int i=0;i<N;i++)for(int j=0;j<N;j++)w[i][j]=inf;
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",&k);
            w[i][k]=1;
        }
        Floyd();
        ll ans=inf;
        bool flag=false;
        for(int i=1;i<=n;i++){
            if(w[i][i]<inf){
                   if(w[i][i]&1){
                    if(!flag)ans=w[i][i],flag=true;
                    else ans=(ll)ans*(w[i][i]/GCD(min(w[i][i],ans),max(w[i][i],ans)));
                   }
                   else {
                    w[i][i]/=2;
                    if(!flag)ans=w[i][i],flag=true;
                    else ans=(ll)ans*(w[i][i]/GCD(min(w[i][i],ans),max(w[i][i],ans)));
                   }
            }
            else if(w[i][i]==inf){
                ans=inf;
                break;
            }
        }
        if(ans==inf)puts("-1");
        else printf("%lld
    ",ans);
        return 0;
    }
  • 相关阅读:
    2014-写给明年现在的自己
    DDD 领域驱动设计-Value Object(值对象)如何使用 EF 进行正确映射
    DDD 领域驱动设计-在动手之前,先把你的脑袋清理干净
    醍醐灌顶:领域驱动设计实现之路
    拨乱反正:DDD 回归具体的业务场景,Domain Model 再再重新设计
    设计窘境:来自 Repository 的一丝线索,Domain Model 再重新设计
    No zuo no die:DDD 应对具体业务场景,Domain Model 重新设计
    拨开迷雾,找回自我:DDD 应对具体业务场景,Domain Model 到底如何设计?
    【记录】GitHub/TortoiseGit 修改邮箱/提交者
    死去活来,而不变质:Domain Model(领域模型) 和 EntityFramework 如何正确进行对象关系映射?
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/6145994.html
Copyright © 2011-2022 走看看