记录编号 355119 评测结果 AAAAAAAAAA
题目名称 教官 最终得分 100
用户昵称 Gravatarsxysxy 是否通过 通过
代码语言 C++ 运行时间 0.020 s
提交时间 2016-11-23 20:45:02 内存使用 0.62 MiB
显示代码纯文本
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <iostream>
#include <queue>
#include <list>
using namespace std;
#define MAXN 10001
int scc[MAXN];
int scc_size[MAXN];
int scc_cnt;
int low[MAXN], dfn[MAXN];
vector<int> G[MAXN];
int dfn_tim;
int stk[MAXN];
int stk_top;
typedef long long LL;
LL gcd(LL a, LL b)
{
    return b?gcd(b, a%b):a;
}
LL lcm(LL a, LL b)
{
    return a*b/gcd(a, b);
}
LL ans;
void tarjan(int u)
{
    low[u] = dfn[u] = ++dfn_tim;
    stk[stk_top++] = u;
    for(int i = 0; i < G[u].size(); i++)
    {
        int to = G[u][i];
        if(!dfn[to])
        {
            tarjan(to);
            low[u] = min(low[u], low[to]);
        }else if(!scc[to])
            low[u] = min(low[u], dfn[to]);
    }
    if(low[u] == dfn[u])
    {
        scc_cnt++;
        while(stk_top)
        {
            int x = stk[--stk_top];
            scc[x] = scc_cnt;
            scc_size[scc_cnt]++;
            if(x == u)break;
        }
        if(ans)ans = lcm(ans, scc_size[scc_cnt]);
        else ans = scc_size[scc_cnt];
    }
}
int main()
{
    freopen("officer.in", "r", stdin);
    freopen("officer.out", "w", stdout);
    int n;
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
    {
        int x;
        scanf("%d", &x);
        G[i].push_back(x);
    }
    for(int i = 1; i <= n; i++)
        if(!dfn[i])tarjan(i);
    printf("%lld\n", ans);
    return 0;
}