不能快读的快读?
查看原帖
不能快读的快读?
1108078
nothing_exe_studio楼主2024/5/13 19:17

code : https://www.luogu.com.cn/problem/P2661

#include <iostream>
using namespace std;
using ll = long long;
ll n;
ll a[200010];
ll whead[200010], wnext[200010], ver[200010];
ll tot;
ll mp[200010];
long long ans = 114514114514;
char getint(int& x) {
    x = 0;
    int neg = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-')
            neg = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        x = 10 * x + c - '0';
        c = getchar();
    }
    x = x * neg;
    return c;
}
void add(ll x, ll y) {
    ver[++tot] = y;
    wnext[tot] = whead[x];
    whead[x] = tot;
    return;
}
void f(ll x, ll cs) {
    if (mp[x]) {
        ans = ans < cs ? ans : cs;
        return;
    }
    mp[x] = 1;
    for (ll i = whead[x]; i; i = wnext[i]) {
        ll y = ver[i];
        f(y, cs + 1);
    }
    mp[x] = 0;
    return;
}
int main() {
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        int j = getint(j);
        a[i] = j;
        add(i, a[i]);
    }
    for (int i = 1; i <= n; ++i)
        f(i, 0);
    cout << ans;
}
2024/5/13 19:17
加载中...