多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內(nèi)最全IT社區(qū)平臺 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁 > php開源 > 綜合技術(shù) > Codeforces Round #383 (Div. 2) C. Arpa's loud Owf and Mehrdad's evil plan dfs+最小公倍數(shù)

Codeforces Round #383 (Div. 2) C. Arpa's loud Owf and Mehrdad's evil plan dfs+最小公倍數(shù)

來源:程序員人生   發(fā)布時間:2017-04-08 13:58:06 閱讀次數(shù):4586次

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 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 . Otherwise print such smallest t.

Examples
input
4
2 3 1 4
output
3
input
4
4 4 4 4
output
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 is1.

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.



Source

Codeforces Round #383 (Div. 2)


My Solution

題意:當(dāng)1個人開始是另外一個人結(jié)束,但這個人開始時前面那個人結(jié)束,具體還是請看題吧,哈哈


dfs+最小公倍數(shù)

每一個人只能且必須處于1個環(huán)中,自環(huán)也是環(huán),如果環(huán)的元素個數(shù)是奇數(shù)則這個環(huán)必須是ansi =  k*cnt,如果是偶數(shù)則 ans = k * (cnt / 2);//這個是自己畫圖發(fā)現(xiàn)的規(guī)律。

這題不用多想甚么優(yōu)化的方法,弄復(fù)雜了反而容易錯(比如筆者自己 T _ T ),n <= 100,直接對每個點每層dfs時cnt++,

直到找到 目標(biāo) find(u, v),,或找到根節(jié)點時,或 cnt > 100 時return。//這個100是自己設(shè)定的,即最多100個節(jié)點cnt大于100說明ans = ⑴。

比如

5
2 4 3 1 2

這組數(shù)據(jù)。

找出每一個環(huán)的ansi,然后對這些ansi取最大公約數(shù)便可,a、b最大公約數(shù)等于 a * b / (gcd(a, b)),且注意中間進程溢出,由于這里有 a * b了

復(fù)雜度 O(n^2)


#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
typedef long long LL;
const int maxn = 1e2 + 8;

int cnt;
bool flag[maxn];
queue<int> que;

int father[maxn], _rank[maxn];
inline void DisjointSet(int n)
{
    for(int i = 0; i <= n; i++){
        father[i] = i;
    }
}

inline bool _find(int v, int u)
{
    if(father[v] == u){
        //cout << cnt << endl;
        if(cnt % 2 == 1){
            que.push(cnt);
        }
        else{
            que.push(cnt / 2);
        }
        return true;
    }
    else if(father[v] == v){
        return false;
    }
    else{
        cnt++;
        if(cnt == 1024) return false;
        if(!_find(father[v], u)){
            return false;
        }
        else return true;
    }
}

inline LL gcd(LL a, LL b)
{
    return b == 0 ? a : gcd(b, a % b);
}
int main()
{
    #ifdef LOCAL
    freopen("c.txt", "r", stdin);
    //freopen("c.out", "w", stdout);
    int T = 4;
    while(T--){
    #endif // LOCAL
    ios::sync_with_stdio(false); cin.tie(0);

    int n;
    cin >> n;
    DisjointSet(n);
    for(int i = 1; i <= n; i++){
        cin >> father[i];
    }

    LL ans = 1;
    for(int i = 1; i <= n; i++){
        cnt = 1;
        if(!_find(i, i)){
            ans = 1024;
            break;
        }
    }
    if(ans != 1024){
        ans = que.front();
        que.pop();
        while(!que.empty()){
            ans = (ans * que.front()) / gcd(ans, que.front());
            //!WA 61 這里溢出了,換成LL 以后過了, 畢竟求的是最小公倍數(shù)
            que.pop();
        }
        cout << ans << endl;
    }
    else cout << ⑴ << endl;



    #ifdef LOCAL
    memset(flag, false, sizeof flag);
    cout << endl;
    }
    #endif // LOCAL
    return 0;
}



  Thank you!

                                                                                                                                               ------from ProLights

生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 欧美黑人三级 | 国产精品视频久久久久 | 精品一区二区久久 | 波多野结衣视频免费在线观看 | 在线观看亚洲 | 一级毛片免费观看视频 | 97涩色 | 黑人猛交 | 久久精品一区二区三区不卡 | 亚洲精品久久久久久久网站 | 九九精品久久久久久久久 | 亚洲高清在线观看 | 母狗求操| 亚洲欧美精品一区天堂久久 | 免费xxx视频| 国产精品一区二区三区免费视频 | 中文字幕最新在线 | 自拍在线 | 精品一久久香蕉国产二月 | 精品一区亚洲 | 秋霞毛片 | 羞羞网站在线看 | 欧美日韩亚洲高清不卡一区二区三区 | 日本在线不卡一区二区 | 国产成人精品日本亚洲语言 | 在线观看视频一区二区 | 国产成人精品午夜二三区 | 2021国产成人午夜精品 | 亚洲视频在线看 | 成人自拍偷拍 | 国产一级理论免费版 | xxx在线视频 | 午夜久久久久久网站 | 精品剧情v国产在免费线观看 | 日本中文字幕在线视频 | 九九精品免费观看在线 | 亚洲精品色综合久久久 | 国产在线播放成人免费 | 2020国产成人精品视频人 | 性xxxxx大片免费视频 | 欧美13一14周岁a在线播放 |